How to capture variable parameters with Mockito?

Mockito 1.10.5 has introduced this feature. For the code sample in the question, here is one way to capture the varargs: ArgumentCaptor<String> varArgs = ArgumentCaptor.forClass(String.class); A mock = Mockito.mock(A.class); mock.setNames(“Jeff”, “Mike”, “John”); Mockito.verify(mock).setNames(varArgs.capture()); //Results may be validated thus: List<String> expected = Arrays.asList(“Jeff”, “Mike”, “John”); assertEquals(expected, varArgs.getAllValues()); Please see the ArgumentCaptor javadoc for details.

Cannot instantiate @InjectMocks field named exception with java class

if you do a employee = new Employee(param1, param2); you may as well skip @InjectMocks. It is supposed to do the following: @InjectMocks ClassUnderTest cut; @Mock Dependency1 dep1; @Mock Dependency2 dep2; @Before public void setup() { initMocks(this); } omitting @InjectMocks the same behaviour can be achieved with the following code: ClassUnderTest cut; @Mock Dependency1 dep1; … Read more

Throwing an exception from Mockito

Take a look at this reference in the official API. You want to reverse the way your call is made and adjust the argument too, since this is a void method that you expect to throw an exception. doThrow(new ContentIOException()).when(StubbedObject).putContent(contentStream);

mockito verify interactions with ArgumentCaptor

I recommend using Mockito’s Hamcrest integration to write a good, clean matcher for it. That allows you to combine the verification with detailed checking of the passed argument: import static org.mockito.hamcrest.MockitoHamcrest.argThat; verify(mock, times(1)).someMethod(argThat(personNamed(“Bob”))); Matcher<Person> personNamed(final String name) { return new TypeSafeMatcher<Person>() { public boolean matchesSafely(Person item) { return name.equals(item.getName()); } public void describeTo(Description description) { … Read more

How to mock a autowired list of Spring beans?

I finally figured it out… Sometimes, asking a question can give you a better approach to your problems :p The problem is I was linking the validators to the list before they were mocked. The validators was then null and no reference could be updated when the MockitAnnotations.initMocks(this) was called. Moreover, to avoid iterator problems … Read more

mockito return sequence of objects on spy method

You can chain doReturn() calls before when(), so this works (mockito 1.9.5): private static class Meh { public String meh() { return “meh”; } } @Test public void testMeh() { final Meh meh = spy(new Meh()); doReturn(“foo”).doReturn(“bar”).doCallRealMethod().when(meh).meh(); assertEquals(“foo”, meh.meh()); assertEquals(“bar”, meh.meh()); assertEquals(“meh”, meh.meh()); } Also, I didn’t know you could do when(x.y()).thenReturn(z1,z2), when I have … Read more

java.util.MissingResourceException: Can’t find bundle for base name javax.servlet.LocalStrings, locale es_ES

Caused by: java.util.MissingResourceException: Can’t find bundle for base name javax.servlet.LocalStrings, locale es_ES That’s the real error. Your running tests are missing the servlet-api dependency. If you’re using maven make sure this dependency is in your project: <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency>

Mockito mock objects returns null

It really depends on GeneralConfigService#getInstance() implementation. Also you can simplify your test code a lot if you use @InjectMocks annotation. When using MockitoJUnitRunner you don’t need to initialize mocks and inject your dependencies manually: @RunWith(MockitoJUnitRunner.class) public class GeneralConfigServiceImplTest { @InjectMocks private GeneralConfigService generalConfigService; @Mock private GeneralConfigDAO generalConfigDAO; @Test public void testAddGeneralConfigCallDAOSuccess() { // generalConfigService is … Read more

Mockito NotaMockException

As you noted, act is not a mock, and therefore you cannot record behavior on it. You could use Mockito.spy to, well, spy (or partially mock) the act object so that you only record the behavior of secondMethod and execute the actual code for firstMethod. Note, however, that matchers can’t be used in doReturn calls … Read more

Mockito throw Exception

Change this: thenThrow(DataAccessException.class) to thenThrow(new DataAccessException(“…”){ }) Example: when(userSubModuleDao.findById(any(UserSubModuleId.class),eq(false))).thenThrow(new DataAccessException(“…”){}); You can only pass a Class reference when that Exception type has a No-Arg constructor, and the Spring exception does not have one.

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)