Java - Interview Questions and Answers on Testing

Q1. What is assert keyword used for ? show Answer

Ans. The assert keyword is used to make an assertion—a statement which the programmer believes is always true at that point in the program. This keyword is intended to aid in testing and debugging.
Q2. Which load testing tools have you used ? show Answer

Ans. Rational Robot, JMeter, LoadRunner.
Q3. What are use cases?
show Answer

Ans. It is part of the analysis of a program and describes a situation that a program might encounter and what behavior the program should exhibit in that circumstance.
Q4. Difference between Assert and Verify ? show Answer

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

Assert throws an exception and hence doesn't continue with the test if assert evaluates to false whereas it's not so with Verify.
Q5. What are the annotations used in Junit with Junit4 ? show Answer

Ans. @Test

The Test annotation indicates that the public void method to which it is attached can be run as a test case.

@Before

The Before annotation indicates that this method must be executed before each test in the class, so as to execute some preconditions necessary for the test.

@BeforeClass

The BeforeClass annotation indicates that the static method to which is attached must be executed once and before all tests in the class.

@After

The After annotation indicates that this method gets executed after execution of each test.

@AfterClass

The AfterClass annotation can be used when a method needs to be executed after executing all the tests in a JUnit Test Case class so as to clean-up the set-up.

@Ignores

The Ignore annotation can be used when you want temporarily disable the execution of a specific test.
Q6. How to create a Junit to make sure that the tested method throws an exception ? show Answer

Ans. Using annotation Test with the argument as expected exception.

@Test (expected = Exception.class)
Q7. What should I do if I want to make sure that a particular method of a class is getting called ? show Answer

Ans. If its a static method of the class , we can use verify to make sure its getting called.

If its an instance method , We can mock the object and then use verify with the mocked object to make sure that the method is getting called.
Q8. What is the use of Mockito.any ? show Answer

Ans. In case we need to verify that a method is being called with any argument and not a specific argument we can use Mockito.any(Class), Mockito.anyString, Mockito.anyLong etc.
Q9. How should we ignore or avoid executing set of tests ? show Answer

Ans. We can remove @Test from the respective test so as to avoid its execution. Alternatively we can put @Ignore annotation on the Junit file if we want to ignore all tests in a particular file.
Q10. How can we test methods individually which are not visible or declared private ? show Answer

Ans. We can either increase their visibility and mark them with annotation @VisibleForTesting or can use reflection to individually test those methods.
Q11. Name few Java Mocking frameworks ? show Answer

Ans. Mockito, PowerMock, EasyMock, JMock, JMockit
Q12. What are the steps to be performed while coding Junit with Mocking framework ? show Answer

Ans. Initialize required objects for working with mocks and tested method
Set the mock behaviour on dependent objects
Execute the tested method
Perform assertions
Verify if a method is invoked or not
Q13. How to perform a clean install without executing Tests ? show Answer

Ans. mvn clean install -Dmaven.test.skip=true

or

mvn install -DskipTests

or


org.apache.maven.plugins
maven-surefire-plugin

true



within parent POM file.
Q14. Which of the following annotation is used to avoid executing Junits ?

a. @explicit
b. @ignore
c. @avoid
d. @NoTest
show Answer

Ans. @ignore
Q15. Which of the following is not the advantage of Mocking frameworks ?

a. It helps testing the module independently
b. It helps in faster unit testing
c. It helps in testing code even when external dependencies like service calls are not working
d. It helps in doing end to end Integration Testing
show Answer

Ans. It helps in doing end to end Integration Testing
Q16. Which of the following are usually manual ?

a. Unit Test
b. Integration Test
c. Load Test
d. Both a and c
show Answer

Ans. Integration Test
Q17. Which of the following nearly involves same Test execution plan ?

a. Unit and Integration tests
b. Unit and Regression Tests
c. Integration and Performance Tests
d. Performance and Load Tests
show Answer

Ans. Performance and Load Tests