Check out sample Adaface MCQ questions on Junit topics
Are you a candidate? Complete list of Junit interview questions 👇
- Explain what is Unit Test Case?
- Explain how you can write a simple JUnit test case?
- Mention different methods of exception handling in JUnit?
- Explain what is ignore test in JUnit?
- List some useful JUnit extensions and explain their purpose.
- Explain who should use JUnit – a developer or tester? Why you use JUnit to test your code?
- Explain what is JUnitCore Class?
- Explain how you can run JUnit from the command window?
- What is the difference between manual testing and automated testing?
- Give some disadvantages of manual testing.
- List out some advantages of automated testing.
- How is the testing of the 'protected' method done?
- How is the testing of 'private' method done?
- If the JUnit method's return type is 'string', what will happen?
- What is XMLUnit? What is the use of supporting classes in XMLUnit?
- What are the Cactus extensions? And what are its common components?
- What are the methods in fixtures?
- What is the use of @Test annotation?
- What are the important JUnit annotations?
- What is @Before and @BeforeClass and it’s usage?
- What is @After and @AfterClass and it’s usage?
- What is Parameterized test in JUnit and what all annotations used for this?
- What’s the use of @Rule annotation?
- What do you understand by JWebUnit? And what are its advantages?
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.
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.
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
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.
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.
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: Java org.junit.runner.JUnitCore
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.
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.
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.
To test the protected method, the test class is declared in the same package as the target class.
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.
JUnit test methods are designed to return 'void'. So the execution will fail.
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.
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.
The @Test annotation is used to mark the method as the test method.
The test runner is used to execute the test cases.
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.
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.
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.
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.
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.
@Rule annotation is used for creating objects, which later can be used in test methods.
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.