Created
June 7, 2016 02:31
-
-
Save mike-neck/1fc0c4ada25fd09f472506237713256f to your computer and use it in GitHub Desktop.
テストでstaticメソッドに渡された引数をキャプチャーする
This file contains 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
import org.junit.Before; | |
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
import org.mockito.ArgumentCaptor; | |
import org.powermock.core.classloader.annotations.PrepareForTest; | |
import org.powermock.modules.junit4.PowerMockRunner; | |
import java.util.HashMap; | |
import java.util.Map; | |
import static org.junit.Assert.assertEquals; | |
import static org.junit.Assert.assertNotNull; | |
import static org.mockito.Matchers.any; | |
import static org.powermock.api.mockito.PowerMockito.doReturn; | |
import static org.powermock.api.mockito.PowerMockito.mock; | |
import static org.powermock.api.mockito.PowerMockito.mockStatic; | |
import static org.powermock.api.mockito.PowerMockito.verifyStatic; | |
@RunWith(PowerMockRunner.class) | |
@PrepareForTest({Static.class}) | |
public class ArgumentCaptorSample { | |
private final Map<String, String> map = new HashMap<String, String>(){{ | |
put("name", "test"); | |
put("value", "argument captor for static method."); | |
}}; | |
@Before | |
public void setup() { | |
mockStatic(Static.class); | |
} | |
@SuppressWarnings("unchecked") | |
@Test | |
public void sample() throws Exception { | |
doReturn(mock(Static.class)).when(Static.class, "create", any(Map.class)); | |
Static obj = Static.create(map); | |
verifyStatic(); | |
ArgumentCaptor<Map> captor = ArgumentCaptor.forClass(Map.class); | |
Static.create(captor.capture()); | |
Map<String, String> arg = captor.getValue(); | |
assertNotNull(obj); | |
assertEquals(map, arg); | |
} | |
} |
This file contains 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
import java.util.Map; | |
import java.util.StringJoiner; | |
public class Static { | |
private final String name; | |
private final String value; | |
private Static(String name, String value){ | |
this.name = name; | |
this.value = value; | |
} | |
public String getName() { | |
return name; | |
} | |
public String getValue() { | |
return value; | |
} | |
@Override | |
public String toString() { | |
return new StringJoiner(", ", "Static:[", "]") | |
.add("name: [" + (name == null ? "null" : name) + "]") | |
.add("value: [" + (value == null ? "null" : value) + "]") | |
.toString(); | |
} | |
public static Static create(Map<String, String> map) { | |
String name = map.getOrDefault("name", ""); | |
String value = map.getOrDefault("value", ""); | |
return new Static(name, value); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment