Saturday, June 28, 2008

Junit 1

What Is JUnit?

What is JUnit? You need to remember the following points when answering this question:

  • It is a software testing framework to for unit testing.
  • It is written in Java and designed to test Java applications.
  • It is an Open Source Software maintained by the JUnit.org community.

Answer from the JUnit FAQ:

JUnit is a simple, open source framework to write and run repeatable tests. 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

JUnit was originally written by Erich Gamma and Kent Beck.


Why Do You Use JUnit to Test Your Code?

This is a commonly asked question in a job interview. Your answer should have these points:

  • I believe that writing more tests will make me more productive, not less productive.
  • I believe that tests should be done as soon as possible at the code unit level.
  • I believe that using JUnit makes unit testing easier and faster.

How To Wirte a Simple JUnit Test Class?

This is a common test in a job interview. You should be able to write this simple test class with one test method:

import org.junit.*;
// by FYICenter.com
public class HelloTest {
@Test public void testHello() {
String message = "Hello World!";
Assert.assertEquals(12, message.length());
}
}

How To Compile a JUnit Test Class?

Compiling a JUnit test class is like compiling any other Java classes. The only thing you need watch out is that the JUnit JAR file must be included in the classpath. For example, to compile the test class HelloTest.java described previously, you should do this:

javac -cp junit-4.4.jar HelloTest.java

dir HelloTest.*
453 HelloTest.class
183 HelloTest.java


How To Run a JUnit Test Class?

A JUnit test class usually contains a number of test methods. You can run all test methods in a JUnit test class with the JUnitCore runner class. For example, to run the test class HelloTest.java described previously, you should do this:

java -cp .; junit-4.4.jar org.junit.runner.JUnitCore HelloTest JUnit version 4.4 . Time: 0.015 OK (1 test)

What CLASSPATH Settings Are Needed to Run JUnit?

It doesn't matter if you run your JUnit tests from a command line, from an IDE, or from "ant", you must define your CLASSPATH settings correctly. Here is what recommended by the JUnit FAQ with some minor changes:

To run your JUnit tests, you'll need the following elemements in your CLASSPATH:

  • The JUnit JAR file.
  • Location of your JUnit test classes.
  • Location of classes to be tested.
  • JAR files of class libraries that are required by classes to be tested.

If attempting to run your tests results in a NoClassDefFoundError, then something is missing from your CLASSPATH.

If you are running your JUnit tests from a command line on a Windows system:

set CLASSPATH=c:\A\junit-4.4.jar;c:\B\test_classes;
c:\B\target_classes;c:\D\3rd_party.jar

If you are running your JUnit tests from a command line on a Unix (bash) system:

export CLASSPATH=/A/junit-4.4.jar:/B/test_classes:

How Do I Run JUnit Tests from Command Window?

To run JUnit tests from a command window, you need to check the following list:

1. Make sure that JDK is installed and the "java" command program is accessible through the PATH setting. Type "java -version" at the command prompt, you should see the JVM reports you back the version string.

2. Make sure that the CLASSPATH is defined as shown in the previous question.

3. Invoke the JUnit runner by entering the following command:

java org.junit.runner.JUnitCore

How To Write a JUnit Test Method?

This interview question is to check if you know the basic rules about writing a JUnit test method:

  • You need to mark the method as a JUnit test method with the JUnit annotation: @org.junit.Test.
  • A JUnit test method must be a "public" method. This allows the runner class to access this method.
  • A JUnit test method must be a "void" method. The runner class does not check any return values.
  • A JUnit test should perform one JUnit assertion - calling an org.junit.Assert.assertXXX() method.

Here is a simple JUnit test method:

import org.junit.*;
// by FYICenter.com
...
@Test public void testHello() {
String message = "Hello World!";
Assert.assertEquals(12, message.length());


}

Can You Provide a List of Assertion Methods Supported by JUnit 4.4?

You should be able to answer this question by looking up the org.junit.Assert class API document. Here is a list of assertino methods supported by JUnit 4.4:

  • assertEquals(expected, actual)
  • assertEquals(message, expected, actual)
  • assertEquals(expected, actual, delta)
  • assertEquals(message, expected, actual, delta)
  • assertFalse(condition)
  • assertFalse(message, condition)
  • assertNotNull(object)
  • assertNotNull(message, object)
  • assertNotSame(expected, actual)
  • assertNotSame(message, expected, actual)
  • assertNull(object)
  • assertNull(message, object)
  • assertSame(expected, actual)
  • assertSame(message, expected, actual)
  • assertTrue(condition)
  • assertTrue(message, condition)
  • fail()
  • fail(message)
  • failNotEquals(message, expected, actual)
  • failNotSame(message, expected, actual)
  • failSame(message)

Java Doc

org.junit
Class Assert

java.lang.Object extended by org.junit.Assert
public class Assert
extends java.lang.Object

A set of assertion methods useful for writing tests. Only failed assertions are recorded. These methods can be used directly: Assert.assertEquals(...), however, they read better if they are referenced through static import:
import static org.junit.Assert.*;
...
assertEquals(...);

See Also:
AssertionError


Constructor Detail

Assert

protected Assert()
Protect constructor since it is a static only class

Method Detail

assertTrue

public static void assertTrue(java.lang.String message, boolean condition)
Asserts that a condition is true. If it isn't it throws an AssertionError with the given message.

Parameters:
message - the identifying message or null for the AssertionError
condition - condition to be checked

assertTrue

