4 Replies Latest reply: Jun 26, 2008 7:10 AM by 807589 RSS

    JUnit4 extend TestCase or not ?

    807589
      Hello,
      I'm not sure whether or not my JUnit4 class has to extend the TestCase class or not.

      When it extends the TestCase, my tests behaves as JUnit3:
      - only methods starting with testMyTest are executed
      - @Before @After @Ignore are ignored
      -

      When it doesnt extend TestCase:
      - @Before @After @Ignore workds fine
      - I can't add the test class in a TestSuite
      - I have to statically import import static org.junit.Assert.*;

      What is the correct usage ?

      Thanks in advance for your help.

      Regards,
      Tex
        • 1. Re: JUnit4 extend TestCase or not ?
          mlk
          What is the correct usage ?
          Not extending TestCase.
          • 2. Re: JUnit4 extend TestCase or not ?
            791266
            - I can't add the test class in a TestSuite
            You can create something similar:
            @RunWith(Suite.class)
            @Suite.SuiteClasses({
                TestClass1.class,
                TestClass2.class,
            })
            
            public class ExampleSuite {
                public static junit.framework.Test suite() {
                    return new JUnit4TestAdapter(ExampleSuite.class);
                }
            }
            - I have to statically import import static org.junit.Assert.*;
            Why is that a problem?

            Kaj
            • 3. Re: JUnit4 extend TestCase or not ?
              807589
              - I have to statically import import static org.junit.Assert.*;
              Why is that a problem?
              Agreed, and no, you don't have to do a static import, but it sure cuts down the clutter.

              ~
              • 4. Re: JUnit4 extend TestCase or not ?
                807589
                ok. the static import are fine for me.

                kajbj wrote:
                - I can't add the test class in a TestSuite
                You can create something similar:
                @RunWith(Suite.class)
                @Suite.SuiteClasses({
                TestClass1.class,
                TestClass2.class,
                })
                
                public class ExampleSuite {
                public static junit.framework.Test suite() {
                return new JUnit4TestAdapter(ExampleSuite.class);
                }
                }
                I will try the TestSuite as you specified. I'm not sure to understand thought :)

                Tex