JUnit Interview Questions Part I

What is Junit?

Junit is a unit testing framework introduced by Apache. Junit is based on Java.

What are Junit annotations?

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.

Learn more about JUnit

What is TestNG and how is it better than Junit?

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

Explain what is JUnit?

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.

Explain what is Unit Test Case?

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.

Explain how you can write a simple JUnit test case?

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.

Mention what are parameterized tests?

Parameterized tests enable developer to perform the same test over and again using different values.

Mention different methods of exception handling in JUnit?

try catch idiom

  • With JUnit rule

  • With @Test annotation

  • With catch exception library

  • With customs annotation

Explain what is ignore test in JUnit?

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

Learn about Java in detail

List out some useful JUnit extensions?

JUnit extensions include

  • Cactus

  • WebUnit

  • XMLUnit

  • MockObject

Explain who should use JUnit – a developer or tester? Why you use JUnit to test your code?

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

Explain what is JUnitCore Class?

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.

Explain how you can run JUnit from the command window?

To run JUnit from the command window, you have to follow the steps

  • Set the CLASSPATH

  • Invoke the runner

Who Should Use JUnit?

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.

What are the features of JUnit?
  • Opensource

  • Annotation support for test cases

  • Assertion support for checking the expected result

  • Test runner support to execute the test case

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

Where Do You Download JUnit?

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 You 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

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 :

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.

Do You Know the Book "JUnit in Action"?

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

  • JSP

  • EJB

  • DB apps

  • Taglibs

Why Java is considered dynamic?

It is designed to access to objects on run-time.