February 8, 2018

Sreekanth B

Easy Mock Most Common Interview Questions Answers


What Are Junits ?

Junt is the unit testing framework for Java.

How To Create A Junit To Make Sure That The Tested Method Throws An Exception ?

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

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

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


How Should We Ignore Or Avoid Executing Set Of Tests?

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.

How Can We Test Methods Individually Which Are Not Visible Or Declared Private?

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

Name Few Java Mocking Frameworks?

Mockito, PowerMock, EasyMock, JMock, JMockit.

What Is The Use Of Mockito.any?

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.

Are Junits Tested Manually?

No , they are executed automatically.

How To Test Whether The Returns Value Of The Method Is Expected ?

Using Assert.

Which Package Assert Belong To?

java.unit

What Happens If The Assert Doesn't Evaluate To Be True?

Junit fails.


Subscribe to get more Posts :