Testing / Junit Interview questions III
1. Explain parameterized tests in Junit.
In Junit 4, a new feature named "parameterized tests" is introduced that allows developer to run the same test repeated for different input values. Creating parameterized tests using Junit 4 is simple and the steps are as follows....
2. When are unit tests garbage collected in Junit?
As per the design, the Test instances tree is built at first pass and the test execution is performed as the second pass. The test runner holds strong references to all test instances for the duration of the test execution....
3. What are the different methods of exception handling in JUnit?
There are different ways of exception handling in JUnit. Try catch idiom.
4. Explain the steps involved in writing a simple JUnit test case.
Create a subclass of TestCase. To initialize object under test, override the setup() method....
5. What is @ignore test annotation in JUnit framework?
When the junit test code is not complete and it could fail if executed, then it can be annotated using @Ignore annotation to prevent the test method/class from execution. When @Ignore applied to a test method, it prevents the method...
6. How do I execute JUnit from command line?
Executng Junit from command line involves 2 steps. 1.
7. Junit: What is @Rule annotation?
@Rule annotation in Junit is used to create objects that later can be used with the test methods. Junit @Rule annotation works on the principle of AOP (aspect oriented programming)....
8. Junit: Can we pass command line arguments to a test execution?
Yes, We can pass command line arguments to a test execution by using -D JVM command-line options as shown below. java -D JVM command-line options
9. Explain Junit Test Fixture.
A test fixture refers a fixed state of a set of objects used as a baseline for running tests. The purpose of a test fixture is to ensure that there is a well known and fixed environment in which tests...
10. Can a Junit test method be declared as private?
No. Junit requires that all test methods must be declared as public.
11. Junit: How do I test a protected method?
In Java, a protected method can only be accessed within the same package where the class is defined. So, define your Junit test class in the same package as the target class.
12. Junit: How do I test a private method?
The private methods cannot be tested as any private method only be accessed within the same class. The private method need to tested manually or be converted to protected method.
13. Explain cyclomatic complexity.
Cyclomatic complexity is determined based on number of decision points within the code and hence execution paths. Higher the cyclomatic complexity makes it difficult to attain achieve test/code coverage.
14. What is JWebUnit?
JWebUnit is a Java-based testing framework for unit testing web applications. It wraps existing testing frameworks such as HtmlUnit and Selenium with a unified, simple testing interface to allow you to quickly test the correctness of your web applications.
15. List some of the JUnit extensions.
JUnit extensions include, Cactus. JWebUnit.
16. What happens if a test method throws an exception?
The JUnit runner will declare that test as fail when the test method throws an exception.
17. What is Mockito?
Mockito is a popular mock framework that can be used in conjunction with JUnit. Mockito allows you to create and configure mock/dummy objects....
18. Mention a few mocking test frameworks.
Mockito and EasyMock.
19. What are the useful JUnit Extensions?
JWebUnit, XMLUnit, Cactus, and MockObject.