public static void assertTrue(boolean condition)
Asserts that a condition is true. If it isn't it throws an AssertionError without a message.

Parameters:
condition - condition to be checked

assertFalse

public static void assertFalse(java.lang.String message, boolean condition)
Asserts that a condition is false. If it isn't it throws an AssertionError with the given message.

Parameters:
message - the identifying message or null for the AssertionError
condition - condition to be checked

assertFalse

public static void assertFalse(boolean condition)
Asserts that a condition is false. If it isn't it throws an AssertionError without a message.

Parameters:
condition - condition to be checked

fail

public static void fail(java.lang.String message)
Fails a test with the given message.

Parameters:
message - the identifying message or null for the AssertionError
See Also:
AssertionError

fail

public static void fail()
Fails a test with no message.



assertEquals

public static void assertEquals(java.lang.String message, java.lang.Object expected, java.lang.Object actual)
Asserts that two objects are equal. If they are not, an AssertionError is thrown with the given message.

Parameters:
message - the identifying message or null for the AssertionError
expected - expected value
actual - actual value

assertEquals

public static void assertEquals(java.lang.Object expected, java.lang.Object actual)
Asserts that two objects are equal. If they are not, an AssertionError without a message is thrown.

Parameters:
expected - expected value
actual - the value to check against expected

assertEquals

public static void assertEquals(java.lang.String message, java.lang.Object[] expecteds, java.lang.Object[] actuals)
Asserts that two object arrays are equal. If they are not, an AssertionError is thrown with the given message.

Parameters:
message - the identifying message or null for the AssertionError
expecteds - Object array or array of arrays (multi-dimensional array) with expected values
actuals - Object array or array of arrays (multi-dimensional array) with actual values

assertEquals

public static void assertEquals(java.lang.Object[] expecteds, java.lang.Object[] actuals)
Asserts that two object arrays are equal. If they are not, an AssertionError is thrown.

Parameters:
expecteds - Object array or array of arrays (multi-dimensional array) with expected values
actuals - Object array or array of arrays (multi-dimensional array) with actual values

assertEquals

public static void assertEquals(java.lang.String message, double expected, double actual, double delta)
Asserts that two doubles are equal to within a positive delta. If they are not, an AssertionError is thrown with the given message. If the expected value is infinity then the delta value is ignored. NaNs are considered equal: assertEquals(Double.NaN, Double.NaN, *) passes

Parameters:
message - the identifying message or null for the AssertionError
expected - expected value
actual - the value to check against expected
delta - the maximum delta between expected and actual for which both numbers are still considered equal.

assertEquals

public static void assertEquals(double expected, double actual, double delta)
Asserts that two doubles are equal to within a positive delta. If they are not, an AssertionError is thrown. If the expected value is infinity then the delta value is ignored.NaNs are considered equal: assertEquals(Double.NaN, Double.NaN, *) passes

Parameters:
expected - expected value
actual - the value to check against expected
delta - the maximum delta between expected and actual for which both numbers are still considered equal.

assertEquals

public static void assertEquals(java.lang.String message, float expected, float actual, float delta)
Asserts that two floats are equal to within a positive delta. If they are not, an AssertionError is thrown with the given message. If the expected value is infinity then the delta value is ignored.NaNs are considered equal: assertEquals(Float.NaN, Float.NaN, *) passes

Parameters:
message - the identifying message or null for the AssertionError
expected - the expected float value
actual - the float value to check against expected
delta - the maximum delta between expected and actual for which both numbers are still considered equal.

assertEquals

public static void assertEquals(float expected, float actual, float delta)
Asserts that two floats are equal to within a positive delta. If they are not, an AssertionError is thrown. If the expected value is infinity then the delta value is ignored. NaNs are considered equal: assertEquals(Float.NaN, Float.NaN, *) passes

Parameters:
expected - the expected value
actual - the value to check against expected
delta - the maximum delta between expected and actual for which both numbers are still considered equal.

assertNotNull

public static void 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.

Parameters:
message - the identifying message or null for the AssertionError
object - Object to check or null

assertNotNull

public static void assertNotNull(java.lang.Object object)
Asserts that an object isn't null. If it is an AssertionError is thrown.

Parameters:
object - Object to check or null

assertNull

public static void 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.

Parameters:
message - the identifying message or null for the AssertionError
object - Object to check or null

assertNull

public static void assertNull(java.lang.Object object)
Asserts that an object is null. If it isn't an AssertionError is thrown.

Parameters:
object - Object to check or null

assertSame

public static void 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.

Parameters:
message - the identifying message or null for the AssertionError
expected - the expected object
actual - the object to compare to expected

assertSame

public static void assertSame(java.lang.Object expected, java.lang.Object actual)
Asserts that two objects refer to the same object. If they are not the same, an AssertionError without a message is thrown.

Parameters:
expected - the expected object
actual - the object to compare to expected

assertNotSame

public static void assertNotSame(java.lang.String message, java.lang.Object unexpected, java.lang.Object actual)
Asserts that two objects do not refer to the same object. If they do refer to the same object, an AssertionError is thrown with the given message.

Parameters:
message - the identifying message or null for the AssertionError
unexpected - the object you don't expect
actual - the object to compare to unexpected

assertNotSame

public static void assertNotSame(java.lang.Object unexpected, java.lang.Object actual)
Asserts that two objects do not refer to the same object. If they do refer to the same object, an AssertionError without a message is thrown.

Parameters:
unexpected - the object you don't expect
actual - the object to compare to unexpected

No comments:

Topics