da JUnit 4 (@Test annotations): advantages:

  • The @Test annotaton is more explicit and is easier to support in tools (for example it's easy to search for all tests this way)
  • Multiple methods can be annotated with @Before/@BeforeClass and @After/@AfterClass providing more flexibility
  • Support for @Rule annotations on things like ExpectedException
  • Support for the @Ignored annotation
  • Support for alternative test runners using @RunWith

see android testing

import org.junit.*;
public class TestFoobar {
    @BeforeClass
    public static void setUpClass() throws Exception {
        // Code executed before the first test method
    }
 
    @Before
    public void setUp() throws Exception {
        // Code executed before each test
    }
 
    @Test
    public void testOneThing() {
        // Code that tests one thing
    }
 
    @Test
    public void testAnotherThing() {
        // Code that tests another thing
    }
 
    @Test
    public void testSomethingElse() {
        // Code that tests something else
    }
 
    @After
    public void tearDown() throws Exception {
        // Code executed after each test
    }
 
    @AfterClass
    public static void tearDownClass() throws Exception {
        // Code executed after the last test method
    }
}

build testSuite

import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
 
@RunWith(Suite.class)
@SuiteClasses({
    Test1.class,
    Test2.class,
    Test3.class,
    Test4.class
})public class TestSuite
{
 /* empty class */
}

JUnit 3:

import junit.framework.*;
public class JavaTest extends TestCase {
   protected int value1, value2;
   // assigning the values
   protected void setUp(){
      value1=3;
      value2=3;
   }
   // test method to add two values
   public void testAdd(){
      double result= value1 + value2;
      assertTrue(result == 6);
   }
}