Saturday, June 28, 2008

Junit 5

Do You Have To Write a Test for Everything?

No, just test everything that could reasonably break.

Be practical and maximize your testing investment. Remember that investments in testing are equal investments in design. If defects aren't being reported and your design responds well to change, then you're probably testing enough. If you're spending a lot of time fixing defects and your design is difficult to grow, you should write more tests.

If something is difficult to test, it's usually an opportunity for a design improvement. Look to improve the design so that it's easier to test, and by doing so a better design will usually emerge.

How Often Should You Run Your JUnit Tests?

Run all your unit tests as often as possible, ideally every time the code is changed. Make sure all your unit tests always run at 100%. Frequent testing gives you confidence that your changes didn't break anything and generally lowers the stress of programming in the dark.

For larger systems, you may just run specific test suites that are relevant to the code you're working on.

Run all your acceptance, integration, stress, and unit tests at least once per day (or night).

If you're using Eclipse, be sure to check out David Saff's continuous testing plug-in.

What Do You Do When a Defect Is Reported?

Test-driven development generally lowers the defect density of software. But we're all fallible, so sometimes a defect will slip through. When this happens, write a failing test that exposes the defect. When the test passes, you know the defect is fixed!

Don't forget to use this as a learning opportunity. Perhaps the defect could have been prevented by being more aggressive about testing everything that could reasonably break.

Or perhaps there are other places in the application that have the similar code that might break too.

How simple is 'too simple to break'?

The general philosophy is this: if it can't break on its own, it's too simple to break.

First example is the getX() method. Suppose the getX() method only answers the value of an instance variable. In that case, getX() cannot break unless either the compiler or the interpreter is also broken. For that reason, don't test getX(); there is no benefit. The same is true of the setX() method, although if your setX() method does any parameter validation or has any side effects, you likely need to test it.

Next example: suppose you have written a method that does nothing but forward parameters into a method called on another object. That method is too simple to break.

    public void myMethod(final int a, final String b) {
myCollaborator.anotherMethod(a, b);
}

myMethod cannot possibly break because it does nothing: it forwards its input to another object and that's all.

The only precondition for this method is "myCollaborator != null", but that is generally the responsibility of the constructor, and not of myMethod. If you are concerned, add a test to verify that myCollaborator is always set to something non-null by every constructor.

The only way myMethod could break would be if myCollaborator.anotherMethod() were broken. In that case, test myCollaborator, and not the current class.

It is true that adding tests for even these simple methods guards against the possibility that someone refactors and makes the methods "not-so-simple" anymore. In that case, though, the refactorer needs to be aware that the method is now complex enough to break, and should write tests for it -- and preferably before the refactoring.

Another example: suppose you have a JSP and, like a good programmer, you have removed all business logic from it. All it does is provide a layout for a number of JavaBeans and never does anything that could change the value of any object. That JSP is too simple to break, and since JSPs are notoriously annoying to test, you should strive to make all your JSPs too simple to break.

Here's the way testing goes:

    becomeTimidAndTestEverything
while writingTheSameThingOverAndOverAgain
becomeMoreAggressive
writeFewerTests
writeTestsForMoreInterestingCases
if getBurnedByStupidDefect
feelStupid
becomeTimidAndTestEverything
end
end

The loop, as you can see, never terminates.


How To Verify the JUnit Plugin in Eclipse?

If you download and install the standard package of Eclipse 3.1.2, it comes with the JUnit plugin: org.junit_3.8.1. To verify this, you should find this directory under inside the plugins directory with these items:

Directory of C:\eclipse\plugins\org.junit_3.8.1 12/30/2007 03:24 PM . 12/30/2007 03:24 PM .. 01/18/2006 04:28 PM 2,520 about.html 01/18/2006 04:28 PM 121,070 junit.jar 01/18/2006 04:28 PM META-INF 01/18/2006 04:28 PM 592 plugin.properties 01/18/2006 04:28 PM 84 plugin.xml 4 File(s) 124,266 bytes


How To Create Test Class in Eclipse?

There are five ways to create a JUnit test case class in Eclipse with JUnit plugin: org.junit_3.8.1. First, select the directory (usually unittests/) that you wish to create the test case class in.

1. Select File > New > JUnit Test Case.

2. Select the arrow of the button in the upper left of the toolbar. Select JUnit Test Case.

3. Right click on a package in the Package Explorer view in the Java Perspective, and select JUnitTestCase.

4. Click on the arrow of the icon in the toolbar. Select JUnit Test Case.

5. You can create a normal Java class as shown in the Eclipse tutorial, but include junit.framework.TestCase as the super class of the test class you are creating.

How to Run a JUnit Test Case in Eclipse?

There are three ways to run JUnit Test Cases or Test Suites in Eclipse with JUnit plugin: org.junit_3.8.1.

1. You can right click on the test case class or test suite class and select Run As > JUnit Test.

2. You can select a test case or suite and click the arrow on the icon or select Run from the toolbar, and select Run As > JUnit Test.

3. You can select a test case or suite and click the arrow on the icon or select Run from the toolbar, and select Run... From here you will create a new JUnit test configuration, and name it. You can choose to run a single test case, or run all test cases in a project or folder.


Can You Describe Steps of Creating Test Case Classes in Eclipse?

There are detailed steps to create a test case class in Eclipse with JUnit plugin: org.junit_3.8.1.

1. Use the Browse button to search for a different super class. The default super class is junit.framework.TestCase.

2. Check which method stubs you would like to create. You can create a main method, setUp(), tearDown(), or a constructor(), but all of these are optional. A constructor is only run when the test case class is first instantiated, but the setUp() and tearDown() methods are run before and after, respectively, each test case is run.

3. You can browse the application that you are creating for a class that you wish to test, or this could be left blank if you will generate the class while creating while creating the test.

- If you selected a "Class Under Test" you can click the Next button, otherwise click Finish. You will be able to select which methods in the class under test that you want to write test cases for. The method signatures will be created for you. Click Finish. The new test case class will be open in the editor.

- This test class demonstrates the basic functionality of the setUp() and tearDown() methods, and gives example test cases. The testForException() method demonstrates how to test that an exception is properly thrown.

Note: All source methods in the class under test must be public or protected, not private, in order to be tested by JUnit. If the method in the class under test is private, the test class must be in the same package.

How to creating a Test Suite using JUnit in Eclipse?

There are four ways to create a JUnit test suite class in Eclipse with JUnit plugin: org.junit_3.8.1. First, select the directory (usually unittests/) that you wish to create the test suite class in.

1. Select File > New > Other... > Java > JUnit > JUnit Test Suite.

2. Select the arrow of the button in the upper left of the toolbar. Select Other... > Java > JUnit > JUnit Test Suite,

3. Right click on a package in the Package Explorer view in the Java Perspective, and select Other... > Java > JUnit > JUnit Test Suite, or

4. You can create a normal Java class as shown in the Eclipse tutorial, but include junit.framework.TestSuite as the super class of the test class you are creating.

How Do You Use Ant to Create HTML Test Reports?

1. Ensure that Ant's optional.jar file is either in your CLASSPATH or exists in your $ANT_HOME/lib directory.

2. Add an ANT property for the directory containing the HTML reports:

      

3. Define the Ant task for running JUnit and generating reports:



















4. Run the test:

      ant test-html

How Do You Test Classes That Must Be Run in a J2EE Container?

To test classes that must be run in a J2EE container (e.g. servlets, EJBs), you should refactor J2EE components to delegate functionality to other objects that don't have to be run in a J2EE container will improve the design and testability of the software.

Cactus is an open source JUnit extension that can be used to test J2EE components in their natural environment.

How Do You Test Classes That Must Be Run in a J2EE Container?

To test classes that must be run in a J2EE container (e.g. servlets, EJBs), you should refactor J2EE components to delegate functionality to other objects that don't have to be run in a J2EE container will improve the design and testability of the software.

Cactus is an open source JUnit extension that can be used to test J2EE components in their natural environment.

Can You Explain the Exception: "No runnable methods"?

You will get this exception, if you use the JUnit 4.4 core runner to execute a class that has no "@Test" method. You can try this sample class:

public class Hello {
public static void main(String[] arg) {
System.out.print("Hello world!");
}
}

Run this class with the JUnit 4.4 core runner:

java -cp .;junit-4.4.jar org.junit.runner.JUnitCore Hello

JUnit version 4.4
.E
Time: 0
There was 1 failure:
1) initializationError0(Hello)
java.lang.Exception: No runnable methods
at org.junit.internal.runners.MethodValidator.validateInstanceM
at org.junit.internal.runners.MethodValidator.validateMethodsFo
at org.junit.internal.runners.JUnit4ClassRunner.validate(JUnit4
at org.junit.internal.runners.JUnit4ClassRunner.(JUnit4Cl
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Nativ
...
at org.junit.runner.JUnitCore.run(JUnitCore.java:100)
at org.junit.runner.JUnitCore.runMain(JUnitCore.java:81)
at org.junit.runner.JUnitCore.main(JUnitCore.java:44)

FAILURES!!!
Tests run: 1, Failures: 1

Junit 4

When Do You Need to Write an @After Method?

You need to consider the following points when anwering this question:

  • If there is no "@Before" method, you don't need to write an "@After" method. There is nothing to be released.
  • If the "@Before" method only creates Java internal objects, you don't need to write an "@After" method. Java internal objects will be released automatically by the Java garbage collector.
  • If the "@Before" method creates any external resources like files or database objects, you need to write an "@After" method to release those external resources.

Here is a good example of using "@After" methods from the JUnit FAQ:

package junitfaq;

import org.junit.*;
import static org.junit.Assert.*;
import java.io.*;

public class OutputTest {

private File output;

@Before
public void createOutputFile() {
output = new File(...);
}

@After
public void deleteOutputFile() {
output.delete();
}

@Test
public void testSomethingWithFile() {
...
}

@Test
public void anotherTestWithFile() {
...
}
}

How Do You Test an Unexpected Exception with JUnit?

If you want to test a method that could raise an unexpected exception, you should design a test that:

  • Put the test code inside a "try" block".
  • Catch any unexpected exception object.
  • Fail the test witl Assert.fail().

JUnit runner will fail this test if the test code raised any unexpected exception.

Here is a good test class that test any unexpected exception raised by the get() method of the ArrayList class:

import org.junit.*; import java.util.*; // by FYICenter.com public class UnexpectedExceptionTest1 { @Test public void testGet() { ArrayList emptyList = new ArrayList(); // catch any unexpected exception try { Object o = emptyList.get(1); } catch (Exception e) { Assert.fail("Unexpected exception"); } } }

If you run this test, it will fail:

java -cp .;junit-4.4.jar org.junit.runner.JUnitCore UnexpectedExceptionTest1 JUnit version 4.4 .E Time: 0.015 There was 1 failure: 1) testGet(UnexpectedExceptionTest1) java.lang.AssertionError: Unexpected exception at org.junit.Assert.fail(Assert.java:74) at UnexpectedExceptionTest1.testGet(UnexpectedExceptionTest1.ja at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAcce at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMe at java.lang.reflect.Method.invoke(Method.java:597) at org.junit.internal.runners.TestMethod.invoke(TestMethod.java at org.junit.internal.runners.MethodRoadie.runTestMethod(Method at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.j at org.junit.internal.runners.MethodRoadie.runBeforesThenTestTh at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.jav at org.junit.internal.runners.JUnit4ClassRunner.invokeTestMetho at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUni at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4Cla at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassR at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoa at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4Class at org.junit.internal.runners.CompositeRunner.runChildren(Compo at org.junit.internal.runners.CompositeRunner.run(CompositeRunn at org.junit.runner.JUnitCore.run(JUnitCore.java:130) at org.junit.runner.JUnitCore.run(JUnitCore.java:109) at org.junit.runner.JUnitCore.run(JUnitCore.java:100) at org.junit.runner.JUnitCore.runMain(JUnitCore.java:81) at org.junit.runner.JUnitCore.main(JUnitCore.java:44) FAILURES!!! Tests run: 1, Failures: 1

What Happens If a Test Method Throws an Exception?

If you write a test method that throws an exception by itself or by the method being tested, the JUnit runner will declare that this test fails.

The example test below is designed to let the test fail by throwing the uncaught IndexOutOfBoundsException exception:

import org.junit.*; import java.util.*; // by FYICenter.com public class UnexpectedExceptionTest2 { // throw any unexpected exception @Test public void testGet() throws Exception { ArrayList emptyList = new ArrayList(); Exception anyException = null; // don't catch any exception Object o = emptyList.get(1); } }

If you run this test, it will fail:

java -cp .;junit-4.4.jar org.junit.runner.JUnitCore UnexpectedExceptionTest2 JUnit version 4.4 .E Time: 0.015 There was 1 failure: 1) testGet(UnexpectedExceptionTest2) java.lang.IndexOutOfBoundsException: Index: 1, Size: 0 at java.util.ArrayList.RangeCheck(ArrayList.java:547) at java.util.ArrayList.get(ArrayList.java:322) at UnexpectedExceptionTest2.testGet(UnexpectedExceptionTest2.ja at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAcce at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMe at java.lang.reflect.Method.invoke(Method.java:597) at org.junit.internal.runners.TestMethod.invoke(TestMethod.java at org.junit.internal.runners.MethodRoadie.runTestMethod(Method at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.j at org.junit.internal.runners.MethodRoadie.runBeforesThenTestTh at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.jav at org.junit.internal.runners.JUnit4ClassRunner.invokeTestMetho at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUni at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4Cla at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassR at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoa at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4Class at org.junit.internal.runners.CompositeRunner.runChildren(Compo at org.junit.internal.runners.CompositeRunner.run(CompositeRunn at org.junit.runner.JUnitCore.run(JUnitCore.java:130) at org.junit.runner.JUnitCore.run(JUnitCore.java:109) at org.junit.runner.JUnitCore.run(JUnitCore.java:100) at org.junit.runner.JUnitCore.runMain(JUnitCore.java:81) at org.junit.runner.JUnitCore.main(JUnitCore.java:44) FAILURES!!! Tests run: 1, Failures: 1

How Do You Test a Method That Doesn't Return Anything?

You need to follow the logic below to answer this question:

  • If a method is not returning anything through the "return" statement (void method), it may return data through its arguments. In this case, you can test the data returned in any argument.
  • Else if a method is not returning any data through its arguments, it may change values of its instance variables. In this case, you can test changes of any instance variables.
  • Else if a method is not changing any instance variable, it may change values of its class variables. In this case, you can test changes of any class variables.
  • Else if a method is not changing any class variable, it may change external resources. In this case, you can test changes of any external resources.
  • Else if a method is not changing any external resources, it may just doing nothing but holding the thread in a waiting status. In this case, you can test this waiting condition.
  • Else if a method is not holding the thread in waiting status, then this method is really doing nothing. In this case, there is no need to test this method. :-)

When Objects Are Garbage Collected After a Test Is Executed?

My guess would be that all objects used in a test will be ready for Java garbage collector to release them immediately after the test has been executed. But it seems that I was wrong. The JUnit FAQ provided the following answer.

By design, the tree of Test instances is built in one pass, then the tests are executed in a second pass. The test runner holds strong references to all Test instances for the duration of the test execution. This means that for a very long test run with many Test instances, none of the tests may be garbage collected until the end of the entire test run.

Therefore, if you allocate external or limited resources in a test, you are responsible for freeing those resources. Explicitly setting an object to null in the tearDown() method, for example, allows it to be garbage collected before the end of the entire test run.

If this is true, the JUnit runner should be improved to stop building all test instances before executing any tests. Instead, the JUnit runner should just take one test at a time, build an instance of this test, execute the test, and release the test when the execution is done.


What Is Java "assert" Statement?

"assert" statements are part of the Java assertion feature introduced in Java 1.4. Java assertion feature allows developer to put "assert" statements in Java source code to help unit testing and debugging.

An "assert" statement has the following format:

   assert boolean_expression : string_expression;

When this statement is executed:

  • If boolean_expression evaluates to true, the statement will pass normally.
  • If boolean_expression evaluates to false, the statement will fail with an "AssertionError" exception.

Here is a good example of an "assert" statement used to check an invariant:

// Day-of-week calculator
// by FYICenter.com

public class DayOfWeek {
private int days = 0;

// main method for testing purpose
public static void main(String[] arg) {
int d = Integer.parseInt(arg[0]);
DayOfWeek o = new DayOfWeek(d);
System.out.print("Day of the week: "+o.getDayOfWeek());
}

// constrcutor
public DayOfWeek(int d) {
days = d;
}

// calculate day of the week
public String getDayOfWeek() {
if (days % 7 == 0) {
return "Sunday";
} else if (days % 7 == 1) {
return "Monday";
} else if (days % 7 == 2) {
return "Tuesday";
} else if (days % 7 == 3) {
return "Wednesday";
} else if (days % 7 == 4) {
return "Thursday";
} else if (days % 7 == 5) {
return "Friday";
} else {
assert days % 7 == 6 : days;
return "Saturday";
}
}
}

How To Compile and Run Java Programs with "assert" Statements?

If you are using "assert" statements in your Java program for unit testing and debugging, you need to compile and execute the program with extra command options to use them:

  • Compilation - "javac -source 1.4 MyClass.java". This tells the compiler to accept source code containing assertions.
  • Executionn - "java -ea MyClass". This tells the JVM to not skill "assert" statements. "-ea" is the same as "-enableassertions".

Here is an example of compiling and executing the sample program, DayOfWeek.java, presented in the previous question:

javac -source 1.4 DayOfWeek.java

java -ea DayOfWeek 1
Day of the week: Monday

java -ea DayOfWeek 6
Day of the week: Saturday

java -ea DayOfWeek 14
Day of the week: Sunday

java -ea DayOfWeek -2
Exception in thread "main" java.lang.AssertionError: -2
at DayOfWeek.getDayOfWeek(DayOfWeek.java:34)
at DayOfWeek.main(DayOfWeek.java:11)

As expected, the code does not handle negative numbers correctly.

How To Test Programs Thats Use Java "assert" Statements?

Since JUnit 3.8, the JUnit runner if fully compatible with the assertion feature. If a Java "assert" statement failed in a JUnit test, the JUnit runner will report that this test fails.

When you design your tests, you should ignore the fact the target class is using "assert" statements. Ingore those "assert" statement and write tests to cover all cases where the target class could break.

Here is a good example of JUnit tests testing the sample program, DayOfWeek.java, presented in the previous question. test3() is designed to test the same area where the Java "assert" statement is located.

import org.junit.*;
// by FYICenter.com

public class DayOfWeekTest {

@Test public void test1() {
DayOfWeek d = new DayOfWeek(6);
Assert.assertTrue("Expecting Saturday",
d.getDayOfWeek().equals("Saturday"));
}

@Test public void test2() {
DayOfWeek d = new DayOfWeek(14);
Assert.assertTrue("Expecting Sunday",
d.getDayOfWeek().equals("Sunday"));
}

@Test public void test3() {
DayOfWeek d = new DayOfWeek(-2);
Assert.assertTrue("Expecting Friday",
d.getDayOfWeek().equals("Friday"));
}
}

Should You Run JUnit Tests with Java Assertion Disabled?

If your test class is well designed and covering all cases where the target class could break, you should run your test with Java assertion disabled by using the JVM "-da" option.

If you are using the test class, DayOfWeekTest.java, presented in the previous question, here is the error report when executed with the "-da" option, disabling Java assertion:

java -da -cp .;\local\junit4.4\junit-4.4.jar
org.junit.runner.JUnitCore DayOfWeekTest

JUnit version 4.4
...E
Time: 0.015
There was 1 failure:
1) test3(DayOfWeekTest)
java.lang.AssertionError: Expecting Friday
at org.junit.Assert.fail(Assert.java:74)
at org.junit.Assert.assertTrue(Assert.java:37)
at DayOfWeekTest.test3(DayOfWeekTest.java:20)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
...
FAILURES!!!
Tests run: 3, Failures: 1

test3() failed with the JUnit assertTrue() call, and caught the code problem with negative values.


What Happens If You Run JUnit Tests with Java Assertion Enabled?

As of JUnit 3.8, The JUnit runner will work nicely with Java assertion feature. If Java assertion is enabled and a Java "assert" statement fails, the JUnit runner will report this in same way as the calling test fails.

If you are using the test class, DayOfWeekTest.java, presented in the previous question, Here is the error report when executed with the "-ea" option, enabling Java assertion:

java -ea -cp
.;\local\junit4.4\junit-4.4.jar
org.junit.runner.JUnitCore DayOfWeekTest
JUnit version 4.4
...E
Time: 0
There was 1 failure:
1) test3(DayOfWeekTest)
java.lang.AssertionError: -2
at DayOfWeek.getDayOfWeek(DayOfWeek.java:34)
at DayOfWeekTest.test3(DayOfWeekTest.java:20)
at sun.reflect.NativeMethodAccessorImpl.invoke0
(Native Method)
...

FAILURES!!!
Tests run: 3, Failures: 1

test3() failed with the Java "assert" statement inside the getDayOfWeek() method, not the JUnit assertTrue() call. In other work, the code problem is caught by the Java "assert" statement, not by the JUnit test.

Why Does JUnit Only Report the First Failed Assertion in a Single Test?

Reporting multiple failures in a single test is generally a sign that the test does too much, compared to what a unit test ought to do. Usually this means either that the test is really a functional/acceptance/customer test or, if it is a unit test, then it is too big a unit test.

JUnit is designed to work best with a number of small tests. It executes each test within a separate instance of the test class. It reports failure on each test. Shared setup code is most natural when sharing between tests. This is a design decision that permeates JUnit, and when you decide to report multiple failures per test, you begin to fight against JUnit. This is not recommended.

Long tests are a design smell and indicate the likelihood of a design problem. Kent Beck is fond of saying in this case that "there is an opportunity to learn something about your design." We would like to see a pattern language develop around these problems, but it has not yet been written down.

Finally, note that a single test with multiple assertions is isomorphic to a test case with multiple tests:

One test method, three assertions:

    public class MyTestCase {
@Test
public void testSomething() {
// Set up for the test, manipulating local variables
assertTrue(condition1);
assertTrue(condition2);
assertTrue(condition3);
}
}

Three test methods, one assertion each:

    public class MyTestCase {
// Local variables become instance variables

@Before
public void setUp() {
// Set up for the test, manipulating instance variables
}

@Test
public void testCondition1() {
assertTrue(condition1);
}

@Test
public void testCondition2() {
assertTrue(condition2);
}

@Test
public void testCondition3() {
assertTrue(condition3);
}
}

The resulting tests use JUnit's natural execution and reporting mechanism and, failure in one test does not affect the execution of the other tests. You generally want exactly one test to fail for any given bug, if you can manage it.


Junit 3

Do You Need to Write a Test Class for Every Class That Need to Be Tested?

This is a simple question. But the answer shows your organization skills.

The technical answer is no. There is no need to write one test class for each every class that need to be tested. One test class can contain many tests for many test target classes.

But the practical answer is yes. You should design one test class per test target class for low level basic tests. This makes your test classes much easier to manage and maintain.

You should write separate test classes for high level tests that requires multiple target classes working together.


What Is JUnit TestCase?

JUnit TestCase is the base class, junit.framework.TestCase, used in JUnit 3.8 that allows you to create a test case. TestCase class is no longer supported in JUnit 4.4.

A test case defines the fixture to run multiple tests. To define a test case

  • Implement a subclass of TestCase
  • Define instance variables that store the state of the fixture
  • Initialize the fixture state by overriding setUp
  • Clean-up after a test by overriding tearDown

Each test runs in its own fixture so there can be no side effects among test runs. Here is an example:

import junit.framework.*;
// by FYICenter.com
public class MathTest extends TestCase {
protected double fValue1;
protected double fValue2;

protected void setUp() {
fValue1= 2.0;
fValue2= 3.0;
}

public void testAdd() {
double result= fValue1 + fValue2;
assertTrue(result == 5.0);
}

What Is JUnit TestSuite?

JUnit TestSuite is a container class, junit.framework.TestSuite, used in JUnit 3.8 that allows you to group multiple test cases into a collection and run them together. TestSuite class is no longer supported in JUnit 4.4.

Each test runs in its own fixture so there can be no side effects among test runs. Here is an example:

import junit.framework.*; // by FYICenter.com public class RunTestSuite { public static void main(String[] a) { TestSuite suite = new TestSuite(MathTest.class); TestResult result = new TestResult(); suite.run(result); System.out.println("Was it successful? " +result.wasSuccessful()); System.out.println("How many tests were there? " +result.runCount()); } }

How To Write a Single Class to Define Multiple Tests and Run Them?

Here is the answer for JUnit 3.8:

   1. Define a subclass of junit.framework.TestCase.
2. Override the setUp() and tearDown() methods.
3. Define multiple "public void testXXX()" methods.
One for each test.
A test method name must be started with "test".
Call the methods of tested object.
Check the expected results with an assertXXX() method.
4. Define a "public static void main()" method to run tests.
Call "junit.textui.TestRunner.run()" to run all tests.

Here is a nice example:

import junit.framework.*;
import junit.textui.*;
// by FYICenter.com
public class MathTestAndRun extends TestCase {
protected double fValue1;
protected double fValue2;

public static void main(String[] a){
junit.textui.TestRunner.run(MathTestAndRun.class);
}

protected void setUp() {
fValue1= 2.0;
fValue2= 3.0;
}

public void testAdd() {
double result= fValue1 + fValue2;
assertTrue(result == 5.0);
}

public void testMultiply() {
double result= fValue1 * fValue2;
assertTrue(result == 6.0);
}
}
}

How to Run Your JUnit 4.4 Tests with a JUnit 3.8 Runner?

I am not sure why you have to do this. But if you want to, you can use the junit.framework.JUnit4TestAdapter class included in JUnit 4.4 JAR file. Here is sample code:

import junit.framework.Test;
import junit.textui.TestRunner;
import junit.framework.JUnit4TestAdapter;
// by FYICenter.com

public class JUnit3Adapter {
public static void main (String[] args) {
Test adaptedTest = new JUnit4TestAdapter(HelloTest.class);
TestRunner.run(adaptedTest);
}
}

Classes junit.framework.Test, junit.textui.TestRunner and junit.framework.JUnit4TestAdapter are included in the JUnit 4.4 JAR file. You don't need to include the JUnit 3.8 JAR file in your CLASSPATH.

How Do You Test a "private" Method?

When a method is declared as "private", it can only be accessed within the same class. So there is no way to test a "private" method of a target class from any test class.

To resolve this problem, you have to perform unit testing manually. Or you have to change your method from "private" to "protected".


Can You Write a JUnit Test Case Class in 10 Minutes?

Here is a good JUnit test case class example that you should be able to write within 10 minutes. This example is provided by Varun Chopra and valid for JUnit 3.8.

Assuming that Java class DirLister.java has following three methods and one variable:

  • String dirPath - Member variable that represents directory in use
  • createLogFile(String fileName) - Member method that creates a file in dirPath
  • exists(String fileName) - Member method that checks the existence of a file within dirPath
  • getChildList() - Member method that gets the list of files and directories within dirPath

We want to test above three methods with Junit. For that, we will create another Java class that will have the test code for these three methods. See the class below:

/*
* DirListerTest.java
* JUnit based test
*
* Created on May 26, 2005, 11:33 AM
*/
package javaegs.junit;

import java.io.File;
import java.io.IOException;
import junit.framework.*;
/**
* @author varunc
*/

public class DirListerTest extends TestCase {

DirLister dl;

/**
* Test of createLogFile method, of class
javaegs.junit.DirLister.
*/

public void testCreateLogFile() {
dl = new DirLister("D:/temp/junittestdir");
dl.createLogFile("logFile.log");
assertTrue("File does not exist",dl.exists("logFile.log"));
}

/**
* Test of exists method, of class
javaegs.junit.DirLister.
*/

public void testExists() {
dl = new DirLister("D:/temp/junittestdir");
assertTrue("File does not exist",dl.exists("logFile.log"));
}

/**
* Test of getChildList method, of class
javaegs.junit.DirLister.
*/

public void testGetChildList() {
dl = new DirLister("D:/temp/junittestdir");
String[] files = null;
try {
files = dl.getChildList();
} catch(Exception ex) {
fail("Exception occured"+ex);
ex.printStackTrace();
}
assertNotNull("Children can't be null",files);
assertTrue("No. of files can't be 0",files.length>0);
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
junit.textui.TestRunner.run(DirListerTest.class);
}
}

Can You Explain a Sample JUnit Test Case Class?

Assuming that you have been give the sample JUnit test case class listed in the previous question, you should be able to provide the following notes:

DirListerTest class imports classes in junit.framework package. junit.framework package contains all common classes required to create junit test cases.

DirListerTest class extends junit.framework.TestCase class. TestCase class further extends junit.framework.Assert class which contains various assertXXX() methods required to created test cases. Therefore to use Junit framework, test class must extend TestCase class.

Corresponding to each method to be tested there is a method in the test class namely: testCreateLogFile(), testExists(), and testGetChildList(). You can name them anything but the starting word must be test.

One important point I would like to mention here is that I created three different methods to demonstrate three different and common scenarios while programming.

  • exists() method of DirLister returns a value. Our test case will demonstrate how to test such methods, which return a value.
  • createLogFile() returns nothing. It even catches the exception and ignores it (in one sense). Therefore, we won?t get the exception even if that method fails. Our test case will demonstrate how to go about such methods.
  • getChildList() returns a value as well as may throw exception. We will see how to handle this using Junit framework.

Let's now look inside the second test case method testExists():

public void testExists() {
dl = new DirLister("D:/temp/junittestdir");
assertTrue("File does not exist",dl.exists("logFile.log"));
}

First statement in it instantiates the DirLister object and points it to D:/temp/junittestdir directory. Next statement is what we need to focus on:

  assertTrue("File does not exist",dl.exists("logFile.log"));

assertTrue() is a standard method provided by Junit framework (there are many other assert methods also). It takes two arguments: a string message and a boolean condition.

If boolean condition is true, test passes and string message is ignored. Otherwise test fails and string message is displayed. In this case, if dl.exists("logFile.log") passes, test passes, otherwise test fails.

There is another variant of assertTrue() method provided by Junit that takes only one argument - the condition to check. This is true for all assertXXX methods provided by Junit.

Now you may say, that was a boolean result. What if a non-boolean value is returned? Okay, so Junit provides alternatives. We can use assertEquals() as below:

  assertEquals("not equal",dl.exists("logFile.log"), true);

Here first argument is the message that is displayed in case test fails. Second argument is the actual value. Normally we will call the method to be tested here and whatever value that method will return, will become the actual value. Third argument is the value we expect. So here, we expect it to be ?true?, hence we put true there.

There are various overloaded forms of assertEquals() method that can handle every result (from byte to String to Object).

Let's now look inside the first test case method testCreateLogFile(). If a method returns nothing and throws no exception, either it is not doing anything or is creating a side effect. createLogFile() is one such method. To test such methods, we will have to test the side effect they created. That's what testCreateLogFile() does.

public void testCreateLogFile() {
dl = new DirLister("D:/temp/junittestdir");
dl.createLogFile("logFile.log");
assertTrue("File does not exist",dl.exists("logFile.log"));
}

We expect that dl.createLogFile("logFile.log") should create logFile.log in directory in use. Therefore, in next statement we check the existence of such a file.

 assertTrue("File does not exist",dl.exists("logFile.log"));

If file will be there, test case will pass, otherwise it will fail.

Let's now look inside the other test case method testGetChildList(). getChildList() method may throw an exception. Therefore, we will have to call it in try block.

 public void testGetChildList() {
dl = new DirLister("D:/temp/junittestdir");
String[] files = null;
try {
files = dl.getChildList();
} catch(Exception ex) {
fail("Exception occured"+ex);
ex.printStackTrace();
}
assertNotNull("Children can't be null",files);
assertTrue("No. of files can't be 0",files.length>0);
}

If we don't expect getChildList() to throw exception for the directory value we provide, we can put in catch block a call to fail() method (as we have done).

fail() is again a standard method provided by Junit framework (belongs to junit.framework.Assert class). It fails a test case, which means it will show in the output that test case was failed and show the message we provided as argument to it. It has a without-argument variant also.

One more thing fail() will do is to exit the method. So assert statements following catch block will not be executed if fail() is called. That's logical since you should handle one bug at a time in unit testing.

Okay, so what if getChildList() succeeds. If we know that directory we have provided has at least one file in it, then:

    assertNotNull("Children can't be null",files);

will be useful. It will check that "files" variable is not null. If this test fails, it will display the message provided by us.

    assertTrue("No. of files can't be 0",files.length>0);

will further make it certain that number of files is greater than 0.

If we know the number of files within the directory we can do something like this:

  assertEquals(?Not equal?, files.length, n);

where n is the number of files.


Do You Need to Write a main() Method in a JUnit Test Case Class?

The right answer to this question is "No". But many developers do write a main() method in a JUnit test case class to call a JUnit test runner to run all tests defined in this class. This is not recommended, because you can always call a JUnit runner to run a test case class as a system command.

If you want to know how to call a JUnit runner in a main() method, you can this code included in the sample test case class listed in the previous question. This code is provided by Varun Chopra and valid for JUnit 3.8.

public static void main(String[] args) {
junit.textui.TestRunner.run(DirListerTest.class);
}

junit.textui.TestRunner.run() method takes test class name as argument. Using reflection, this method finds all class methods whose name starts with test. So it will find following 3 methods:

  testCreateLogFile()
testExists()
testGetChildList()

It will execute each of the 3 methods in unpredictable sequence (hence test case methods should be independent of each other) and give the result in console. Result will be something like this:

Time: 0.016
OK (3 tests)

In case you want to see the output in a GUI, you just need to replace statement in main method with following:

    junit.swingui.TestRunner.run(DirListerTest.class);

This will open up a nice swing based UI, which will show a green progress bar to show the status.


Can You Explain the Life Cycle of a JUnit 4.4 Test Class?

A JUnit 4.4 test class contains a @Before method, an @After method and multiple @test methods. When calling a test runner to run this test class, the runner will execute those methods in a specific order giving the test class an execution life cycle like this:

@Before
@Test XXX1
@After

@Before
@Test XXX2
@After

@Before
@Test XXX3
@After

What Is a JUnit Test Fixture?

A test fixture is a fixed state of a set of objects used as a baseline for running tests. The purpose of a test fixture is to ensure that there is a well known and fixed environment in which tests are run so that results are repeatable. Examples of fixtures:

  • Loading a database with a specific, known set of data
  • Copying a specific known set of files
  • Preparation of input data and setup/creation of fake or mock objects

In other word, creating a test fixture is to create a set of objects initialized to certain states.

If a group of tests requires diferent test fixtures, you can write code inside the test method to create its own test fixture.

If a group of tests shares the same fixtures, you should write a separate setup code to create the common test fixture.





How Do You Test an Expected Exception with JUnit?

If you want to test a method that will throw an exception under a specific condition, you can:

  • Put the test code inside a "try" block".
  • Catch the expected exception object.
  • Assert the exception object with Assert.assertNotNull().

JUnit runner will fail this test if the test code did not raise the expected exception.

Here is a good test class that test the expected IndexOutOfBoundsException exception raised by the get() method of the ArrayList class:

import org.junit.*;
import java.util.*;

// by FYICenter.com
public class ExpectedExceptionTest1 {
@Test public void outOfBounds() {
ArrayList emptyList = new ArrayList(); Exception eOutOfBounds = null; // catch the expected exception try { Object o = emptyList.get(1); } catch (IndexOutOfBoundsException e) { eOutOfBounds = e; } // asset the exception object Assert.assertNotNull("No expected exception", eOutOfBounds); } }

But there a better way to test the expected exception provided by the JUnit @Test annotation. See the next question.


Junit 2

What Happens If a JUnit Test Method Is Declared as "private"?

If a JUnit test method is declared as "private", the compilation will pass ok. But the execution will fail. This is decause JUnit requires that all test methods must be declared as "public". For example:

type HelloTestPrivate.java

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

javac -cp junit-4.4.jar HelloTestPrivate.java

java -cp .;junit-4.4.jar org.junit.runner.JUnitCore
HelloTestPrivate
JUnit version 4.4
.E
Time: 0
There was 1 failure:
1) initializationError0(HelloTestPrivate)
java.lang.Exception: Method testHello should be public
at org.junit.internal.runners.MethodValidator.validateTestMethod
at org.junit.internal.runners.MethodValidator.validateInstanceMe
at org.junit.internal.runners.MethodValidator.validateMethodsFor
at org.junit.internal.runners.JUnit4ClassRunner.validate(JUnit4C
at org.junit.internal.runners.JUnit4ClassRunner.(JUn
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeC
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Del
at java.lang.reflect.Constructor.newInstance(Constructor.java:51
at org.junit.internal.requests.ClassRequest.buildRunner(ClassReq
at org.junit.internal.requests.ClassRequest.getRunner(ClassReque
at org.junit.internal.requests.ClassesRequest.getRunner(ClassesR
at org.junit.runner.JUnitCore.run(JUnitCore.java:109)
at org.junit.runner.JUnitCore.run(JUnitCore.java:100)
at org.junit.runner.JUnitCore.runMain(JUnitCore.java:81)
at org.junit.runner.JUnitCore.main(JUnitCore.java:44)

FAILURES!!!
Tests run: 1, Failures: 1

What Happens If a JUnit Test Method Is Declared to Return "String"?

If a JUnit test method is declared to return "String", the compilation will pass ok. But the execution will fail. This is decause JUnit requires that all test methods must be declared to return "void". For example:

type HelloTestNonVoid.java import org.junit.Test; import static org.junit.Assert.*; // by FYICenter.com public class HelloTestNonVoid { @Test public String testHello() { String message = "Hello World!"; assertEquals(12, message.length()); return message; } } javac -cp junit-4.4.jar HelloTestNonVoid.java java -cp .;junit-4.4.jar org.junit.runner.JUnitCore HelloTestNonVoid JUnit version 4.4 .E Time: 0 There was 1 failure: 1) initializationError0(HelloTestNonVoid) java.lang.Exception: Method testHello should be void at org.junit.internal.runners.MethodValidator.validateTe at org.junit.internal.runners.MethodValidator.validateIn at org.junit.internal.runners.MethodValidator.validateMe at org.junit.internal.runners.JUnit4ClassRunner.validate at org.junit.internal.runners.JUnit4ClassRunner.(J at sun.reflect.NativeConstructorAccessorImpl.newInstance at sun.reflect.NativeConstructorAccessorImpl.newInstance at sun.reflect.DelegatingConstructorAccessorImpl.newInst at java.lang.reflect.Constructor.newInstance(Constructor at org.junit.internal.requests.ClassRequest.buildRunner( at org.junit.internal.requests.ClassRequest.getRunner(Cl at org.junit.internal.requests.ClassesRequest.getRunner( at org.junit.runner.JUnitCore.run(JUnitCore.java:109) at org.junit.runner.JUnitCore.run(JUnitCore.java:100) at org.junit.runner.JUnitCore.runMain(JUnitCore.java:81) at org.junit.runner.JUnitCore.main(JUnitCore.java:44) FAILURES!!! Tests run: 1, Failures: 1

Why Does Poeple Import org.junit.Assert Statically?

Poeple use the static import statement on org.junit.Assert to save coding time on calling its assetion methods. With a normal import statement, assertion method names must qualified with the class name like this:

import org.junit.Assert; ... Assert.assertEquals(12, message.length());

With a static import statement, assertion method names can be used directly like this:

import static org.junit.Assert.*; ... assertEquals(12, message.length());

How To Group Multiple Test Classes into a Suite in JUnit 4.4?

JUnit 4.4 stops using the "public static Test suite()" method to build a test suite class. It is now provides the org.junit.runners.Suite class and two annotations to help you to build test suite.

org.junit.runners.Suite - JUnit 4.4 runner class that runs a group of test classes.

org.junit.runner.RunWith - JUnit 4.4 class annotation that specify runner class to run the annotated class.

org.junit.runner.Suite.SuiteClasses - JUnit 4.4 class annotation that specify an array of test classes for the Suite.class to run.

The annotated class should be an empty class.

To run "@Suite.SuiteClasses" class, you can use the core runner: org.junit.runner.JUnitCore.

Here is a good example of "@Suite.SuiteClasses" class:

import org.junit.runner.RunWith; import org.junit.runners.Suite; import org.junit.runners.Suite.SuiteClasses; // by FYICenter.com // specify a runner class: Suite.class @RunWith(Suite.class) // specify an array of test classes @Suite.SuiteClasses({ HelloTest.class, ExpectedExceptionTest1.class, ExpectedExceptionTest2.class, UnexpectedExceptionTest1.class, UnexpectedExceptionTest2.class} ) // the actual class is empty public class AllTests { }

How To Run a "@Suite.SuiteClasses" Class in JUnit 4.4?

If you define create "@Suite.SuiteClasses" class as described in the previous question, you run it with the core runner: org.junit.runner.JUnitCore:

java -cp .;junit-4.4.jar org.junit.runner.JUnitCore AllTests JUnit version 4.4 ....E.E Time: 0.016 There were 2 failures: 1) testGet(UnexpectedExceptionTest1) java.lang.AssertionError: Unexpected exception at org.junit.Assert.fail(Assert.java:74) at UnexpectedExceptionTest1.testGet(UnexpectedExceptionTest1.java:13) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ... at org.junit.runner.JUnitCore.run(JUnitCore.java:100) at org.junit.runner.JUnitCore.runMain(JUnitCore.java:81) at org.junit.runner.JUnitCore.main(JUnitCore.java:44) 2) testGet(UnexpectedExceptionTest2) java.lang.IndexOutOfBoundsException: Index: 1, Size: 0 at java.util.ArrayList.RangeCheck(ArrayList.java:547) at java.util.ArrayList.get(ArrayList.java:322) at UnexpectedExceptionTest2.testGet(UnexpectedExceptionTest2.java:13) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ... at org.junit.runner.JUnitCore.run(JUnitCore.java:100) at org.junit.runner.JUnitCore.runMain(JUnitCore.java:81) at org.junit.runner.JUnitCore.main(JUnitCore.java:44) FAILURES!!! Tests run: 5, Failures: 2

What Is the "@SuiteClasses" Annotation?

"@SuiteClasses" is a class annotation defined in JUnit 4.4 in org.junit.runners.Suite.SuiteClasses. It allows you to define a suite class as described in the previous question.

By the way, the API document of JUnit 4.4 has a major typo for the org.junit.runners.Suite class (Suite.html).

Using Suite as a runner allows you to manually build a suite containing tests from many classes. It is the JUnit 4 equivalent of the JUnit 3.8.x static Test suite() method. To use it, annotate a class with @RunWith(Suite.class) and @SuiteClasses(TestClass1.class, ...). When you run this class, it will run all the tests in all the suite classes.

"@SuiteClasses(TestClass1.class, ...)" should be changed to "@Suite.SuiteClasses({TestClass1.class, ...})".

Someone provided wrong information on build test suite in JUnit 4.4. Do not follow this:

JUnit provides tools to define the suite to be run and to display its results. To run tests and see the results on the console, run:

org.junit.runner.TextListener.run(TestClass1.class, ...);


Why Not Just Write a main() Method for Unit Testing?

It is possible to write a main() method in each class that need to be tested for unit testing. In the main() method, you could create test object of the class itself, and write some tests to test its methods.

However, this is not a recommended approach because of the following points:

  • Your classes will be cluttered with test code in main method. All those test codes will be packaged into the final product.
  • If you have a lots of classes to test, you need to run the main() method of every class. This requires some extra coding effort.
  • If you want the test results to be displayed in a GUI, you will have to write code for that GUI.
  • If you want to log the results of tests in HTML format or text format, you will have to write additional code.
  • If one method call fails, next method calls won?t be executed. You will have to work-around this.
  • If you start working on a project created by some other team in your organization, you may see an entirely different approach for testing. That will increase your learning time and things won?t be standard.

Above are some of the problems, which will be taken care of automatically if you use Junit. Junit provides a standard framework for writing your tests. It separates test code from project code, hence keeps everything clean.


Why Not Just Use System.out.println() for Unit Testing?

Inserting debug statements into code is a low-tech method for debugging it. It usually requires that output be scanned manually every time the program is run to ensure that the code is doing what's expected.

It generally takes less time in the long run to codify expectations in the form of an automated JUnit test that retains its value over time. If it's difficult to write a test to assert expectations, the tests may be telling you that shorter and more cohesive methods would improve your design.

Under What Conditions Should You Test set() and get() Methods?

This is a good question for a job interview. It shows your experience with test design and data types.

Tests should be designed to target areas that might break. set() and get() methods on simple data types are unlikely to break. So no need to test them.

set() and get() methods on complex data types are likely to break. So you should test them.

Under What Conditions Should You Not Test Get() and Set() Methods?

The JUnit FAQ provides a good answer to this question:

Most of the time, get/set methods just can't break, and if they can't break, then why test them? While it is usually better to test more, there is a definite curve of diminishing returns on test effort versus "code coverage". Remember the maxim: "Test until fear turns to boredom."

Assume that the getX() method only does "return x;" and that the setX() method only does "this.x = x;". If you write this test:

    @Test
public void testGetSetX() {
setX(23);
assertEquals(23, getX());
}

then you are testing the equivalent of the following:

    @Test
public void testGetSetX() {
x = 23;
assertEquals(23, x);
}

or, if you prefer,

    @Test
public void testGetSetX() {
assertEquals(23, 23);
}

At this point, you are testing the Java compiler, or possibly the interpreter, and not your component or application. There is generally no need for you to do Java's testing for them.

If you are concerned about whether a property has already been set at the point you wish to call getX(), then you want to test the constructor, and not the getX() method. This kind of test is especially useful if you have multiple constructors:

    @Test

public void testCreate() {
assertEquals(23, new MyClass(23).getX());
}

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

Wednesday, June 25, 2008

Singleton Design Pattern

Simply Singleton

Navigate the deceptively simple Singleton pattern

Sometimes it's appropriate to have exactly one instance of a class: window managers, print spoolers, and filesystems are prototypical examples. Typically, those types of objects—known as singletons—are accessed by disparate objects throughout a software system, and therefore require a global point of access. Of course, just when you're certain you will never need more than one instance, it's a good bet you'll change your mind.

The Singleton design pattern addresses all of the previous paragraph's concerns. With the Singleton design pattern you can:

  • Ensure that only one instance of a class is created
  • Provide a global point of access to the object
  • Allow multiple instances in the future without affecting a singleton class's clients

Although the Singleton design pattern—as evidenced below by the figure below—is one of the simplest design patterns, it presents a number of pitfalls for the unwary Java developer. This article discusses the Singleton design pattern and addresses those pitfalls.

Note: You can download this article's source code from Resources.

The Singleton pattern

In Design Patterns, the authors describe the Singleton pattern like this:

Ensure a class has only one instance, and provide a global point of access to it.


The figure below illustrates the Singleton design pattern class diagram.

Singleton class diagram

As you can see from the figure above, there's not a whole lot to the Singleton design pattern. Singletons maintain a static reference to the sole singleton instance and return a reference to that instance from a static instance() method.

Example 1 shows a classic Singleton design pattern implementation:

Example 1. The classic singleton

public class ClassicSingleton {
private static ClassicSingleton instance = null;
protected ClassicSingleton() {
// Exists only to defeat instantiation.
}
public static ClassicSingleton getInstance() {
if(instance == null) {
instance = new ClassicSingleton();
}
return instance;
}
}


The singleton implemented in Example 1 is easy to understand. The ClassicSingleton class maintains a static reference to the lone singleton instance and returns that reference from the static getInstance() method.

There are several interesting points concerning the ClassicSingleton class. First, ClassicSingleton employs a technique known as lazy instantiation to create the singleton; as a result, the singleton instance is not created until the getInstance() method is called for the first time. This technique ensures that singleton instances are created only when needed.

Second, notice that ClassicSingleton implements a protected constructor so clients cannot instantiate ClassicSingleton instances; however, you may be surprised to discover that the following code is perfectly legal:

public class SingletonInstantiator {
public SingletonInstantiator() {
ClassicSingleton instance = ClassicSingleton.getInstance();
ClassicSingleton anotherInstance =
new ClassicSingleton();
...
}
}


How can the class in the preceding code fragment—which does not extend ClassicSingleton—create a ClassicSingleton instance if the ClassicSingleton constructor is protected? The answer is that protected constructors can be called by subclasses and by other classes in the same package. Because ClassicSingleton and SingletonInstantiator are in the same package (the default package), SingletonInstantiator() methods can create ClassicSingleton instances. This dilemma has two solutions: You can make the ClassicSingleton constructor private so that only ClassicSingleton() methods call it; however, that means ClassicSingleton cannot be subclassed. Sometimes, that is a desirable solution; if so, it's a good idea to declare your singleton class final, which makes that intention explicit and allows the compiler to apply performance optimizations. The other solution is to put your singleton class in an explicit package, so classes in other packages (including the default package) cannot instantiate singleton instances.

A third interesting point about ClassicSingleton: it's possible to have multiple singleton instances if classes loaded by different classloaders access a singleton. That scenario is not so far-fetched; for example, some servlet containers use distinct classloaders for each servlet, so if two servlets access a singleton, they will each have their own instance.

Fourth, if ClassicSingleton implements the java.io.Serializable interface, the class's instances can be serialized and deserialized. However, if you serialize a singleton object and subsequently deserialize that object more than once, you will have multiple singleton instances.

Finally, and perhaps most important, Example 1's ClassicSingleton class is not thread-safe. If two threads—we'll call them Thread 1 and Thread 2—call ClassicSingleton.getInstance() at the same time, two ClassicSingleton instances can be created if Thread 1 is preempted just after it enters the if block and control is subsequently given to Thread 2.

As you can see from the preceding discussion, although the Singleton pattern is one of the simplest design patterns, implementing it in Java is anything but simple. The rest of this article addresses Java-specific considerations for the Singleton pattern, but first let's take a short detour to see how you can test your singleton classes.

links

Deploying WebLogic Server Applications
http://e-docs.bea.com/wls/docs81/deployment/scenarios.html#1014366

Understanding Domain Configuration

http://edocs.bea.com/wls/docs92/domain_config/understand_domains.html

Using WebLogic Server Clusters
http://edocs.bea.com/wls/docs92/cluster/overview.html


Troubleshooting Deployment Issues
https://support.bea.com/application_content/product_portlets/support_patterns/wls/TroubleshootingDeploymentIssuesPattern.html#Types_of_deployable_applications_and

Deploying WebLogic Server Applications

http://e-docs.bea.com/wls/docs81/deployment/tools.html#999152

SQL Interview Questions with Answers

http://www.geekinterview.com/articles/sql-interview-questions-with-answers.html

Deploying WebLogic Server Applications

http://e-docs.bea.com/wls/docs81/deployment/tools.html

Tuesday, June 24, 2008

Running multiple Tomcat instances on one server

Here's a brief step by step guide to running more than one instance of Tomcat on a single machine.

Step 1: Install the Tomcat files

Download Tomcat 4.1 or 5.5, and unzip it into an appropriate directory. I usually put it in /usr/local, so it ends up in a directory called /usr/local/apache-tomcat-5.5.17 (5.5.17 being the current version as of this writing), and make a symlink named /usr/local/tomcat to that directory. When later versions come out, I can unzip them and relink, leaving the older version in case things don't work out (which rarely if ever happens, but I'm paranoid).

Step 2: Make directories for each instance

For each instance of Tomcat you're going to run, you'll need a directory that will be CATALINA_HOME. For example, you might make them /var/tomcat/serverA and /var/tomcat/serverB.

In each of these directories you need the following subdirectories: conf, logs, temp, webapps, and work.

Put a server.xml and web.xml file in the conf directory. You can get these from the conf directory of the directory where you put the tomcat installation files, although of course you should tighten up your server.xml a bit.

The webapps directory is where you'll put the web applications you want to run on the particular instance of Tomcat.

I like to have the Tomcat manager webapp installed on each instance, so I can play with the webapps, and see how many active sessions there are. See my instructions for configuring the Tomcat manager webapp.

Step 3: Configure the ports and/or addresses for each instance

Tomcat listens to at least two network ports, one for the shutdown command, and one or more for accepting requests. Two instances of Tomcat can't listen to the same port number on the same IP address, so you will need to edit your server.xml files to change the ports they listen to.

The first port to look at is the shutdown port. This is used by the command line shutdown script (actually, but the Java code it runs) to tell the Tomcat instance to shut itself down. This port is defined at the top of the server.xml file for the instance.




Monday, June 23, 2008

Weblogic Interview questions -5

How do I provide user credentials for starting a server?

When you create a domain, the Configuration Wizard prompts you to provide the username and password for an initial administrative user. If you create the domain in development mode, the wizard saves the username and encrypted password in a boot identity file. A WebLogic Server instance can refer to a boot identity file during its startup process. If a server instance does not find such a file, it prompts you to enter credentials.

If you create a domain in production mode, or if you want to change user credentials in an existing boot identity file, you can create a new boot identity file.

Can I start a Managed Server if the Administration Server is unavailable?

By default, if a Managed Server is unable to connect to the specified Administration Server during startup, it can retrieve its configuration by reading a configuration file and other files directly. You cannot change the server's configuration until the Administration Server is available. A Managed Server that starts in this way is running in Managed Server Independence mode.

What is the function of T3 in WebLogic Server?
T3 provides a framework for WebLogic Server messages that support for enhancements. These enhancements include abbreviations and features, such as object replacement, that work in the context of WebLogic Server clusters and HTTP and other product tunneling. T3 predates Java Object Serialization and RMI, while closely tracking and leveraging these specifications. T3 is a superset of Java Object. Serialization or RMI; anything you can do in Java Object Serialization and RMI can be done over T3. T3 is mandated between WebLogic Servers and between programmatic clients and a WebLogic Server cluster. HTTP and IIOP are optional protocols that can be used to communicate between other processes and WebLogic Server. It depends on what you want to do. For example, when you want to communicate between a browser and WebLogic Server-use HTTP, or an ORB and WebLogic Server-IIOP.

How do you set the classpath?

WebLogic Server installs the following script that you can use to set the classpath that a server requires:
WL_HOME\server\bin\setWLSEnv.cmd (on Windows)
WL_HOME/server/bin/setWLSEnv.sh (on UNIX)

where WL_HOME is the directory in which you installed WebLogic Server.

How do stubs work in a WebLogic Server cluster?
Clients that connect to a WebLogic Server cluster and look up a clustered object obtain a replica-aware stub for the object. This stub contains the list of available server instances that host implementations of the object. The stub also contains the load balancing logic for distributing the load among its host servers.

What happens when a failure occurs and the stub cannot connect to a WebLogic Server instance?
When the failure occurs, the stub removes the failed server instance from its list. If there are no servers left in its list, the stubb uses DNS again to find a running server and obtain a current list of running instances. Also, the stub periodically refreshes its list of available server instances in the cluster; this allows the stub to take advantage of new servers as they are added to the cluster.

How does a server know when another server is unavailable?

WebLogic Server uses two mechanisms to determine if a given server instance is unavailable.

Each WebLogic Server instance in a cluster uses multicast to broadcast regular "heartbeat" messages that advertise its availability. By monitoring heartbeat messages, server instances in a cluster determine when a server instance has failed. The other server instances will drop a server instance from the cluster, if they do not receive three consecutive heartbeats from that server instance

WebLogic Server also monitors socket errors to determine the availability of a server instance. For example, if server instance A has an open socket to server instance B, and the socket unexpectedly closes, server A assumes that server B is offline.

How are notifications made when a server is added to a cluster?
The WebLogic Server cluster broadcasts the availability of a new server instance each time a new instance joins the cluster. Cluster-aware stubs also periodically update their list of available server instances.

How do clients handle DNS requests to failed servers?

If a server fails and DNS continues to send requests to the unavailable machine, this can waste bandwidth. For a Java client application, this problem occurs only during startup. WebLogic Server caches the DNS entries and removes the unavailable ones, to prevent the client from accessing a failed server twice.

Failed servers can be more of a problem for browser-based clients, because they always use DNS. To avoid unnecessary DNS requests with browser-based clients, use a third-party load-balancer such as Resonate, BigIP, Alteon, and LocalDirector. These products mask multiple DNS addresses as a single address. They also provide more sophisticated load-balancing options than round-robin, and they keep track of failed servers to avoid routing unnecessary requests.

How many WebLogic Servers can I have on a multi-cpu machine?

There are many possible configurations and each has its own advantages and disadvantages. BEA WebLogic Server has no built-in limit for the number of server instances that can reside in a cluster. Large, multi-processor servers such as Sun Microsystems, Inc. Sun Enterprise 10000, therefore, can host very large clusters or multiple clusters.

In most cases, WebLogic Server clusters scale best when deployed with one WebLogic Server instance for every two CPUs. However, as with all capacity planning, you should test the actual deployment with your target web applications to determine the optimal number and distribution of server instances.

How can I set deployment order for applications?
WebLogic Server allows you to select the load order for applications. WebLogic Server deploys server-level resources (first JDBC and then JMS) before deploying applications. Applications are deployed in this order: connectors, then EJBs, then Web Applications. If the application is an EAR, the individual components are loaded in the order in which they are declared in the application.xml deployment descriptor.

Can I refresh static components of a deployed application without having to redeploy the entire application?

Yes. You can use weblogic.Deployer to specify a component and target a server, using the following syntax:


java weblogic.Deployer -adminurl http://admin:7001 -name appname -targets server1,server2 -deploy jsps/*.jsp

When should I use the -nostage option?
Set the staging mode to -nostage (using weblogic.Deployer or the Administration Console) if you don't want to copy deployment files but want to deploy an application from its present location. All target servers must be able to access the same set of deployment files.

When should I use the external_stage option?
Set -external_stage using weblogic.Deployer if you want to stage the application yourself, and prefer to copy it to its target by your own means.

Can I set the deployment order for application modules? For standalone modules?

The Load Order attribute controls the deployment order of standalone modules and applications relative to other modules and applications of the same type. For example, standalone EJBs with smaller Load Order values are deployed before those with higher values.

Modules that are deployed as part of an Enterprise Application (EAR file or directory) are deployed in the order in which they are specified in the application.xml deployment descriptor.

What is the difference between the WL_HOME/config/examples/applications folder and the WL_HOME/config/examples/stage folder?
The applications folder is intended for applications that are not yet ready for a production environment. WebLogic Server dynamically deploys the contents of the applications folder. The stage folder (or a folder that you create for the same purpose) is for storing copies of deployment files that are ready for deployment in a production environment (deployments that use the stage or external_stage deployment modes).

How do I turn the auto-deployment feature off?

The auto-deployment feature checks the applications folder every three seconds to determine whether there are any new applications or any changes to existing applications and then dynamically deploys these changes.

The auto-deployment feature is enabled for servers that run in development mode. To disable auto-deployment feature, use one of the following methods to place servers in production mode:

* In the Administration Console, click the name of the domain in the left pane, then select the Production Mode checkbox in the right pane.
* At the command line, include the following argument when starting the domain's Administration Server:

-Dweblogic.ProductionModeEnabled=true

Production mode is set for all WebLogic Server instances in a given domain.

Must EJBs be homogeneously deployed across a cluster? Why?

Yes. In WebLogic Server 6.0 and later, EJBs must be homogeneously deployed across a cluster for the following reasons:

* To keep clustering EJBs simple
* To improve performance by avoiding cross-server calls. If EJBs are not deployed on all servers, cross-server calls are more likely.
* To ensure that every EJB is available locall.y
* To ensure that all classes are loaded in an undeployable way. Every server must have access to each EJB's classes so that it can be bound into the local JNDI tree. If only a subset of the servers deploys the bean, the other servers will have to load the bean's classes in their respective system classpaths which makes it impossible to undeploy the beans.

Topics