Friday, April 20, 2012

http://www.vogella.com/articles/JUnit/article.html

 A unit test is a piece of code written by a developer that executes a specific functionality in the code under test. Unit tests ensure that code is working as intended and validate that this is still the case after code changes.

1.2. Unit Testing with JUnit

JUnit 4.x is a test framework which uses annotations to identify methods that are tests. JUnit assumes that all test methods can be executed in an arbitrary order. Therefore tests should not depend on other tests.
To write a test with JUnit
  • Annotate a method with @org.junit.Test
  • Use a method provided by JUnit to check the expected result of the code execution versus the actual result

You can use Eclipse or the class "org.junit.runner.JUnitCore" to run the test.

2. Installation of JUnit

If you use Eclipse you can use the integrated JUnit in Eclipse for your testing.
If you want to control the used JUnit library explicitly, download JUnit4.x.jar from the JUnit website at http://www.junit.org/ . The download contains the "junit-4.*.jar" which is the JUnit library. Add this library to your Java project and add it to the classpath.

3. Using JUnit

3.1. Preparation

Create a new project de.vogella.junit.first. We want to create the unit tests in a separate folder. The creation of a separate folder for tests is not mandatory. But it is a good practice to keep the code separated from the regular code. You might even create a separate project for the test classes, but we skip this step to make this example simpler.
Create a new source folder test via right-clicking on your project, select "Properties" and choose the "Java Build Path". Select the "Source" tab.

Create new source folder for the tests

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

Creating a new folder

Alternatively you can add a new source folder by right-clicking on a project and selecting New Source Folder.

3.2. Create a Java class

In the "src" folder, create the de.vogella.junit.first package and the following class.

    
package de.vogella.junit.first;

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

3.3. Create a JUnit test

Right click on your new class in the Package Explorer and select NewJUnit Test Case. Select "New JUnit 4 test" and set the source folder to "test", so that your test class gets created in this folder.

Create new test class

Press "Next" and select the methods which you want to test.

Selecting the methods to test

If the JUnit library in not part of your classpath, Eclipse will prompt you to do so.

Eclipes prompt for adding JUnit to the project class path

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

3.4. Run your test via Eclipse

Right click on your new test class and select Run-AsJUnit Test.

Run JUnit test via Eclipse

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

Result of running a unit test

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.
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 classesright click on itNewOtherJUnitTest Suite.

Create a 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 mypackage;

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

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

   

No comments:

Post a Comment