Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
An example of stubbing with Mockito (with TestNG)
package com.pillartechnology.mail;
public class AddressInputQueue {
public String next() {
return null; //TODO: not implemented yet.
}
}
package com.pillartechnology.mail;
import static java.util.Arrays.*;
import java.util.List;
public class AddressSplitter {
private AddressInputQueue addressInputQueue;
public List<String> split() {
return asList(addressInputQueue.next().split(","));
}
}
package com.pillartechnology.mail;
import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;
import static org.mockito.Mockito.*;
import java.util.List;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class AddressSplitterTest {
@InjectMocks private AddressSplitter subject = new AddressSplitter();
@Mock private AddressInputQueue addressInputQueue;
@BeforeMethod(alwaysRun=true)
public void injectDoubles() {
MockitoAnnotations.initMocks(this); //This could be pulled up into a shared base class
}
@Test
public void splitsAddressesByComma() {
when(addressInputQueue.next()).thenReturn("jim@weirich.com,kent@beck.com");
List<String> result = subject.split();
assertThat(result,hasItems("jim@weirich.com","kent@beck.com"));
}
}
@christopinka

This comment has been minimized.

Copy link

@christopinka christopinka commented Jan 8, 2013

Can you include the versions of the tools you are using? Thanks

@dklotz

This comment has been minimized.

Copy link

@dklotz dklotz commented Jul 15, 2014

Be aware that TestNG does not recreate your test class for multiple test methods in the same test class (and, unfortunately, Mockito wont recreate the @InjecktMocks objects if the instances are not null), which will lead to your mocks / tested objects not being recreated for different test methods, even though the initMocks call is in a @BeforeMethod-annotated method. I know this example only has one test method, but people using this as a starting point will quickly fall for this. See also: https://code.google.com/p/mockito/issues/detail?id=304

@activedecay

This comment has been minimized.

Copy link

@activedecay activedecay commented Jan 26, 2017

When I was using mockito without @mock, the verify() calls would count up for every @test annotated method. The only wayw to fix this problem was to use this gist, or new up the mock in the @BeforeMethod (which didn't seem reasonable). I like this default behavior better, where the mock isn't expected to persist state from previous tests.

@sriavr

This comment has been minimized.

Copy link

@sriavr sriavr commented Jul 3, 2019

Thank you!

@prasgit

This comment has been minimized.

Copy link

@prasgit prasgit commented Sep 24, 2019

Thanks, very helpful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment