Skip to main content

Notice: This Wiki is now read only and edits are no longer possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "User:Michael.norman.oracle.com/Info on JUnit4"

(Information on JUnit4)
(Information on JUnit4)
Line 17: Line 17:
 
  <font color="#000000">'''<s><font color="#7f0055">import</font>'''<font color="#7f0055"> junit</font><font color="#7f0055">.</font><font color="#7f0055">framework</font><font color="#7f0055">.</font><font color="#7f0055">TestCase</font><font color="#7f0055"><nowiki>;</nowiki></font></s>
 
  <font color="#000000">'''<s><font color="#7f0055">import</font>'''<font color="#7f0055"> junit</font><font color="#7f0055">.</font><font color="#7f0055">framework</font><font color="#7f0055">.</font><font color="#7f0055">TestCase</font><font color="#7f0055"><nowiki>;</nowiki></font></s>
 
  '''<font color="#7f0055">import</font>'''<font color="#7f0055"> org</font><font color="#7f0055">.</font><font color="#7f0055">junit</font><font color="#7f0055">.</font><font color="#7f0055">Test</font><font color="#7f0055"><nowiki>;</nowiki></font>
 
  '''<font color="#7f0055">import</font>'''<font color="#7f0055"> org</font><font color="#7f0055">.</font><font color="#7f0055">junit</font><font color="#7f0055">.</font><font color="#7f0055">Test</font><font color="#7f0055"><nowiki>;</nowiki></font>
  '''<font color="#000000">'''<font color="#7f0055">import</font>'''<font color="#7f0055"> static org.junit.Assert.*;<nowiki>;</nowiki></font>
+
  '''<font color="#000000">'''<font color="#7f0055">import</font>'''<font color="#7f0055"> static org.junit.Assert.*<nowiki>;</nowiki></font>
 
   
 
   
 
  '''<font color="#7f0055">public</font>''' '''<font color="#7f0055">class</font>''' CalculatorTest '''<s><font color="#7f0055">extends</font>''' TestCase</s> {
 
  '''<font color="#7f0055">public</font>''' '''<font color="#7f0055">class</font>''' CalculatorTest '''<s><font color="#7f0055">extends</font>''' TestCase</s> {
Line 41: Line 41:
 
   @After
 
   @After
 
   '''<font color="#7f0055">public void'''</font> cleanupTestData() { ... }
 
   '''<font color="#7f0055">public void'''</font> cleanupTestData() { ... }
 +
}
 +
* improved fixture lifecycle management:
 +
import junit.framework.*;
 +
import junit.extensions.TestSetup;
 +
 +
public class AllTestsOneTimeSetup {
 +
 +
    public static Test suite() {
 +
 +
        TestSuite suite = new TestSuite();
 +
 +
        suite.addTest(SomeTest.suite());
 +
        suite.addTest(AnotherTest.suite());
 +
 +
        TestSetup wrapper = new TestSetup(suite) {
 +
 +
            protected void setUp() {
 +
                oneTimeSetUp();
 +
            }
 +
 +
            protected void tearDown() {
 +
                oneTimeTearDown();
 +
            }
 +
        };
 +
 +
        return wrapper;
 +
    }
 +
 +
    public static void oneTimeSetUp() {
 +
        // one-time initialization code
 +
    }
 +
 +
    public static void oneTimeTearDown() {
 +
        // one-time cleanup code
 +
    }
 
  }
 
  }

Revision as of 15:22, 4 December 2007

Information on JUnit4

  • successor to most widely used unit-testing framework for Java, JUnit3
  • under active development (last JUnit3 release 3.8.2 2006-03-03)
    • JUnit 4 released 2006-02-16
      • 4.1 2006-04-27
      • 4.2 2006-11-16
      • 4.3 2006-11-16
        • 4.3.1 2007-03-28
      • 4.4 (current as of 071204) 2007-07-18
  • based on Java 5 features:
    • no longer need to extend class TestCase
    • test-method names do not have to start with the prefix test
    • mark your test method with a @Test annotation
    • use static import's to get assert methods (instead of via inheritance)
import junit.framework.TestCase;
import org.junit.Test;
import static org.junit.Assert.*;

public class CalculatorTest extends TestCase {

    @Test
    public void testadd() { 
        ....
        assertEquals(4, calculator.add( 1, 3 ));
    } 
}

  • improved test lifecycle management:
    • setUp() and tearDown() replaced with @Before and @After annotations (allows for multiple @Before/@After methods)
public class CalculatorTest {

  @Before
  public void prepareTestData() { ... }

  @Before
  public void setupMocks() { ... }

  @After
  public void cleanupTestData() { ... }
}
  • improved fixture lifecycle management:
import junit.framework.*;
import junit.extensions.TestSetup;

public class AllTestsOneTimeSetup {

   public static Test suite() {

       TestSuite suite = new TestSuite();

       suite.addTest(SomeTest.suite());
       suite.addTest(AnotherTest.suite());

       TestSetup wrapper = new TestSetup(suite) {

           protected void setUp() {
               oneTimeSetUp();
           }

           protected void tearDown() {
               oneTimeTearDown();
           }
       };

       return wrapper;
   }

   public static void oneTimeSetUp() {
       // one-time initialization code
   }

   public static void oneTimeTearDown() {
       // one-time cleanup code
   }
}

Back to the top