Prerequisite

Setup the test environment

  1. Open Eclipse and create a new Java Project.
  2. Click File > New > Java Project > Enter "JseUnitTesting" as project name > Finish

  3. You should already have a source directory called: src. Create another one called test.
  4. Right Click on the project > New > Source Folder



    Enter "test" as Folder Name > Finish

  5. From the menu access Project(or simply Right Click on the project) > Properties > Libraries tab > Add External JARs >



  6. Select the downloaded JAR(junit4.10) from the folder (e.g.: D:\Automation Workspace\lib) > Now we have the junit jar in our build path. Press OK button

  7. In the Package Explorer tab the new added junit library is displayed under Referenced Libraries:



    We are now ready to start developing and running junits in Eclipse IDE.

    Note: If you don’t have Eclipse Helios version or other version which comes with junit 4 plugin, you should download the plugin separately and install it in Eclipse.

Create a Java class

  • Create a Java class in the src namely MyClass with package junitSrc

  • package junitSrc;
    
    public class MyClass {
    	public int multiply(int x, int y) {
    		return x / y;
    	}
    }
    

    Create a JUnit test

    Create a JUnit test in test namely MyClassTest

    Right click on test folder > New > Other... > Java > JUnit > JUnitTestCase >

    Next > Finish

    Create a test with the following code looking in the figure

    package junitTest;
    
    import junitSrc.MyClass;
    import org.junit.Test;
    import static org.junit.Assert.assertEquals;
    
    public class MyClassTest {
    
    	@Test
    	public void testMultiply() {
    		MyClass tester = new MyClass();
    		assertEquals("Result::", 50, tester.multiply(10, 5));
    	}
    }
    

    Run your test via Eclipse

    Right click on your new test class and select Run-As > JUnit Test.

    The result of the tests will be displayed in the JUnit View.

    The test should be failing (indicated via a red bar).

    This is because our multiplier class is currently not working correctly (it does a division instead of multiplication). Fix the bug and re-run test to get a green bar.

    Create a Test Suite

    If you have several tests you can combine them into a test suite. Running a test suite will execute all tests in that suite.

    To create a test suite, select your test classes > right click on it > New > Other... > JUnit > JUnit Test Suite

    Select "Next" and select the methods for which you want to create a test.

    Change the code to the following to make your test suite run your test. If you develop another test later you can add it to @Suite.SuiteClasses.

    package junitTest;
    
    import org.junit.runner.RunWith;
    import org.junit.runners.Suite;
    
    @RunWith(Suite.class)
    @Suite.SuiteClasses({ MyClassTest.class })
    public class AllTests {} 
    
    

    Run your test via code

    You can also run your tests from via your own code. The class org.junit.runner.JUnitCore provides the method runClasses() which allows you to run one or several tests classes. As a return parameter you receive an object of the type org.junit.runner.Result. This object can be used to retrieve information about the tests.

    In your "test" folder create a new class MyTestRunner with the following code. This class will execute your test class and write potential failures to the console.

    package junitTest;
    
    import org.junit.runner.JUnitCore;
    import org.junit.runner.Result;
    import org.junit.runner.notification.Failure;
    
    public class MyTestRunner {
      public static void main(String[] args) {
    	Result result = JUnitCore.runClasses(MyClassTest.class);
    	for (Failure failure : result.getFailures()) {
    	  System.out.println(failure.toString());
    	}
      }
    } 
    


    For more details please go through my post Getting started with Selenium RC (selenium 1) with JUnit 3 or JUnit 4 and more examples.