Junit interview questions and answers 👇

  1. Junit General Interview Questions

Junit General Interview Questions

Explain what is Unit Test Case?

View answer

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?

View answer

  • 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 different methods of exception handling in JUnit?

View answer

There are 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?

View answer

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

List some useful JUnit extensions and explain their purpose.

View answer

There are four main JUnit extensions, namely Cactus, JWebUnit, XMLUnit, and MockObject. We have described their uses in detail below.

  • Cactus: It is a framework for testing server-side Java code that implements tests inside a container. The cactus ecosystem has two components, namely the Cactus Framework and Cactus Integration Modules. The framework is the engine that provides the API for writing tests, while the integration modules take care of the front-end with plugins’ help.
  • JWebUnit: This java-based framework provides a unified testing interface by combining the capabilities of HtmlUnit and Selenium. JWebUnit makes it a breeze to navigate web applications via links, table content validation, form entry and submission, and other features. In addition to the high-level Java API, it allows rapid test creation with its ready-to-use assertions.
  • XMLUnit: It offers a single extension class called XMLTestCase. XMLUnit also provides supporting classes that allow assertions about the validity of an XML piece (Validator class), differences between two pieces of XML (Diff and DetailedDiff), the outcome of transforming XML (Transformer class), among others.
  • MockObject: When it is impossible or impracticable to include real objects in a unit test, mock objects can prove to be a boon.

You can simulate the behavior of complex objects and incorporate them with the following coding steps:

  • Create instances of mock objects.
  • Set state and expectations.
  • Invoke domain code using mock objects as parameters.
  • Verify consistency.

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

View answer

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:

  • To test early and 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?

View answer

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?

View answer

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

  • Set the CLASSPATH
  • Invoke the runner: Java org.junit.runner.JUnitCore

What is the difference between manual testing and automated testing?

View answer

Manual testing is performed by Human, so it is time-consuming and costly. Automated testing is performed by testing tools or programs, so it is fast and less costly.

Give some disadvantages of manual testing.

View answer

Following are some disadvantages of manual testing:

  • The testing is very time consuming and is very tiring.
  • The testing demands a very big investment in the human resources.
  • The testing is less reliable
  • The testing cannot be programmed.

List out some advantages of automated testing.

View answer

Some of the advantages of automated testing are:

  • It is very fast.
  • Investment is very less.
  • Testing is more reliable.
  • The testing can be programmed.

How is the testing of the 'protected' method done?

View answer

To test the protected method, the test class is declared in the same package as the target class.

How is the testing of 'private' method done?

View answer

There is no direct way for testing of the private method; hence manual testing is to be performed, or the method is changed to "protected" method.

If the JUnit method's return type is 'string', what will happen?

View answer

JUnit test methods are designed to return 'void'. So the execution will fail.

What is XMLUnit? What is the use of supporting classes in XMLUnit?

View answer

XMLUnit is used as a single JUnit extension class, XMLTestCase, and a set of supporting classes. Supporting classes assertions are made about following−

  • The distinction between two pieces of XML such as through Diff and DetailedDiff classes.
  • The validation of a piece of XML through the Validator class.
  • The result of transforming a piece of XML using XSLT through Transform class.
  • The evaluation of an XPath expression from an XML through classes that implement the XPath engine interface.
  • Individual nodes in a piece of XML which are exposed by DOM Traversal through Node Test class.

What are the Cactus extensions? And what are its common components?

View answer

Cactus is a simple test framework. It is for unit testing server-side java code such as servlets, EJBs and Tag Libs. The idea of Cactus is to minimize the cost of writing tests on server-side code. It uses Junit internally and extends the same. The way cactus implements are through an in-container strategy.

Cactus Ecosystem is made of many components −

  • Cactus Framework is the core of Cactus. To write Cactus tests, it has an API.
  • Cactus Integration Modules is another component which are front ends and frameworks that provide quicker ways of using the Cactus Framework be it Ant scripts or - Eclipse plugin or Maven plugin.

What are the methods in fixtures?

View answer

  • setup
  • tearDown

What is the use of @Test annotation?

View answer

The @Test annotation is used to mark the method as the test method.

What are the important JUnit annotations?

View answer

The test runner is used to execute the test cases.

  • @Test
  • @BeforeClass
  • @Before
  • @After
  • @AfterClass

What is @Before and @BeforeClass and it’s usage?

View answer

@Before annotation:

syntax: @Before public void myMethod()

This method should execute before each test. Such methods are generally used for initialization before performing a actual test in test environment.

@BeforeClass annotation:

syntax: @BeforeClass public static void myMethod()

This method should execute before all the tests. It executes only once. Method should be declared static. Mostly used for database connectivity tasks before execution of any of the test.

What is @After and @AfterClass and it’s usage?

View answer

@After annotation:

syntax: @After public void myMethod()

This method should execute after each test and used for cleaning up the test and temporary data to avoid memory issues.

@AfterClass annotation:

syntax: @AfterClass public static void myMethod()

This method should execute at the end, once all the tests are finished. Method should be declared static and executes only a single time. Mostly used for closing the database connection.

What is Parameterized test in JUnit and what all annotations used for this?

View answer

Parameterized tests are possible in JUnit and they provides us the liberty of passing parameters into the test classes.

@RunWith(Parameterized.class) – For making a class parametrized.

@Parameters – Parameterized class must have a static method for generating and returning a collection of array, this method should be marked with @Parameters annotation.

What’s the use of @Rule annotation?

View answer

@Rule annotation is used for creating objects, which later can be used in test methods.

What do you understand by JWebUnit? And what are its advantages?

View answer

This is the most popular Junit Interview Questions asked in an interview. WebUnit is also a Java-based testing framework to test web applications. It wraps around existing testing frameworks to allow quick testing of web applications and comes with a unified, simple testing interface.

To verify the correctness of an application, JWebUnit provides a high-level Java API to test a web application along with a set of assertions. This includes navigation through links and forms entry and submission. It also involves validation of table contents and other usual business web application features. The easy navigation methods that come with ready-to-use assertions allow for more rapid test creation than using Junit or HtmlUnit only. And if switching from HtmlUnit to other plugins such as Selenium are needed there should be no need to rewrite tests.