Skip to content

Instantly share code, notes, and snippets.

@ValeriusGC
Last active March 5, 2018 04:43
Show Gist options
  • Save ValeriusGC/8a0f65f21661295c2f26cb1d12e61307 to your computer and use it in GitHub Desktop.
Save ValeriusGC/8a0f65f21661295c2f26cb1d12e61307 to your computer and use it in GitHub Desktop.
This is how one can use parameterized tests along with Mockito to provide cyclic tests of Event Handlers
package com.gdetotut.login;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.mockito.ArgumentCaptor;
import java.util.Arrays;
import java.util.Collection;
import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
/**
* This is cyclic test with parameters. So we use parameterized concept.
*/
@RunWith(Parameterized.class)
public class VfxSetUserTest {
private int index;
private VfxUser.State state;
public VfxSetUserTest(int index, VfxUser.State state) {
this.index = index;
this.state = state;
}
// {name} is optional just for the logging output.
@Parameterized.Parameters(name = "{index}: param = {0}, res = {1}")
public static Collection getParameters() {
return Arrays.asList(new Object[][]{
{0, VfxUser.State.US_GodMode},
{1, VfxUser.State.US_Disp},
{2, VfxUser.State.US_Admin},
{3, VfxUser.State.US_NotReg},
{10, VfxUser.State.US_NotReg},
{-1, VfxUser.State.US_NotReg}
});
}
/**
* We set various parameter for {@link VfxModel#setUser} and await parameterized result.
*/
@Test
public void testModel() {
VfxModel service = VfxModel.INSTANCE;
VfxModel.OnEventListener event = mock(VfxModel.OnEventListener.class);
service.setListener(event);
service.setUser(index);
ArgumentCaptor<VfxUser> captor = ArgumentCaptor.forClass(VfxUser.class);
verify(event).onUserChanged(captor.capture(), eq(""));
VfxUser captured = captor.getValue();
assertEquals(state, captured.getState());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment