JUnit Questions

JUnit Interview Questions :-

  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.

  What is unit testing?

A complete application can be build up by integrating small-2 functional parts, such parts are called as units. It is always better to test such individual units before testing the entire application. The process of testing the functionality and working of these individual unit is known as unit testing. Unit testing can be done manually and the process can also be automated.

  Who Should Use JUnit, Developers or Testers?

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.

  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 Manual testing vs Automated testing?

Manual testing :- Test cases are executed by humans, it's time consuming and costly.

Automated Testing :- No human involvement, test cases are executed by automated tools and programs, It's fast and less costly compared to manual testing.

  Explain how you can write a simple JUnit test case?

1. Determine a subclass of TestCase.

2. To initialize object(s) under test, override the setup() method.

3. To release object(s) under test override the teardown() method.

  Mention different methods of exception handling in JUnit?

There are different methods of exception handling in JUnit.

1. Try catch idiom.

2. With JUnit rule.

3. With @Test annotation.

4. With catch exception library.

5. With customs annotation.

  What is @Test and where it's used?

@Test annotation is used to mark a method as test method, result of which is then compared with expected output to check whether the test is successful or not.

  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.

1. It will not execute a test method annotated with @Ignore.

2. It will not execute any of the test methods of test class if it is annotated with @Ignore

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

@Before annotation :-

syntax :-


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


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.

  List out some useful JUnit extensions?

JUnit extensions include :-

1. Cactus

2. JWebUnit

3. XMLUnit

4. MockObject

  Explain what is JUnitCore Class?

UnitCore class is an inbuilt class in JUnit package; it is based on Facade design pattern, this class is used to run only definite test classes only.

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

@After annotation :-

syntax :-


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


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.

  Explain how you can run JUnit from the command window?

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

1. Set the CLASSPATH.

2. Invoke the runner :- Java org.junit.runner.JUnitCore

  Mention what are parameterized tests?

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

  What is the difference between JUnit and TestNG?

JUnit :-

1. In JUnit the naming convention for annotation is a bit complicated for, e.g., "Before", "After" and "Expected".

2. In JUnit, for a method declaration you have to follow a specific style like using "@BeforeClass" and "@AfterClass".

3. In JUnit method name constraint is present.

4. JUnit framework does not have JUnit does not support parallel execution on Selenium test cases "Parameterized Test" or "Dependency Test" feature.

5. In JUnit grouping of test cases are not available.

6. JUnit does not support parallel execution on Selenium test cases

7. It cannot re-run the failed cases.

TestNG :-

1. In TestNG it is easier to understand annotations like "BeforMethod", "AfterMethod" and "ExpectedException".

2. In TestNG, there is no restriction like you have to declare methods in a specific format.

3. In TestNG method name constraint is not present, and you can determine any test method names.

4. TestNG use "dependOnMethods" to implement the dependency testing.

5. In TestNG, grouping of test cases is available.

6. In TestNG Parallel execution of Selenium test cases are possible.

7. It can rerun the failed tests.

  What's the use of @Rule annotation?

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

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

Ideally you should not do this. All the JUnit test methods should have a void return type. If you change the return type then the test method would not be considered as a test method and would be ignored during execution of tests.

  Is it possible to pass command-line arguments to a test execution?

Yes, It's possible to pass command line arguments to a test execution :-

You should use this command :-

-D JVM command-line options

  What are important features of JUnit?

1. It is an open source framework.

2. Provides Annotation to identify the test methods.

3. Provides Assertions for testing expected results.

4. Provides Test runners for running tests.

5. JUnit tests can be run automatically and they check their own results and provide immediate feedback.

6. JUnit tests can be organized into test suites containing test cases and even other test suites.

7. JUnit shows test progress in a bar that is green if test is going fine and it turns red when a test fails.

  What are JUnit classes? List some of them?

JUnit classes are important classes which are used in writing and testing JUnits. Some of the important classes are :-

1. Assert :- A set of assert methods.

2. Test Case :- It defines the fixture to run multiple tests.

3. Test Result :- It collects the results of executing a test case.

4. Test Suite :- It is a Composite of Tests.

  What is JUnit Test Fixture?

A test fixture is a fixed state of a set of objects used as a baseline for running tests. Their purpose is to ensure that there is a well known and fixed environment in which tests are run so that results are repeatable.

Examples of fixtures :-

1. Loading a database with a specific, known set of data.

2. Copying a specific known set of files.

3. Preparation of input data and setup/creation of fake or mock objects.

If a group of tests shares the same fixtures, you should write a separate setup code to create the common test fixture. If a group of tests requires different test fixtures, you can write code inside the test method to create its own test fixture.

  Why Not Just Use a Debugger for Unit Testing?

1. A debugger is designed for manual debugging and manual unit testing, not for automated unit testing.

2. JUnit is designed for automated unit testing.

3. Automated unit testing requires extra time to setup initially. But it will save your time, if your code requires changes many times in the future.

  Why Not Just Write a main() Method for Unit Testing?

It is possible to write a main() method in each class that need to be tested for unit testing. In the main() method, you could create test object of the class itself, and write some tests to test its methods.

  Why Not Just Use System.out.println() for Unit Testing?

Inserting debug statements into code is a low-tech method for debugging it. It usually requires that output be scanned manually every time the program is run to ensure that the code is doing what's expected.

It generally takes less time in the long run to codify expectations in the form of an automated JUnit test that retains its value over time. If it's difficult to write a test to assert expectations, the tests may be telling you that shorter and more cohesive methods would improve your design.

  Under What Conditions Should You Test set() and get() Methods?

Tests should be designed to target areas that might break. set() and get() methods on simple data types are unlikely to break. So no need to test them.

set() and get() methods on complex data types are likely to break. So you should test them.


Let's do interview preparation for node.js..

Read more