Testing / Junit Interview questions II
Debugging the code using sysout requires manual scanning and it is not recommended.
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.
Define your Junit test class in the same package as that of class that has the target protected method.
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.
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); }
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.
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.
@Test (expected = Exception.class)
The limitation is only one exception per test method.
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.
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.
junit.framework.TestSuite is a container class that allows grouping and organizing multiple test cases into a collection and run them together.
No. The Test class will compile however it will not be executed.
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.
The JUnit runner declares that test as failed.
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.