Differences between JUnit 3 and JUnit 4
Introduction
In this post, we will discuss some of the differences between JUnit 3 and JUnit 4. The most important difference is that in JUnit 3, we need to extend TestCase (junit.framework.TestCase) to write junit tests. Every method which has the test code in it, should start with testXXXX method.
Sample JUnit 3 Listing:
package com.testingdocs.junit; import junit.framework.TestCase; /** * A Sample JUnit3 testcase */ /** * @author testingdocs */ public class SampleJUnit3TestCase extends TestCase { /** * @param name */ public SampleJUnit3TestCase(String name) { super(name); } /* (non-Javadoc) * @see junit.framework.TestCase#setUp() */ protected void setUp() throws Exception { super.setUp(); } /* (non-Javadoc) * @see junit.framework.TestCase#tearDown() */ protected void tearDown() throws Exception { super.tearDown(); } public void testMethod() { System.out.println("Test method 1"); assertTrue(1 == 1); } }
test prefix methods
If you don’t start test methods with a conventional test**** prefix junit will throw an error
something like “No tests found in the suite.”
If there are no testXXX prefix methods in the test suite, then you will get this error.
junit.framework.AssertionFailedError: No tests found in com.testingdocs.junit.SampleJUnit3TestCase
at junit.framework.Assert.fail(Assert.java:57)
at junit.framework.TestCase.fail(TestCase.java:227)
at junit.framework.TestSuite$1.runTest(TestSuite.java:97)
at junit.framework.TestCase.runBare(TestCase.java:141)
at junit.framework.TestResult$1.protect(TestResult.java:122)
at junit.framework.TestResult.runProtected(TestResult.java:142)
at junit.framework.TestResult.run(TestResult.java:125)
at junit.framework.TestCase.run(TestCase.java:129)
at junit.framework.TestSuite.runTest(TestSuite.java:252)
Annotations in JUnit4
Another notable difference is that, JUnit 4 introduced annotation support. JUnit 4 introduced annotations like @Test, @BeforeClass, @AfterClass, @Before, @After ,@Ignore etc.
JUnit 4 Listing:
package com.testingdocs.junit; import static org.junit.Assert.*; import org.junit.After; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; /** * A sample JUnit4 Test demo * class */ /** * @author testingdocs * */ public class JUnit4TestClass { /** * @throws java.lang.Exception */ @BeforeClass public static void oneTimeSetUp() throws Exception { System.out.println("@BeforeClass annotated method"); } /** * @throws java.lang.Exception */ @AfterClass public static void oneTimeTearDown() throws Exception { System.out.println("@AfterClass annotated method"); } /** * @throws java.lang.Exception */ @Before public void eachMethodFixtureSetUp() throws Exception { System.out.println("@Before annotated method"); } /** * @throws java.lang.Exception */ @After public void eachMethodFixtureTearDown() throws Exception { System.out.println("@After annotated method"); } @Test public void firstTestMethod() { System.out.println("Test method"); fail("Not yet implemented1"); } }
JUnit Tutorial on this website can be found at:
https://www.testingdocs.com/junit-tutorial/
More information on JUnit official website: