Interview Questions

Can You Explain a Sample JUnit Test Case Class?

JUnit Questions and Answers

(Continued from previous question...)

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");
 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);
  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.

(Continued on next question...)

Other Interview Questions