Junit Interview Question Answer Part 1

Junit is a unit testing framework introduced by Apache. Junit is based on Java.
Following are the Junit Annotations:

  • @Test: Annotation lets the system know that the method annotated as @Test is a test method. There can be multiple test methods in a single test script.
  • @Before: Method annotated as @Before lets the system know that this method shall be executed every time before each of the test method.
  • @After: Method annotated as @After lets the system know that this method shall be executed every time after each of the test method.
  • @BeforeClass: Method annotated as @BeforeClass lets the system know that this method shall be executed once before any of the test method.
  • @AfterClass: Method annotated as @AfterClass lets the system know that this method shall be executed once after any of the test method.
  • @Ignore: Method annotated as @Ignore lets the system know that this method shall not be executed.
TestNG is an advance framework designed in a way to leverage the benefits by both the developers and testers. With the commencement of the frameworks, JUnit gained an enormous popularity across the Java applications, Java developers and Java testers with remarkably increasing the code quality. Despite being easy to use and straightforward, JUnit has its own limitations which give rise to the need of bringing TestNG into the picture. TestNG is an open source framework which is distributed under the Apache software License and is readily available for download.

TestNG with WebDriver provides an efficient and effective test result format that can in turn be shared with the stake holders to have a glimpse on the product’s/application’s health thereby eliminating the drawback of WebDriver’s incapability to generate test reports. TestNG has an inbuilt exception handling mechanism which lets the program to run without terminating unexpectedly.

There are various advantages that make TestNG superior to JUnit. Some of them are:

  • Added advance and easy annotations
  • Execution patterns can set
  • Concurrent execution of test scripts
  • Test case dependencies can be set
JUnit is a testing framework for unit testing. It uses Java as a programming platform, and it is an Open Source Software managed by the JUnit.org community.
Unit Test Case is a part of the code that ensures that the another part of code (method) behaves as expected. For each requirement, there must be at least two test cases one negative test and one positive test.
Determine a subclass of TestCase

  • To initialize object(s) under test, override the setup() method
  • To release object(s) under test override the teardown() method


  • Determine one or more public test XYZ() methods that exercise the objects under test and assert expected results..
Parameterized tests enable developer to perform the same test over and again using different values.
try catch idiom
  • With JUnit rule
  • With @Test annotation
  • With catch exception library
  • With customs annotation
When your code is not ready, and it would fail if executed then you can use @Ignore annotation.

  • It will not execute a test method annotated with @Ignore
  • It will not execute any of the test methods of test class if it is annotated with @Ignore
JUnit extensions include

  • Cactus
  • WebUnit
  • XMLUnit
  • MockObject
JUnit is more often used by developers to implement unit tests in JAVA. It is designed for unit testing that is more a coding process and not a testing process. However, many testers and QA engineers use JUnit for unit testing.

JUnit is used because

  • It test early and does automate testing
  • JUnit tests can be compiled with the build so that at unit level, regression testing can be done
  • It allows test code re-usage
  • JUnit tests behave as a document for the unit tests when there is a transfer
JUnitCore class is an inbuilt class in JUnit package; it is based on Façade design pattern, this class is used to run only definite test classes only.
To run JUnit from the command window, you have to follow the steps

  • Set the CLASSPATH
  • Invoke the runner
JUnit is mostly used by developers for testing their written code. 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.
  • Opensource
  • Annotation support for test cases
  • Assertion support for checking the expected result
  • Test runner support to execute the test case
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
You should know this book. It received some good reviews.

Title: JUnit Recipes: Practical Methods for Programmer Testing
Publisher: Manning Publications (July 15, 2004)
Author: J. B. Rainsberger, Scott Stirling
Abstract: Get this book both as an introduction to what JUnit can do for you and also as a reference when you are looking for information on how to solve a particular testing problem. The book covers the basics of using JUnit as well as issues around database and J2EE testing. It is filled with pointers to many frameworks and resources that can simplify your testing experience. This is not just a testing cookbook. J.B. also discusses issues such as how to design for testing (and how that can improve your architecture) as well as what to do when you need to test legacy code. This is an excellent guide to the details of how to use JUnit effectively.
Topics: Getting started with JUnit
Recipes for:

  • Servlets
  • JSPs
  • EJBs
  • Database code


  • ... Difficult-to-test designs, and how to fix them
    How testing saves time
    Choose a JUnit extension:

    • HTMLUnit
    • XMLUnit
    • ServletUnit
    • EasyMock
Where do I download JUnit? I don't think anyone will ask this question in a job interview. But the answer is simple. You should follow the download instructions from the JUnit official Website: JUnit.org.
How do I install JUnit?:
First I will download the lastest version of JUnit.
Then I will extract all files from the downloaded file.
The most important file should be the JUnit JAR file: junit-4.4.jar, which contains all JUnit class packages.
To verify junit-4.4.jar, I will run the org.junit.runner.JUnitCore class

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

JUnit version 4.4

Time: 0

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


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.
You should know this book. It received some good reviews.

Title: JUnit in Action
Publisher: Manning Publications (October 1, 2003)
Author: Vincent Massol, Ted Husted
Abstract: A guide to unit testing Java applications (including J2EE applications) using the JUnit framework and its extensions, this book provides techniques for solving real-world problems such as unit testing legacy applications, writing real tests for real objects, automating tests, testing in isolation, and unit testing J2EE and database applications. Using a sample-driven approach, various unit testing strategies are covered, such as how to unit test EJBs, database applications, JSPs, and Taglibs. Also addressed are testing strategies using freely available open source frameworks and tools, and how to unit test in isolation with Mock Objects. Testing J2EE applications by running tests from inside the container for performing integration unit tests is discussed, as is how to automate unit testing in automated builds (such as Ant and Maven) for performing continuous integration.
Topics:

  • Testing in isolation with mock objects
  • In-container testing with Cactus
  • Automated builds with Ant and Maven
  • Unit testing from within Eclipse
  • Test Driven Development principles
  • Unit testing
  • Java apps
  • Filters
  • Servlets
  • EJB
  • JSP
  • DB apps
  • Taglibs
It is designedaccesses to objects on run-time.