Java - Interview Questions and Answers for 'Junit' | Search Java Interview Question - javasearch.buggybread.com
Javasearch.buggybread.com
Share

Search Java Interview Questions


 2098 questions in repository.
 There are more than 200 unanswered questions.
Click here and help us by providing the answer.
Label / Company      Label / Company / Text

   



Junit


   




Interview Questions and Answers for 'Junit' - 19 question(s) found - Order By Newest

 Q1. How to create a Junit to make sure that the tested method throws an exception ?
admin
info@buggybread.com
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


 Q2. How should we ignore or avoid executing set of tests ?
admin
info@buggybread.com
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


 Q3. How can we test methods individually which are not visible or declared private ?
admin
info@buggybread.com
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


 Q4. What are the steps to be performed while coding Junit with Mocking framework ?
admin
info@buggybread.com
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


 Q5. What is assert keyword used for ?
Anonymous
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


 Q6. Difference between Assert and Verify ?
admin
info@buggybread.com
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   barclays


 Q7. What are the annotations used in Junit with Junit4 ?
Admin
info@buggybread.com
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


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


 Q9. What is the use of Mockito.any ?
admin
info@buggybread.com
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



Do you think these are the Best Java Frameworks ?

OpenXavaSPRING MVCApache StripesCheck everything
that is Best in Java

Click Here



 Q10. Name few Java Mocking frameworks ?
admin
info@buggybread.com
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


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

a. @explicit
b. @ignore
c. @avoid
d. @NoTest
Anonymous
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


 Q12. 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
Anonymous
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


 Q13. Write a Test Case using Mock ?Testing2016-11-04 08:13:38

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     CapitalOne


 Q14. What is the use of @Before annotation ?Junit2016-11-22 11:18:43

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     


 Q15. Have you ever tried mocking static methods ?Testing2016-12-07 16:13:30

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


 Q16. Does a Junit without any assertions makes any sense ?Junit2017-05-09 13:34:52

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


 Q17. What are the benefits of assertThat over assert in Junits ?Junit2017-05-17 16:50:18

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


 Q18. Can you name few matchers that can be used with hamcrest assertThat method ?Junit2017-05-17 16:52:13

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


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



Subscribe to Java News and Posts. Get latest updates and posts on Java from Buggybread.com
Enter your email address:
Delivered by FeedBurner



comments powered by Disqus
 

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:
         

X Close this

X Close this

Help Us Improve.
Please share your
interview experience.

Company Name:   


Questions Asked: