Free tutorials for Java, Eclipse and Web programming



Follow me on twitter

2. JUnit with Eclipse

2.1. Preparation

Create a new project "de.vogella.junit.first". We want to create the unit tests in a separate folder. Create therefore a new source folder "test" via right mouse click on your project, select properties and choose the "Java Build Path". Select the tab source code.

Press "Add folder" then then press "Create new folder". Create the folder "test".

The creation of an separate folder for the test is not mandatory. But it is good advice to keep the test coding separate from the normal coding.

2.2. Create a Java class

Create a package "de.vogella.junit.first" and the following class.

				

package de.vogella.junit.first; public class MyClass { public int multiply(int x, int y) { return x / y; } }

2.3. Create a JUnit test

Select your new class, right mouse click and select New ->JUnit Test case, change the source folder to JUnit. Select "New JUnit 4 test". Make sure you change the source folder to test.

Press next and select the methods which you want to test.

If you have not yet JUnit in your classpath, Eclipse will asked you if it should be added to the classpath.

Create a test with the following code.

				

package de.vogella.junit.first; 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)); } }

2.4. Run your test via Eclipse

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

The test should be failing (indicated via a red bar). This is due to the fact that 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 light.

If you have several tests you can combine them into a test suite. All test in this test suite will then be executed if you run the test suite. To create a new test suite, select your test classes, right mouse click-> New-> Other -> JUnit -Test Suite

Select next and select the methods you would like to have test created for.

Tip

This does currently not work for JUnit4.0 testcases. See Bug Report

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

				
package mypackage;

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

@RunWith(Suite.class)
@Suite.SuiteClasses( { MyClassTest.class })
public class AllTests {
}

			

2.5. Run your test via code

You can also run your test via your own coding. 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 type "org.junit.runner.Result". This object can be used to retrieve information about the tests and provides information about the failed tests.

Create in your "test" folder a new class "MyTestRunner" with the following coding. This class will execute your test class and write potential failures to the console.

				
package de.vogella.junit.first;

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());
		}
	}
}