JUnit3 TestCase (java 1.4-)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import junit.framework.TestCase;


public class SampleTest extends TestCase {


     private java.util.List emptyList;


     /**
      * Sets up the test fixture.
      * (Called before every test case method.)
      */

     protected void setUp() {
          emptyList = new java.util.ArrayList();
     }


     /**
      * Tears down the test fixture.
      * (Called after every test case method.)
      */

     protected void tearDown() {
          emptyList = null;
     }


     public void testSomeBehavior() {
          assertEquals("Empty list should have 0 elements", 0, emptyList.size());
     }

     public void testForException() {
          try {
               Object o = emptyList.get(0);
               fail("Should raise an IndexOutOfBoundsException");
          }
          catch (IndexOutOfBoundsException success) {
          }
     }

http://agile.csc.ncsu.edu/SEMaterials/tutorials/junit/junit_tutorial_jazz.html#section2_0

JUnit Testing Utility Tutorial

JUnit4 TestSuites Snippets Example

Let me give an example.

First two test classes:

1
2
3
4
5
6
7
8
9
10
import static org.junit.Assert.*;

import org.junit.Test;

public class JU1 {
@Test
public void test1() {
assertNotNull("ABC");
}
}

and

package october;

1
2
3
4
5
6
7
8
9
10
import static org.junit.Assert.*;

import org.junit.Test;

public class JU2 {
@Test
public void test1() {
assertNotNull("ABC");
}
}

and now a suite:

1
2
3
4
5
6
7
8
9
10
package october;

import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;

@RunWith(Suite.class)
@SuiteClasses({JU1.class,JU2.class})
public class AllJU {
}