The Mockito re-stub method has already been drowned out with thenthrow

I am having a problem with mockito. I am developing a web application. In my tests, the user manual is being mocked. There are some cases where I have to change the user returned by the getLoggedInUser() method.

The problem is that my getLoggedInUser() method can also raise an AuthenticationException .

Therefore, when I try to switch from one user to another user, the call

 when(userProvider.getLoggedInUser()).thenReturn(user); 

throws an exception because userProvider.getLoggedInUser() already wrapped with thenTrow()

Is there a way to tell the when method not to care about exceptions?

Thanks in advance - IstvΓ‘n

+7
java mockito mocking stubbing
source share
4 answers

My first reaction to your question is that it sounds like you are trying to do too much in one test.

For ease of testing and simplicity, each test should check only one thing. This is the same as the principle of shared responsibility . I often find programmers trying to test several things in one test, and they cause all kinds of problems. Therefore, each of your unit test methods should follow this thread:

  • Install one script for the test.
  • Calling the test class to run the test code.
  • Check the behavior.

So, in your case, I expect to see at least two tests. One where getLoggedInUser() returns the user, and one where getLoggedInUser() throws an exception. This way you will have no problem trying to simulate different behavior in the layout.

The second thought that spring is not to drown. Use wait instead, because you can set up a series of expectations. That is, the first call returns the user, the second call throws an exception, the third call returns another user, etc.

+2
source share

In newer versions of Mockito, you can use sequential calls to throw an exception at the first opportunity and return a value on the second call.

 when(mock.someMethod("some arg")) .thenThrow(new RuntimeException()) .thenReturn("foo"); 

http://docs.mockito.googlecode.com/hg/latest/org/mockito/Mockito.html#stubbing_consecutive_calls

+9
source share

It looks like you might have to use your own answer. Here is an example .

+2
source share

Is there a way to tell when a method doesn't care about exceptions?

To answer this question:

 import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; import static org.junit.Assert.assertThat; import static org.mockito.ArgumentMatchers.any; import static org.powermock.api.mockito.PowerMockito.mock; import static org.powermock.api.mockito.PowerMockito.when; import org.junit.Test; import org.mockito.Mockito; import java.util.ArrayList; public class MyTest { @Test public void testA() { // setup ArrayList<Object> list = mock(ObjectArrayList.class); when(list.indexOf(any())).thenReturn(6); when(list.indexOf(any())).thenReturn(12); // execute int index = list.indexOf(new Object()); // verify assertThat(index, is(equalTo(12))); } @Test public void testB() { // setup ArrayList<Object> list = mock(ObjectArrayList.class); when(list.add(any())).thenThrow(new AssertionError("can't get rid of me!")); when(list.add(any())).thenReturn(true); // execute list.add(new Object()); } @Test public void testC() { // setup ArrayList<Object> list = mock(ObjectArrayList.class); when(list.add(any())).thenThrow(new AssertionError("can't get rid of me!")); Mockito.reset(list); when(list.add(any())).thenReturn(true); // execute list.add(new Object()); } /** * Exists to work around the fact that mocking an ArrayList<Object> * requires a cast, which causes "unchecked" warnings, that can only be suppressed... */ class ObjectArrayList extends ArrayList<Object> { } } 

TestB fails due to the statement that you cannot get rid. TestC shows how the reset method can be used to reset mock and remove the thenThrow command on it.

Please note that reset does not always work in some of the more complex examples that I have. I suspect this could be because they use PowerMockito.mock and not Mockito.mock ?

+1
source share

All Articles