Blog

Top 20 JUnit Interview Questions for SDET

Top 20 JUnit Interview Questions for SDET

Share:

1. What is JUnit?

JUnit is a regression testing framework which is used for performing Unit Testing of Java code.

It is an open-source software managed by JUnit.org community.

JUnit = Java + Unit Testing

2. What are the important features of JUnit?

The important features of JUnit are:

  • It is an open-source framework.
  • It provides various annotations to identify test methods.
  • It provides test runners for running tests.
  • It provides assertions to test the expected results.
  • It shows test progress in a bar. If the test goes fine, then it shows green and when a test fails, it turns to red.
  • It can be organized into test suites that contain test cases and other test suites as well.

3. What are the core features of JUnit?

JUnit framework provides the following features:

  • Fixtures
  • Test Suites
  • Test Runners JUnit Classes

4. What are JUnit classes? List some of JUnit classes.

JUnit classes are important classes that are used in writing and testing JUnit programs.

Some of the important JUnit classes are:

  • Assert: Contains a set of Assert methods.
  • TestCase: Contains a test case that defines the fixture to run multiple tests.
  • TestResult: Contains a method that collects the results after execution of the test case.
  • TestSuite: It is a composition of Junit tests.

5. What is a Fixture in JUnit?

A test fixture in JUnit is defined as a fixed state of some set of objects which are used as a baseline for running tests.

The main objective of a test fixture is to ensure that there is some known and fixed environment in which tests are run so that results are repeatable.

It basically includes the following methods:

  • setUp() method which runs before every test methods.
  • tearDown() method which runs after every test methods.

6. What are JUnit annotations and how are they useful in JUnit?

JUnit annotations are like meta-tags that can be added to our code and we can apply them to methods or in class.

Annotations are very useful in JUnit as they give the information about test methods that which methods are going to run before test methods or which methods are going to run after test methods. It also gives the information that which method or class will be ignored during execution.

7. What are the important JUnit annotations?

The important JUnit annotations are as follows:

  • @Test: The Test annotation tells that this is the test method which will run first unless specified.
  • @BeforeClass: The method with @BeforeClass annotation specifies that it should be run once before any of the Test methods in the class.
  • @Before: The method with @Before annotation specifies that it should be run before each test method.
  • @After: The method with @After annotation specifies that it should be run after the test method.
  • @AfterClass: This method with @AfterClass annotation specifies that it should be run after all tests have finished. This can be used for performing clean-up activities.

8. What is @ignore annotation in JUnit and how is this useful?

@Ignore annotation is used to ignore that particular tests or group of tests so that build failure can be skipped.

@Ignore annotation is very useful when there are cases when we can’t fix a code that is failing but still, we want that method to be around so that it does not get forgotten and we can fix them later.

Also, it is very easy to identify all @Ignore annotation rather than finding the comment out test cases.

9. What are ParameterizAd tests in JUnit and what are the annotations used for this?

Parameterized Tests allow developers to pass parameters into their test classes and hence allowing him to run the same test, again and again, using different values.

Annotations used for Parameterization are

  • @RunWith(Parameterized.Class) – To make a class parameterized.
  • @Parameters – Parameterized class must have a static method with a @Parameter annotation that returns a collection of the array as a test data set.

10. Can we change the return type of JUnit test method from void to some other type?

Ideally, we should not change the return type of JUnit test from void to some other type. All the methods of JUnit tests should have a void return type.

If we change the return type from void to some other type, then the test method would not be considered as a test method and would be ignored during the execution of tests.


11. Name the tools with which JUnit can be easily integrated.

JUnit can be easily integrated with the following tools:

  • Eclipse
  • Ant
  • Maven

12. Name some JUnit extensions.

Following are some of the JUnit extensions:

  • Cactus
  • JWebUnit
  • XMLUnit
  • MockObject

13. What are Cactus extensions and what are its common components?

Cactus is a testing framework that is used for unit testing server-side java code such as Servlets, EJBs and Tag Libs.

The intent of Cactus is to minimize the cost of writing tests for server-side code. It uses JUnit internally and extend it. Cactus implements an in-container strategy which means that the all the tests are executed inside the container.

The components of cactus are:

  • Cactus framework is the core of Cactus. It provides the API to write Cactus tests.
  • Cactus Integration Modules are other components and they are front ends and frameworks that provide the easy way of using the Cactus framework like Ant Scripts, Eclipse plugin or Maven plugin. •

14. What is JWebUnit and what are its advantages?

WebUnit is a Java-based testing framework to test web applications. It wraps existing testing frameworks like Selenium and HtmlUnit with a simple, unified testing interface and that allows us to quickly test the correctness of web applications.

The various advantages of JWebUnit are as follows:

  • It provides a high-level Java API which is used for navigating a web-application with a set of assertions to verify the correctness of the application.
  • This API includes form entry and submission, navigation via the link, validation of table contents and other typical business web application features.
  • The ready to use assertions and simple navigation is helpful for more rapid test creation.

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

