JUnit Basics


What is JUnit?
JUnit is a framework which is used to test a piece of code written by a developer that executes a specific functionality. It is an instance of the xUnit architecture for unit testing frameworks. JUnit features include:
  • Assertions for testing expected results
  • Test fixtures for sharing common test data
  • Test runners for running tests
Installation of JUnit
Windows
To install JUnit on Windows, follow these steps:
Download from JUnit home page (http://junit.org) and unzip the junit.zip distribution file to a directory referred to as %JUNIT_HOME%.
Add JUnit to the classpath:
set CLASSPATH=%CLASSPATH%;%JUNIT_HOME%\JUnit-4.x.jar

Unix (bash)
To install JUnit on Unix, follow these steps:
Download from JUnit home page (http://junit.org) unzip the junit.zip distribution file to a directory referred to as $JUNIT_HOME.
Add JUnit to the classpath:
export CLASSPATH=$CLASSPATH:$JUNIT_HOME/junit-4.x.jar

Note : Test the installation by running the sample tests distributed with JUnit. Note that the sample tests are located in the installation directory directly, not the junit.jar file. Therefore, make sure that the JUnit installation directory is on your CLASSPATH.
Then simply type:
java org.junit.runner.JUnitCore org.junit.tests.AllTests
All the tests should pass with an "OK" message.
If the tests don't pass, verify that junit.jar is in the CLASSPATH.
Eclipse 
Download the Jar and add the JUnit-4.x.jar to java project.

Use  JUnit with example
Create a Java Class and write method which will return the addition of two integers.
  
public class AddIntegers {

 public int add(int x, int y){
  return x+y;
 }
 
}

Create the JUnit Test case
Right click on Package Explorer and select New -> JUnit Test Case (Select "New JUnit 4 test").

Update the Test case as follow...
  
import static org.junit.Assert.assertEquals;

import org.junit.Test;
import org.junit.runner.JUnitCore;

public class AddJUnitTest {

 @Test
 public void test() {
  AddIntegers add = new AddIntegers();
  assertEquals("result", 5,add.add(3, 2));
 }
 
 public static void main(String[] args) {  // run the test with the test runner used in main()
  JUnitCore.main("AddJUnitTest");
 }

}
Run the JUnit
From Eclipse
Right click on test class and select Run-As > JUnit Test.

Out put will show in JUnit view like..
From console
To run the test from the console, run the below command:
java org.junit.runner.JUnitCore AddJUnitTest


 To run the test with the test runner used in main(), run the below command:
 java AddJUnitTest



Assert Methods
            Some of the useful methods of Assert class which are useful to writing tests. These methods can be used directly: Assert.assertEquals(...), or used through static import:
import static org.junit.Assert.*;
...
assertEquals(...);

assertEquals(java.lang.String message, java.lang.Object expected, java.lang.Object actual) Asserts that two objects are equal.
assertArrayEquals(java.lang.String message, java.lang.Object[] expecteds, java.lang.Object[] actuals) throws org.junit.internal.ArrayComparisonFailure Asserts that two object arrays are equal. If they are not, an AssertionError is thrown with the given message. If expecteds and actuals are null, they are considered equal.
assertArrayEquals(java.lang.Object[] expecteds, java.lang.Object[] actuals) Asserts that two object arrays are equal. If they are not, an AssertionError is thrown. If expected and actual are null, they are considered equal.
assertNotNull(java.lang.String message, java.lang.Object object) Asserts that an object isn't null. If it is an AssertionError is thrown with the given message.
assertNotNull(java.lang.Object object) Asserts that an object isn't null. If it is an AssertionError is thrown.
assertNull(java.lang.String message, java.lang.Object object) Asserts that an object is null. If it is not, an AssertionError is thrown with the given message.
assertNull(java.lang.Object object) Asserts that an object is null. If it isn't an AssertionError is thrown.
assertSame(java.lang.String message, java.lang.Object expected, java.lang.Object actual) Asserts that two objects refer to the same object. If they are not, an AssertionError is thrown with the given message.
assertSame(java.lang.Object expected, java.lang.Object actual)/td> Asserts that two objects refer to the same object. If they are not the same, an AssertionError without a message is thrown.
fail() Fails a test with no message
fail(java.lang.String message) Fails a test with the given message.
More details on Assert Class

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...