Prev Next

Testing / Junit Interview questions II

Could not find what you were looking for? send us the question and we would be happy to answer your question.

1. Can we use system.out.println() along with Unit test method?

Debugging the code using sysout requires manual scanning and it is not recommended.

2. Should we create test cases for getters and setters of a bean?

Not required. most of the getters and setters are less like to break. However we must consider adding test cases of getters/setters involving complex data types or special logic.

3. How will you test a method with protected access modifier?

Define your Junit test class in the same package as that of class that has the target protected method.

4. How will you test a method with private access modifier?

There is no way to test it as the private method can not be accessed outside of the class. Manual testing may be performed or use of Reflection API could help. Also consider changing the access modifier to protected.

5. Should every Junit test Class with in suite should have main() method?

Not required. However we may add main() method to run only the tests from that Test class.

 public static void main(String[] args) {
   junit.textui.TestRunner.run(MyTestClass.class);
}

6. What is JUnitCore class?

org.junit.runner.JUnitCore class is responsible for executing tests. runClasses() method of JUnitCore class enables running the one or more test classes which yield Result Object (org.junit.runner.Result).

The test results will be extracted from the Result Object.

JUnitCore is based on facade design pattern.

7. Differentiate Assert and Verify.

Assert works only if assertions ( -ea ) are enabled which is not required for Verify.

Assert throws an exception and hence it discontinue abruptly with the test if assert evaluates to false whereas it's not so with Verify.

8. How @Test annotation is used for testing exceptions?

@Test (expected = Exception.class)

The limitation is only one exception per test method.

9. When Unit Tests are created during software life cycle?

During development cycle, developers create unit tests for the functionality they are developing. It is developed parallelly along with actual code or immediately after the actual implementation.

10. Procedure to create a simple Junit test class.

Junit3

1.Define a subclass of TestCase.

2.Override the setUp() method to initialize object(s) under test.

3.Optionally override the tearDown() method to release object(s) under test.

4.Define one or more public testXYZ() methods that exercise the object(s) under test and assert expected results.

11. What Is Junit TestSuite?

junit.framework.TestSuite is a container class that allows grouping and organizing multiple test cases into a collection and run them together.

12. Can a test method be declared as private or protected?

No. The Test class will compile however it will not be executed.

13. Can a test method return a value?

For e.g. if the test method returns a List of Objects or an primitive datatype, it will not be excuted as a test method even though it compiles.

14. What happens when a test method throws an exception?

The JUnit runner declares that test as failed.

15. How often should you run your JUnit Tests?

You should run all your unit tests as often as possible, ideally every time the code is changed. Make sure all your unit tests always run at 100%. Frequent testing gives you confidence that your changes didn't break anything and generally lowers the stress of programming in the dark.

«
»
Junit Interview questions III

Comments & Discussions