More than 3000 questions in repository. There are more than 900 unanswered questions. Click here and help us by providing the answer. Have a video suggestion. Click Correct / Improve and please let us know.
Q2. 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
Ans. It helps in doing end to end Integration Testing
Help us improve. Please let us know the company, where you were asked this question :
Q4. How should we ignore or avoid executing set of tests ?
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.
Help us improve. Please let us know the company, where you were asked this question :
LikeDiscussCorrect / Improve  junit   @test   @ignore   unit tests   unit testing   testing
Q5. How can we test methods individually which are not visible or declared private ?
Ans. We can either increase their visibility and mark them with annotation @VisibleForTesting or can use reflection to individually test those methods.
Help us improve. Please let us know the company, where you were asked this question :
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
Help us improve. Please let us know the company, where you were asked this question :
LikeDiscussCorrect / Improve  junit   mocking frameworks   mock   unit testing   java   white box testing  Mockito
Q7. What is assert keyword used for ?
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.
Help us improve. Please let us know the company, where you were asked this question :
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.
Help us improve. Please let us know the company, where you were asked this question :
Q9. What are the annotations used in Junit with Junit4 ?
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.
Help us improve. Please let us know the company, where you were asked this question :
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.
Help us improve. Please let us know the company, where you were asked this question :
Ans. When executing tests it is common that multiple tests need similar objects to be created before they can run. @before specifies a method that provide that initialization for the set of Unit Tests.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Yes, If we are testing a code segment to check if throws / doesn't throw an exception. Moreover while testing void methods, we may just verify and not use assert.
Help us improve. Please let us know the company, where you were asked this question :
Ans. assertThat is introduced with Junit4 and offers many advatanges over assert. Messages are more explanatory, offer better type safety and are more readable. Moreover hamcrest library is portable as it can used both with junit and TestNG.Moreover assertThat provides flexibility as the same method can be used for assert , assertEquals,assertTrue etc through the use of matcher methods.
Help us improve. Please let us know the company, where you were asked this question :
Ans. When we Mock an object and then make a reference to any method using mocked object reference , java never makes a call to that method and looks for mocked value to be returned or null if none specified. But If we want that to be overridden and want java to make actual method call upon using mocked object reference, this method can be used.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Yes, We can pass the test even if the tested method throws an exception.
One way is know as exception testing in which the tested method is expected to throw exception and hence the expectation is set accordingly.
@Test(expected = IndexOutOfBoundsException.class)
public void test(){}
Other way is to just ignore if the tested code throws an exception. In that case , we can enclose the tested method call in the try block and within exception block, we just return gracefully.
Exception testing is perfectly ok to be done as we would like test code in case an exception occurs and if the code is handling the exception properly. Ignoring the exception by putting the tested method call within try block isn't something normal and should be avoided.
Help us improve. Please let us know the company, where you were asked this question :
Ans. public List convertAllToUpperCase(List words) {
return words.stream().map(String::toUpperCase).collect(Collectors.toList());
}
@Test
public void testAllToUpperCase() {
List expected = Arrays.asList("JAVA8", "STREAMS");
List result = convertAllToUpperCase(Arrays.asList("java8", "streams"));
assertEquals(expected, result);
}
Help us improve. Please let us know the company, where you were asked this question :