Easy Mock Interview Questions & Answers

  1. Question 1. What Are Junits ?

    Answer :

    Junt is the unit testing framework for Java.

  2. Question 2. Are Junits Tested Manually?

    Answer :

    No , they are executed automatically.

  3. Adv Java Interview Questions

  4. Question 3. How To Test Whether The Returns Value Of The Method Is Expected ?

    Answer :

    Using Assert.

  5. Question 4. Which Package Assert Belong To?

    Answer :

    java.unit

  6. Adv Java Tutorial

  7. Question 5. What Happens If The Assert Doesn’t Evaluate To Be True?

    Answer :

    Junit fails.

  8. J2EE Interview Questions

  9. Question 6. How To Create A Junit To Make Sure That The Tested Method Throws An Exception ?

    Answer :

    Using annotation Test with the argument as expected exception.
    @Test (expected = Exception.class)

  10. Question 7. What Should I Do If I Want To Make Sure That A Particular Method Of A Class Is Getting Called ?

    Answer :

    If its a static method of the class , we can use verify to make sure it’s 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.”

  11. J2EE Tutorial
    JSP Interview Questions

  12. Question 8. Name Few Java Mocking Frameworks?

    Answer :

    Mockito, PowerMock, EasyMock, JMock, JMockit.

  13. Question 9. What Is The Use Of Mockito.any?

    Answer :

    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.

  14. Adobe Photoshop Interview Questions

  15. Question 10. How Should We Ignore Or Avoid Executing Set Of Tests?

    Answer :

    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.

  16. JSP Tutorial

  17. Question 11. How Can We Test Methods Individually Which Are Not Visible Or Declared Private?

    Answer :

    We can either increase their visibility and mark them with annotation @VisibleForTesting or can use reflection to individually test those methods.

  18. Hibernate Interview Questions