JUnit - Interview Questions and Answers for 'JUnit' | Search Interview Question - javasearch.buggybread.com
Javasearch.buggybread.com

Search Interview Questions


 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.
Label / Company      Label / Company / Text

   



JUnit - Interview Questions and Answers for 'JUnit' - 28 question(s) found - Order By Rating

 Q1. What does this warning message for the unit test means

WARN: 'Spec has no expectations.'
Angular
 This question is still unanswered. Can you please provide an answer.


 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     Junit   Jasmine   Karma


 Q2. Write a unit test case using java 8.JUnit
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 :   

   Like         Discuss         Correct / Improve     java8  java 8     Asked in 1 Companies


 Q3. Can we pass the junit test if the tested method throws the exception ? and Should we do that ?Junit
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 :   

   Like         Discuss         Correct / Improve     Junit exception testing


 Q4. What are the points to consider while reviewing junits ?Junit
 This question is still unanswered. Can you please provide an answer.


 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     


 Q5. How do you test object construction or constructors using junit ? Junit
 This question is still unanswered. Can you please provide an answer.


 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     


 Q6. How to find if any employees address is null or not during Junit testingJUnit
Ans. By using assertNotNull(employee_address)

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve          Asked in 1 Companies


 Q7. What is JUnitRunner ?JUnit
 This question was recently asked at 'Barclays'.This question is still unanswered. Can you please provide an answer.


 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve          Asked in 1 Companies


 Q8. What is a difference between JUnit 3 and 4 ?Junit
 This question was recently asked at 'Conde Nast'.This question is still unanswered. Can you please provide an answer.


 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     testing  junit     Asked in 1 Companies


 Q9. What is doCallRealMethod() method used for ?PowerMock
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 :   

   Like         Discuss         Correct / Improve     junit  mocking  java unit testing  Mockito


 Q10. What are the benefits of Junits ?Junit
 This question was recently asked at 'Cognizant ( CTS )'.This question is still unanswered. Can you please provide an answer.


 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve          Asked in 1 Companies


 Q11. Can you name few matchers that can be used with hamcrest assertThat method ?Junit
Ans. is
not
equals
anyOf
hasSize

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     assertThat.hamcrest


 Q12. What are the benefits of assertThat over assert in Junits ?Junit
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 :   

   Like         Discuss         Correct / Improve     assertThat  assert  assert vs assertThat


 Q13. Does a Junit without any assertions makes any sense ?Junit
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 :   

   Like         Discuss         Correct / Improve     junit  assert


 Q14. Have you ever tried mocking static methods ?Testing
Ans. Yes, that can be done using Power Mock. Mockito doesnt provide a way to mock static methods.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     Mockito  junit  powermock     Asked in 1 Companies


 Q15. What is the use of @Before annotation ?Junit
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 :   

   Like         Discuss         Correct / Improve     


 Q16. Write a Test Case using Mock ?Testing
Ans. http://javasearch.buggybread.com/CodeSnippets/searchCodeSamples.php?keyword=mock&category=code

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     Junit  Mock     Asked in 1 Companies


 Q17. 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 :   

   Like         Discuss         Correct / Improve     java   junit.assert   testing   white box testing


 Q18. Difference between Assert and Verify ?Testing
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 :   

   Like         Discuss         Correct / Improve     assert   junit   mockito   verify   testing   unit testing     Asked in 5 Companies


 Q19. 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 :   

   Like         Discuss         Correct / Improve     java   junit   jnuit4   junit annotations


 Q20. How to create a Junit to make sure that the tested method throws an exception ?
Ans. Using annotation Test with the argument as expected exception.

@Test (expected = Exception.class)

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     junit   junit annotations


 Q21. What should I do if I want to make sure that a particular method of a class is getting called ?
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.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     junit   mockito   mock


 Q22. What is the use of Mockito.any ?
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 :   

   Like         Discuss         Correct / Improve     junit   mockito   mock   mockito.any


 Q23. 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 :   

   Like         Discuss         Correct / Improve     junit   @test   @ignore   unit tests   unit testing   testing


 Q24. 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 :   

   Like         Discuss         Correct / Improve     junit   reflection api   @visiblefortesting   white box tester


 Q25. Name few Java Mocking frameworks ?
Ans. Mockito, PowerMock, EasyMock, JMock, JMockit

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     junit   mocking frameworks   mock   architecture   white box testing


 Q26. What are the steps to be performed while coding Junit with Mocking framework ?JUnit
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 :   

   Like         Discuss         Correct / Improve     junit   mocking frameworks   mock   unit testing   java   white box testing  Mockito


 Q27. Which of the following annotation is used to avoid executing Junits ?

a. @explicit
b. @ignore
c. @avoid
d. @NoTest
Ans. @ignore

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   testing   junit   unit testing


 Q28. 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 :   

   Like         Discuss         Correct / Improve     mocking frameworks   mockito   unit testing   junits


 Q29. Which of following annotation is used to initialize objects before executing set of tests ?Junit
a. @Test
b. @Ignore
c. @After
d. @Before

Ans.d. @Before


Help us and Others Improve. Please let us know the questions asked in any of your previous interview.

Any input from you will be highly appreciated and It will unlock the application for 10 more requests.

Company Name:
Questions Asked: