Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Example of using Mockito with Junit Parameterized tests
package mani;
import static org.hamcrest.Matchers.equalTo;
import static;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;
import java.util.Arrays;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.mockito.Mock;
public class BradsParameterizedMockTest {
private UserManager userManager = new UserManagerImpl();
@Mock private UserService userService;
private String userName = null;
private User expectedUser = null;
public BradsParameterizedMockTest(String userName, User user) {
this.userName = userName;
this.expectedUser = user;
public static List<Object[]> balanceRates() {
return Arrays.asList(new Object[][] {
{"user1", new User("user1")},
{"user2", new User("user2")}
public void setUp() {
public void shouldReturnExpectedUser() {
// given
// when
User user = userManager.findUser(userName);
// then
assertThat(user, is(equalTo(expectedUser)));

This comment has been minimized.

Copy link

@ggorsontanguy ggorsontanguy commented Dec 31, 2015

Very useful! I could move forward quickly with this example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.