Index

General

  1. Explain what is Unit Test Case?
  2. Explain how you can write a simple JUnit test case?
  3. Mention different methods of exception handling in JUnit?
  4. Explain what is ignore test in JUnit?
  5. List some useful JUnit extensions and explain their purpose.
  6. Explain who should use JUnit – a developer or tester? Why you use JUnit to test your code?
  7. Explain what is JUnitCore Class?
  8. Explain how you can run JUnit from the command window?
  9. What is the difference between manual testing and automated testing?
  10. Give some disadvantages of manual testing.
  11. List out some advantages of automated testing.
  12. How is the testing of the 'protected' method done?
  13. How is the testing of 'private' method done?
  14. If the JUnit method's return type is 'string', what will happen?
  15. What is XMLUnit? What is the use of supporting classes in XMLUnit?
  16. What are the Cactus extensions? And what are its common components?
  17. What are the methods in fixtures?
  18. What is the use of @Test annotation?
  19. What are the important JUnit annotations?
  20. What is @Before and @BeforeClass and it’s usage?
  21. What is @After and @AfterClass and it’s usage?
  22. What is Parameterized test in JUnit and what all annotations used for this?
  23. What’s the use of @Rule annotation?
  24. What do you understand by JWebUnit? And what are its advantages?


The Questions
General
1. 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.

2. 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.
3. Mention different methods of exception handling in JUnit?

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.
4. 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
5. List some useful JUnit extensions and explain their purpose.

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.

6. 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:

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

8. 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: Java org.junit.runner.JUnitCore
9. What is the difference between manual testing and automated testing?

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.

10. Give some disadvantages of manual testing.

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.
11. List out some advantages of automated testing.

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.
12. How is the testing of the 'protected' method done?

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

13. How is the testing of 'private' method done?

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.

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

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

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

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.
16. What are the Cactus extensions? And what are its common components?

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.
17. What are the methods in fixtures?
  • setup
  • tearDown
18. What is the use of @Test annotation?

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

19. What are the important JUnit annotations?

The test runner is used to execute the test cases.

  • @Test
  • @BeforeClass
  • @Before
  • @After
  • @AfterClass
20. What is @Before and @BeforeClass and it’s usage?

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

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

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

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

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.

23. What’s the use of @Rule annotation?

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

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

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.

Want to test this skill? Check out Adaface assessments

JUnit Test

View Test