Hi.
I havn't really been seriously into testing yet and therefore I was
looking into JUnit as it seems it is much used. Also version 4 uses
annotations which looked very much simpler than previous versions.
Anyhow, I've made a new project using NetBeans and have added the latest
JUnit library (version 4.4) for compiling and testing. The examples I've
seen seems very simple however this simple test give me no output,
selecting "Test Project" from the right-click menu. The only thing shown
in the "JUnit Test Results" window is "JUnit results root node" under
statistics. I hope someone can enlighten me :) Code follows:
package junittest;
import org.junit.Test;
import static org.junit.Assert.*;
public class Main {
public Main() {
testmethod();
}
public static void main(String[] args) {
new Main();
}
@Test
private void testmethod() {
int a = 5;
int b = 3;
int c = a + b;
assertEquals("Number error",new Integer(9), new Integer(c));
}
}
As a side question, how is this testing usually performed? Is it common
to add testing from the very beginning and are they left in the code
after the tests has been conducted? It's clutter in a sense I guess, but
on the other hand it could become quite a lot to remove?
> Hi.
>
> I havn't really been seriously into testing yet and therefore I was
> looking into JUnit as it seems it is much used. Also version 4 uses
> annotations which looked very much simpler than previous versions.
NetBeans doesn't support JUnit 4. Run your JUnit 4 tests as a normal
application, run them outside of the IDE, or use the supported Junit
3.
> As a side question, how is this testing usually performed? Is it common
> to add testing from the very beginning
Yes. Before you write the code you write the unit test.
> and are they left in the code
> after the tests has been conducted?
Unit tests are not in the production code. Unit tests are right from
the beginning supposed to be in own, separate classes.
Open a class in NetBeans. Press Shift-Ctrl-U. See how NetBeans creates
a separate test class for your class. Implement the unit tests for
your class in that separate test class.
> It's clutter in a sense I guess, but
> on the other hand it could become quite a lot to remove?
Unit tests are not removed, because they are not in the production
code. They are in separate classes which one usually doesn't ship.
news@arenybakk.com - 30 Oct 2007 11:06 GMT
> > Hi.
>
[quoted text clipped - 5 lines]
> application, run them outside of the IDE, or use the supported Junit
> 3.
I have tested it with NetBeans 6, which supposedly, supports JUnit 4.
The JUnit 4.1 library is included in the IDE. The code I wrote gave no
result there either.
> > As a side question, how is this testing usually performed? Is it common
> > to add testing from the very beginning
[quoted text clipped - 6 lines]
> Unit tests are not in the production code. Unit tests are right from
> the beginning supposed to be in own, separate classes.
I thought the thing about JUnit 4 was that one doesn't have to make a
separate test class?
> Open a class in NetBeans. Press Shift-Ctrl-U. See how NetBeans creates
> a separate test class for your class. Implement the unit tests for
[quoted text clipped - 5 lines]
> Unit tests are not removed, because they are not in the production
> code. They are in separate classes which one usually doesn't ship.
EricF - 31 Oct 2007 05:22 GMT
>> > Hi.
>>
[quoted text clipped - 23 lines]
>I thought the thing about JUnit 4 was that one doesn't have to make a
>separate test class?
No! You need a test class.
Eric
> Hi.
>
[quoted text clipped - 8 lines]
> in the "JUnit Test Results" window is "JUnit results root node" under
> statistics. I hope someone can enlighten me :) Code follows:
I haven't had the opportunity to use JUnit with NetBeans, but I can
point out a few things that seem to have escaped your attention:
- first, a unit test class needs no main(). The test runner
(NetBeans ) is responsible for instantiating the test class and
runnung its test methods.
- second, it is considered good form to give your test class and its
methods a more descriptive name. In this case, something like
"IntegerMathTest" would be a nice descriptive class name.
- third, have a look at all the different flavors of assertEquals().
"assertEquals(9, c)" would have done nicely. Oh, and read up on auto-
boxing/unboxing...
> package junittest;
>
[quoted text clipped - 25 lines]
> after the tests has been conducted? It's clutter in a sense I guess, but
> on the other hand it could become quite a lot to remove?