Last active
August 27, 2023 09:22
-
-
Save bjacques/11064527 to your computer and use it in GitHub Desktop.
Example of using Mockito with Junit Parameterized tests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package mani; | |
import static org.hamcrest.Matchers.equalTo; | |
import static org.hamcrest.Matchers.is; | |
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; | |
@RunWith(Parameterized.class) | |
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; | |
} | |
@Parameters | |
public static List<Object[]> balanceRates() { | |
return Arrays.asList(new Object[][] { | |
{"user1", new User("user1")}, | |
{"user2", new User("user2")} | |
}); | |
} | |
@Before | |
public void setUp() { | |
initMocks(this); | |
userManager.setUserService(userService); | |
} | |
@Test | |
public void shouldReturnExpectedUser() { | |
// given | |
when(userService.findUserByName(userName)).thenReturn(expectedUser); | |
// when | |
User user = userManager.findUser(userName); | |
// then | |
assertThat(user, is(equalTo(expectedUser))); | |
verify(userService).findUserByName(userName); | |
verifyNoMoreInteractions(userService); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Very useful! I could move forward quickly with this example.