JUnit 4 @BeforeClass & @AfterClass when using Suites

Write a @BeforeClass method in the AllTests class which will be executed when the suite is started. public class MyTests1 { @BeforeClass public static void beforeClass() { System.out.println(“MyTests1.beforeClass”); } @Before public void before() { System.out.println(“MyTests1.before”); } @AfterClass public static void afterClass() { System.out.println(“MyTests1.AfterClass”); } @After public void after() { System.out.println(“MyTests1.after”); } @Test public void test1() … Read more

Test cases in inner classes with JUnit

You should annontate your class with @RunWith(Enclosed.class), and like others said, declare the inner classes as static: @RunWith(Enclosed.class) public class DogTests { public static class BarkTests { @Test public void quietBark_IsAtLeastAudible() { } @Test public void loudBark_ScaresAveragePerson() { } } public static class EatTests { @Test public void normalFood_IsEaten() { } @Test public void badFood_ThrowsFit() … Read more

Why is assertEquals(double,double) deprecated in JUnit?

It’s deprecated because of the double’s precision problems. If you note, there’s another method assertEquals(double expected, double actual, double delta) which allows a delta precision loss. JavaDoc: Asserts that two doubles are equal to within a positive delta. If they are not, an AssertionError is thrown. If the expected value is infinity then the delta … Read more

How to run all JUnit tests in a category/suite with Ant?

Right, I got it working with <batchtest> quite simply: <junit showoutput=”true” printsummary=”yes” fork=”yes”> <formatter type=”xml”/> <classpath refid=”test.classpath”/> <batchtest todir=”${test.reports}”> <fileset dir=”${classes}”> <include name=”**/FastTestSuite.class”/> </fileset> </batchtest> </junit> I had tried <batchtest> earlier, but had made the silly mistake of using “**/FastTestSuite.java” instead of “**/FastTestSuite.class” in the <include> element… Sorry about that 🙂 NB: it’s necessary to … Read more

Cleanup after all junit tests

I’m using JUnit 4.9. Will this help?: import junit.framework.TestCase; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.runner.RunWith; import org.junit.runners.Suite; import org.junit.runners.Suite.SuiteClasses; @RunWith(Suite.class) @SuiteClasses({First.class,Second.class,Third.class}) public class RunTestSuite extends TestCase { @BeforeClass public static void doYourOneTimeSetup() { … } @AfterClass public static void doYourOneTimeTeardown() { … } } Edit: I am quite positive (unless I misunderstand your question) that … Read more