aspose file tools*
The moose likes Testing and the fly likes How to create a Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Engineering » Testing
Bookmark "How to create a "test suite" in JUnit 4" Watch "How to create a "test suite" in JUnit 4" New topic
Author

How to create a "test suite" in JUnit 4

Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1034
(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

Joined: Jul 11, 2001
Posts: 14112
http://junit.sourceforge.net/javadoc_40/org/junit/runners/Suite.html


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1034
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

Joined: Jul 11, 2001
Posts: 14112
If I remember correctly, asserts are disabled per default.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29283
    
140

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1034
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

Joined: Mar 05, 2007
Posts: 1
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to create a "test suite" in JUnit 4
 
Similar Threads
test suit with jUnit 4
Run as Junit Test case
Ant 1.7 + Junit 4 problems
Should I use TestSuite in Junit 4?
httpUnit report