Wednesday, 20 June 2012

JUnit Documentation

JUnit Documentation

JUNIT

JUnit is a simple Java testing framework to write tests for you Java application.

1.1. Unit Testing

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 in ellipse. 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.
Create a new source folder test via right-clicking on your project, select "Properties" and choose the "Java Build Path". Select the "Source" tab.



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


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

Right Click the "src" folder, create the following  java class.

public class Multiply {
            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 New JUnit Test Case. Select "New JUnit 4 test" and set the source folder to "test", so that your test class gets created in this folder.



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





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





Create a test with the following code.

import static org.junit.Assert.*;
import org.junit.Test;
public class MultiplyTest {
            @Test
            public void testMultiply() {
            Multiply tester = new Multiply();
            assertEquals("Result", 50, tester.multiply(10, 5));
}

}

3.4. 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.
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 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

Pass a HashMap from Angular Client to Spring boot API

This example is for the case where fileData is very huge and in json format   let map = new Map<string, string>()      map.set(this.ge...