Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to create a "test suite" in JUnit 4

 
Mike London
Ranch Hand
Posts: 1190
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(Simple question, probably)

I'm having problems creating a TestSuite in JUnit4 under MyEclipse. I realize this is because, from what I've been able to find, that TestSuites really aren't supported in 4 (which makes them more difficult to use... <g> .

So, if I have two play classes: Calculator and Hello and two JUnit TestCases, how do I test both of these classes at once? I tried creating a TestSuite anyway, but apparently didn't have the correct constructor method in the actual classes. "JUnit in Action" discusses this issue, but relative to JUnit 3.8.

Thanks very much in advance for any suggestions.

Mike
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://junit.sourceforge.net/javadoc_40/org/junit/runners/Suite.html
 
Mike London
Ranch Hand
Posts: 1190
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Thanks for the info...

I did find the answer to my problem.

Here's what works for me:

@Suite.SuiteClasses({CalculatorTest.class, HelloTest.class} )

----------------

I also noticed that using the regular assert method always returns true in Junit4. I have to use assertEquals(...) for Junit4 to work properly.

This is interesting since one of the Web examples I found using a calculator uses the assert (not assertEquals) and it also returns true even when you screw up the method it's supposed to be testing.

Am I interpreting this correctly?

Mike
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I remember correctly, asserts are disabled per default.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34195
340
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike,
Note that JUnit called the assertEquals() method assert() prior to Java 1.4 when the assert keyword was introduced. It's possible you were looking at an older tutorial from this time.
 
Mike London
Ranch Hand
Posts: 1190
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jeanne,

Possible, but the assert command, in the sample code I downloaded, didn't work at all -- and I'm using Java 5. The code didn't give any errors either. It just didn't do anything...I got a green JUnit test case when there should have been an error. To futher confuse things, the sample code had both types of assert statements (assert(...) and assertEquals(...).

I think what the other person said is probably the culprit...asserts are disabled by default. I thought of that, but wasn't sure when the AssertEquals(...) seemed to work OK.

Thanks for your message.

M
 
M. Johnson
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The JUnit assertXxx methods don't use the assert keyword and thus aren't disabled by default. (though as of JUnit 4 they will throw AssertionErrors, which is what is thrown by the assert keyword). As mentioned previously, the assert keyword *is* disabled by default. One way to enable it is to use the -ea (standing for enable assertions) JVM flag.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic