JUnit Interview Questions And Answers

JUnit is the de facto standard unit testing library for the Java language. JUnit 4 is the first significant release of this library in almost three years. It promises to simplify testing by exploiting Java 5's annotation feature to identify tests rather than relying on subclassing, reflection, and naming conventions. In this article, obsessive code tester Elliotte Harold takes JUnit 4 out for a spin and details how to use the new framework in your own work. Note that this article assumes prior experience with JUnit.

JUnit, developed by Kent Beck and Erich Gamma, is almost indisputably the single most important third-party Java library ever developed. As Martin Fowler has said, "Never in the field of software development was so much owed by so many to so few lines of code." JUnit kick-started and then fueled the testing explosion. Thanks to JUnit, Java code tends to be far more robust, reliable, and bug free than code has ever been before. JUnit (itself inspired by Smalltalk's SUnit) has inspired a whole family of xUnit tools bringing the benefits of unit testing to a wide range of languages. nUnit (.NET), pyUnit (Python), CppUnit (C++), dUnit (Delphi), and others have test-infected programmers on a multitude of platforms and languages.

However, JUnit is just a tool. The real benefits come from the ideas and techniques embodied by JUnit, not the framework itself. Unit testing, test-first programming, and test-driven development do not have to be implemented in JUnit, any more than GUI programming must be done with Swing. JUnit itself was last updated almost three years ago. Although it's proved more robust and longer lasting than most frameworks, bugs have been found; and, more importantly, Java has moved on. The language now supports generics, enumerations, variable length argument lists, and annotations--features that open up new possibilities for reusable framework design.

JUnit's stasis has not been missed by programmers who are eager to dethrone it. Challengers range from Bill Venners's Artima SuiteRunner to Cedric Beust's TestNG. These libraries have some features to recommend them, but none have achieved the mind or market share held by JUnit. None have broad out-of-the-box support in products like Ant, Maven, or Eclipse. So Beck and Gamma have commenced work on an updated version of JUnit that takes advantage of the new features of Java 5 (especially annotations) to make unit testing even simpler than it was with the original JUnit. According to Beck, "The theme of JUnit 4 is to encourage more developers to write more tests by further simplifying JUnit." Although it maintains backwards compatibility with existing JUnit 3.8 test suites, JUnit 4 promises to be the most significant innovation in Java unit testing since JUnit 1.0.



<< Previous               

 

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


22. 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.*;

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);
}
}


23. 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.*;

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());
}

}


24. What is JUnit?


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


25. When should tests be written?


Tests should be written before the code. Test-first programming is practiced by only writing new code when an automated test is failing.

Good tests tell you how to best design the system for its intended use. They effectively communicate in an executable format how to use the software. They also prevent tendencies to over-build the system based on speculation. When all the tests pass, you know you're done!

Whenever a customer test fails or a bug is reported, first write the necessary unit test(s) to expose the bug(s), then fix them. This makes it almost impossible for that particular bug to resurface later.


Your Ad Here

26. Who Should Use JUnit, Developers or Testers?


I should say that JUnit is mostly used by developers. JUnit is designed for unit testing, which is really a coding process, not a testing process.

But many testers or QA engineers, are also required to use JUnit for unit testing. For example, I found this job title on the Internet: Lead QA Engineer - Java / J2EE / whitebox / SAP / Junit


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


28. Why does JUnit only report the first failure 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.


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


30. Why not just use System.out.println()?


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.


Your Ad Here

<< Previous               

 
Databases Questions and Answers
 
DB2 Interview Questions and Answers
IMS Interview Questions and Answers
MYSQL Interview Questions and Answers
Oracle Interview Questions and Answers
PL/SQL Interview Questions And Answers
Quel Interview Questions and Answers
SQL Interview Questions and Answers

 
 
.NET Interview Questions and Answers
 
ASP Interview Questions and Answers
C# Interview Questions and Answers
Visual Basic Interview Questions and Answers
 
J2EE Interview Questions and Answers
 
Enterprise Java Beans (EJB) Interview Questions And Answers
Hibernate Interview Questions And Answers
Jave Messaging Service (JMS) Interview Questions And Answers
Jave Server Faces (JSF) Interview Questions And Answers
Java Server Pages (JSP) Interview Questions And Answers
Servlets Interview Questions And Answers
Spring Framework Interview Questions And Answers
Struts Framework Interview Questions And Answers
 
JAVA Interview Questions and Answers
 
Core Java Interview Questions and Answers
Java Platform, Micro Edition (Java ME) Interview Questions And Answers
JAVA GUI Interview Questions and Answers
Java Performance Tuning Interview Questions And Answers
JUnit Interview Questions And Answers
Remote Method Invocation (RMI) Interview Questions And Answers
Unified Modeling Language (UML) Interview Questions And Answers
 
Java Application Server Interview Questions And Answers
 
Tomcat Application Server Interview Questions And Answers
WebLogic Application Server Interview Questions And Answers
 
Mainframe Interview Questions and Answers
 
CICS Interview Questions and Answers
COBOL Interview Questions and Answers
IMS Interview Questions and Answers
JCL Interview Questions and Answers
REXX Interview Questions and Answers
TELON Interview Questions and Answers
VSAM Interview Questions and Answers
 
Object Oriented Language Questions and Answers
 
C Interview Questions and Answers
C++ Interview Questions and Answers
Eiffel Interview Questions and Answers
J2EE Interview Questions and Answers
JAVA Interview Questions and Answers
Sather Interview Questions and Answers
Small talk Interview Questions and Answers
 
Operation Systems Interview Questions and Answers
 
MS DOS Interview Questions and Answers
Unix OS Interview Questions and Answers
Windows Interview Questions and Answers
 
Scripting languages Interview Questions and Answers
 
Java Script Interview Questions and Answers
Practical Extraction and Reporting Language Interview Questions and Answers
PHP Interview Questions and Answers
VBScript Interview Questions and Answers
 
UserInterfaces Questions and Answers
 
Foxit Interview Questions and Answers
Glade Interview Questions and Answers
Tweak Interview Questions and Answers
 
WEB Technologies Questions and Answers
 
AJAX Interview Questions and Answers
HTML Interview Questions and Answers
WML Interview Questions and Answers
XML Interview Questions and Answers
 
Interview Tips And Some Common Questions
 
Behavioral Interview Questions And Answers


Your Ad Here