XMLUnit is used for providing a single JUnit extension class, XMLTestCase and a set of supporting classes.

Supporting classes in XMLUnit allow assertions to be made about:

  • Differences between 2 pieces of XML through Diff and DetailedDiff classes.
  • Validation of a piece of XML through Validator class.
  • The result of transforming a piece of XML using XSLT through transform class.
  • The evaluation of XPath expression on a piece of XML through XPath engine interface.
  • Individual nodes in a piece of XML that are exposed by Dom traversal through NodeTest class.

16. Why does JUnit only report the first failure in a single test?

Reporting multiple failures in a single test implies that the test is doing too much and it is too big a unit test.

So, JUnit is designed in such a way that it works best with a number of small tests.

It executes each test within a separate instance of test class and reports failure on each test.

17. How can we run JUnit from the command window?

To run JUnit from the command window, we have to follow the following steps:

  • Set the ClassPath: set CLASSPATH = %JUNIT_HOME%\junit.jar
  • Invoke JUnit runner: java org.junit.runner.JUnitCore

18. Mention different methods of exception handling in JUnit.

The different methods of Exception handling in JUnit are as follows:

  • Try catch statement
  • With @Test annotation
  • With catch exception library
  • With JUnit rule
  • With custom annotation

19. Mention best practices to write a unit test case in JUnit.

The various best practices to be followed while writing a unit test in JUnit are as follows:

  • A well-written test case is the one that has a known input and expected output, which is computed before the execution of the test.
  • A known input should always test a precondition and the expected output should always verify a post-condition.
  • It is always recommended to have at least 2 unit test cases for each requirement, one of them is positive test and the other one is for negative test.
  • If any requirement have sub-requirements, then it should also have at least 2 unit test cases as positive and negative tests.

20. Mention the differences between JUnit and TestNG.

JUnitTestNG
The naming convention for JUnit annotation is a bit complicated like “Before”, “After” and “Expected”.The naming convention of TestNG is easier to understand like “BeforMethod”, “AfterMethod”, etc.
Grouping of test cases is not available in JUnit.Grouping of test cases is possible in TestNG.
JUnit framework does not have the “Dependency Test” feature.TestNG use “dependsOnMethods” to implement dependency testing.
JUnit does not support parallel execution of Selenium test cases.Parallel execution of Selenium test case is possible in TestNG.
JUnit cannot re-run the failed tests.TestNG can re-run the failed tests.

More SDET Interview Question by Devlabs Alliance

  1. TOP 20 C# PROGRAMMING INTERVIEW QUESTIONS FOR SDET
  2. TOP 50 SELENIUM INTERVIEW QUESTIONS FOR SDET
  3. TOP 50 GIT INTERVIEW QUESTIONS FOR SDET
  4. TOP 20 TESTNG INTERVIEW QUESTIONS FOR SDET
  5. TOP 20 JAVA PROGRAMMING INTERVIEW QUESTIONS FOR SDET
  6. TOP 20 CUCUMBER INTERVIEW QUESTIONS FOR SDET
  7. TOP 20 BASIC JAVA INTERVIEW QUESTIONS FOR SDET
  8. TOP 20 SOFTWARE TESTING INTERVIEW QUESTIONS FOR SDET

Related Articles
Categories
Latest Articles

Popular Courses

Google Reviews
  • review rating 4  Both trainers (Ravi and Naresh) were excellent in their approach. Session was less of a theory and more of a practical. And even the theory part was quite interesting. A bit issue with the volume of information which I had received in just a span of 2-3 days - it seemed a bit over-loaded. But overall, it has given me a great platform to take it forward from here!

    thumb Varenya Varshney
    6/02/2019
  • review rating 4  Training provided by the trainers in DevLabs Alliance is completely awesome. Trainers are highly qualified in their areas with practical experience. Since it is 3 days training we have completely got the opportunity to have hands on.

    thumb Salaja K
    6/30/2019
  • review rating 5  Training provided by the trainers here is completely awesome. Trainer is highly qualified in their areas with practical experience. Also amenities provided during training by institute is great

    thumb Abhishek Tripathi
    6/02/2019
  • review rating 5  Excellent Training program. Training courses and trainers are very good. Trainer will explain you as many time you ask the question. Best thing is hands on based training. You learn by doing. Looking for more courses from here.

    thumb Vijay Kumar
    6/30/2019
  • review rating 4  It was good session, I left my training room with basic knowledge on all the tools. Tutors have great knowledge and experience on the tools. But three days is not enough time.

    thumb sairam shyamala
    6/30/2019
$('.enrollbtn').on('click', function (){ $( "#announcement" ).tabs( "option", "active", 0 ); $(' a[href="#announcement"]').trigger('click'); });