ã¯ããã«
å 容ã¨ãã¦ã¯ãmockito ã¨ä½¿ã£ããstatic ã¡ã½ããã mock åããæ¹æ³ãã§ãã
Â
æ¥åã®ç°åã®é¢ä¿ã§ãJava ã®å©ç¨é »åº¦ã¯å°ãªããªãã¾ãããã
ã¿ã¤ãããããã¨ã¯å ±æãã¦ãããã¨æãã¾ãã
Â
static ã¡ã½ããã® mock åããã¢ããã¼ã·ã§ã³
mock åãããã®ã¯ãããã«ã¦ããããã¹ããæ¸ãéã«ãä¾åã¢ã¸ã¥ã¼ã«ã好ããªå¤ãããããmock/stub ã使ã£ã¦ãã¹ããããã¨ãªãã¨æãã¾ãã
Â
Java ã§å®è£ ãè¡ãå ´åãèªåãã¡ã§ static ã¡ã½ãããä½ãå ´åãããã¨æãã¾ãã
èªåãã¡ã§ä½ããªãå ´åã§ãå¤é¨ã¢ã¸ã¥ã¼ã«ã§å®è£ ããã static ã¡ã½ããã使ã£ããããå ´åãããã¨æãã¾ãã
ãããã£ããã®ã«ä¾åããã¯ã©ã¹ãéçºãããã¨ã«ãªã£ãå ´åã
ã¦ããããã¹ããæ¸ããã¨ããéãmock åããã«ã¯
ã©ã®ããã«ããã¨è¯ããããã¨ãããã¨ãèãã¾ãã
Â
powermock
æ¹æ³ã®ã²ã¨ã¤ã¨ãã¦ã powermock ã使ã£ããã¹ãã¨ããã®ãé¸æè¢ã«å ¥ã£ã¦ããã¨æãã¾ãã
ãã ãã©ãã JUnit5 ã®å¯¾å¿ãããããä½å¹´ãæéãçµéãã¦ãã¾ã£ã¦ããããã§ããã
â»é¡ä¼¼ã®å 容㮠issue ãä»ã«ãè¤æ°ããã£ã¦ãã¾ã
Â
JUnit4 ã§ããã° powermock 使ããã®ã§ããã
SpringBoot ã® test ã©ã¤ãã©ãªã¯ããã©ã«ã㧠JUnit5 ã«ãªã£ã¦ãããã
ãããã PowerMock 使ã£ã¦ãªããããªã¬ãã¸ããªã¼ã¯ãã§ã« JUnit5 ã使ã£ã¦ãããããã®ã§ã
powermock 使ãããã ãã« JUnit4 ã使ãã¨ããã®ã¯ãã¾ããããããªããªã¨æã£ã¦ãã¾ããã
Â
代æ¿æ¡ã¨ãã¦ã® mockito ã®ä½¿ç¨
ãã㧠JUnit5 ã§ä½¿ãã¦ãstatic ã¡ã½ããã mock åã§ããæ¹æ³ã¯ãªããã¨èª¿ã¹ãã¨ããã
mockito 3.4 以éã§ããã°ããããããã¨ãã§ããããã«ãªã£ã¦ãã¾ããã
å®éã«è©¦ããã¨ããããã¾ãåãããã¨ãã§ããã®ã§ããã£ãå 容ãè¨è¼ãã¦ããããã¨æãã¾ãã
Â
éçºç°å¢
éçºç°å¢ããéã«ä½¿ã£ãç°å¢ã«ã¤ãã¦ã
Java ãªã®ã§ãã¾ãéçºç°å¢ã«ã¯ä¾åããªãã¨æãã¾ããã
JDKããã«ããã¼ã«ã«ã¤ãã¦ãä¸å¿è¨è¼ãã¾ãã
ç°å¢ã¯ä»¥ä¸ã«ãªãã¾ãã
- OS: Windows 10 Pro
- IDE: IntelliJ IDEA 2021.1.1 (Ultimate Edition)
- Java JDK: openjdk-15.0.1
- ãã«ããã¼ã«: gradle-6.7
Â
å®è£ æ¹æ³
å®è£ æ¹æ³ã«ã¤ãã¦è¨è¼ãã¦ããã¾ãã
ããã¾ããªæµãã¯ä»¥ä¸ã®éãã§ãã
- ãã«ããã¼ã«ã®ä¾åé¢ä¿ã« mockito-inline ã追å ãã
- ãã¹ãã³ã¼ãã®ã¯ã©ã¹ä½æ
- static ã¡ã½ããããã¤ã¯ã©ã¹ã® mock å
- mock ã®æåå®ç¾©
- ãã¹ãã®å®è£
ï¼ããã¸ã§ã¯ãã®ä½æãããã¸ãã¯ã³ã¼ãå´ã¯ãã§ã«ãããã®ã¨æã£ã¦ãã¾ããï¼
mockito ã¢ã¸ã¥ã¼ã«ã®è¿½å
ãã«ããã¼ã«ï¼ä»å㯠gradle ï¼ã«ãå¿ è¦ãªã¢ã¸ã¥ã¼ã«ã追è¨ãã¾ãã
ç§ã®å ´å㯠build.gradle ã«ä»¥ä¸ãè¨è¼ãã¾ããã
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.0'
testImplementation 'org.mockito:mockito-inline:3.10.0'
testImplementation 'org.assertj:assertj-core:3.19.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'
}
test {
// Junit5 ã使ãè¨å®
useJUnitPlatform()
}
ã©ã¤ãã©ãªã®ãã¼ã¸ã§ã³ã¯å½æã®ææ°çã ã£ããããªãã¹ãæ°ããã®ãé¸ã³ã¾ããã
Â
mockito-inline ã¨ããã¢ã¸ã¥ã¼ã«ããå®éã« static ã¡ã½ããã® mock åãè¡ã£ã¦ããããããªã®ã§ããããåãè¾¼ãå¿ è¦ãããã¾ãã
mockito-core ã¯æå®ããªãã¦ããmockito-inline ã mockito-core ãå¼ã£å¼µã£ã¦ãããã®ã§è¨è¿°ããªãã¦ãåé¡ããã¾ããã
ï¼ä½ããã®çç±ã§ãã¼ã¸ã§ã³ã®ãªã¼ãã¼ã©ã¤ãã¨ããããã¦è¨è¿°ãã¦ãåä½ã¯ããã¨ãããã¾ããï¼
Â
ãã¹ãã³ã¼ãã®ã¯ã©ã¹ä½æ~ãã¹ãã®å®è£
å®éã®ãã¹ããå®è¡ããã¯ã©ã¹ãä½æãã¾ãã
åææ¡ä»¶ã§ããã§ã«ããã¸ã§ã¯ãã®ãã¸ãã¯ã³ã¼ãã¯åå¨ãããã¨ãã¾ãããã
ä»åã¯ä»¥ä¸ã®ãããªã¯ã©ã¹ãããã¨ãã¦ãã ããã
- static ã¡ã½ãããæã¤ã¯ã©ã¹ï¼StaticSampleï¼
- static ã¡ã½ãããæã¤ã¯ã©ã¹ã«ä¾åããã¯ã©ã¹ï¼UseStaticSampleï¼
- UseStaticSample ããã¹ãããããã®ãã¹ãã¯ã©ã¹ï¼UseStaticSampleTestï¼
Â
ãã¸ãã¯ã³ã¼ãã¨ãã¦ã¯ä»¥ä¸ãå®è£ ãã¾ãã
- StaticSample
import java.util.Objects;
public class StaticSample {
public static String value1() {
return "value1";
}
public static boolean isEqual(String value1, String value2) {
return !Objects.isNull(value1) && value1.equals(value2);
}
}
- UseStaticSample
public class UseStaticSample {
private final String value;
public UseStaticSample(String value) {
this.value = value;
}
public String function(String value) {
final boolean isEquals = StaticSample.isEqual(this.value, value);
if (isEquals) {
return String.format("Arg value is equal, value =[%s]", value);
} else {
return "Not Equal";
}
}
}
Â
å®éã«ããããã®ã¯ã©ã¹ããã¹ãããUseStaticSampleTest ã¯ä»¥ä¸ã®ããã«ãªãã¾ã
- UseStaticSampleTest
@SuppressWarnings("NonAsciiCharacters")
class UseStaticSampleTest {
private UseStaticSample target;
private static final String INIT_VALUE = "value";
@BeforeEach
void setUp() {
target = new UseStaticSample(INIT_VALUE);
}
@Test
final void test() {
final String argValue = "someValue";
final String expected = String.format("Arg value is equal, value =[%s]", argValue);
try(MockedStatic<StaticSample> mocked = mockStatic(StaticSample.class)) {
mocked.when(() -> StaticSample.isEqual(anyString(), anyString()))
.thenReturn(true);
String actual = target.function(argValue);
assertThat(actual).isEqualTo(expected);
}
}
}
Â
ãããå®è¡ããã¨ãUseStaticSample å ã§ä½¿ã£ã¦ãããStaticSample ã®ã¡ã½ããã®æåãæ¸ãæããã
ä»ã®ã¯ã©ã¹ã«ä¾åããªããã¹ãã³ã¼ããæ¸ããã¨ãã§ãã¾ããã
ãã¹ãã®å®è£ ã«ã¤ãã¦èª¬æ
ãã¹ãã®å®è£ ã¾ã§ã®å®è£ ããå 容ã«ã¤ãã¦ã触ãã¦ããããã¨æãã¾ãã
mock ã¤ã³ã¹ã¿ã³ã¹ã®çæã«ã¤ãã¦
org.mockito.MockedStatic ã¡ã½ããã§ãstatic ã¡ã½ãããæã¤ã¯ã©ã¹ã® mock ã¤ã³ã¹ã¿ã³ã¹ãçæãã¾ãã
MockedStatic<StaticSample> mocked = mockStatic(StaticSample.class)
mock ã¤ã³ã¹ã¿ã³ã¹ã®ã©ã¤ããµã¤ã¯ã«ã«ã¤ãã¦
mockStatic() ã«ãã£ã¦ mock ãçæããå ´åãæå¾ã« close() ãå¼ãã§mock ãåæ¢ãããå¿ è¦ãããã¾ãã
// create mock
MockedStatic<StaticSample> mocked = mockStatic(StaticSample.class);
mocked.when(() -> StaticSample.isEqual(anyString(), anyString()))
.thenReturn(true);
String actual = target.function(argValue);
assertThat(actual).isEqualTo(expected);
// close mock
mocked.close();
Â
ãããããªãã¨ãä»ã®ãã¹ã㧠mock åãããã¨ããéã«ã¨ã©ã¼ã¨ãªã£ã¦ãã¾ãããã§ãã
ãã ãä¸è¿°ãããããªãtry-with-resource ã®å½¢ã§ã
try ã§åãã¦ãããã°ãtry å¥ãæããéã«ãã£ããã« close ãã¦ããã¾ãã
// create mock
try(MockedStatic<StaticSample> mocked = mockStatic(StaticSample.class)) {
mocked.when(() -> StaticSample.isEqual(anyString(), anyString()))
.thenReturn(true);
String actual = target.function(argValue);
assertThat(actual).isEqualTo(expected);
// close mock
}
Â
mock ã®ãµãã¾ãã®å®ç¾©
mock ã®ãµãã¾ãã«ã¤ãã¦å®ç¾©ãã¾ãã
ããã¯é常㮠mock åããã mock ã¤ã³ã¹ã¿ã³ã¹ã¨åãè¦é ã§ãwhen ã thenReturn ã§æåã決ãã¦ãã¾ãã
ãã®æã«å®ç¾©ãããå¼ã³åºãããã¡ã½ããã¯ã©ã ãå¼ã§è¨è¿°ãã¾ãã
mocked.when(() -> StaticSample.isEqual(anyString(), anyString()))
.thenReturn(true);
â»StaticSample.isEqual() ã®ãµãã¾ãå®ç¾©ããã¹ã¦ã®æååãå¼æ°ã«åããã¨ãã« true ãè¿ãã¦ã»ããã
Â
ã©ã ãå¼ãªã®ã§ãå¼æ°ã®ãªãå ´åã¯ã¡ã½ããåç §ã®å½¢ã§è¨è¿°ãããã¨ãã§ãã¾ãã
å¼æ°ã®ãªããStaticSample.value1() ã®å ´åã¯ä»¥ä¸ã®ã©ã¡ããã«ãªãã¾ã
- ã¡ã½ããåç §ã®å ´å
mocked.when(StaticSample::value1)
.thenReturn("anyString");
- ã©ã ãå¼ã®å ´å
mocked.when(() -> StaticSample.value1())
.thenReturn("anyString");
Â
è£è¶³
mockito ã§ã³ã³ã¹ãã©ã¯ã¿ã® mock ããæ¹æ³ã«ã¤ãã¦ã以ä¸ã§èª¬æãã¾ããã
æå¾ã«
ãã¾ã¾ã§ static ã¡ã½ããã®ãã¹ãã³ã¼ãä½æã«ã¯çµæ§æ©ã¾ããã¦ãã¾ãããã
mockito 㧠static ã¡ã½ããã®ãã¹ããã§ããããã«ãªã£ã¦ããããããã§
ãã¹ãã©ã¤ãã©ãªã®ãã¼ã¸ã§ã³é¢ä¿ããã¹ãã®è¨è¿°æ¹æ³ã«é¢ããåé¡ã解æ¶ã§ããã®ã
é常ã«å¤§ããªåç©«ã«ãªãã¾ããã
Â
ãããã®è¨äºãè¦ãããæ¹ã§è©¦ãããã¨ã®ãªãæ¹ã¯æ¯éã試ãããã ããã°ã¨æãã¾ãã
Â
追è¨
ã³ã³ã¹ãã©ã¯ã¿ã Mockito 㧠mock ããæ¹æ³ã«ã¤ãã¦ãæ¸ãã¾ãã