Last active
February 12, 2016 13:12
-
-
Save kazurof/eb6a17fdd22ee4318cc3 to your computer and use it in GitHub Desktop.
mockitoとJMockitについてのメモ ref: http://qiita.com/kazurof/items/1171c7e038050453c6c9
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
package org.example; | |
public class Calc { | |
int remainder(int dividend, int divisor) { | |
return dividend % divisor; | |
} | |
} |
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
package org.example; | |
public class Logic { | |
Calc calc; | |
Logic(Calc calc) { | |
this.calc = calc; | |
} | |
/**割り切れる数字のペアをアトランダムに返す。*/ | |
int[] getDivisibleNumbers() { | |
while (true) { | |
int candidate = (int) (Math.random() * 100) + 1; | |
int candidate2 = (int) (Math.random() * 10) + 1; | |
if (calc.remainder(candidate, candidate2) == 0) { | |
return new int[]{candidate, candidate2}; | |
} | |
} | |
} | |
} |
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
package org.example; | |
import mockit.Expectations; | |
import mockit.Mocked; | |
import mockit.Verifications; | |
import mockit.integration.junit4.JMockit; | |
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
import static org.junit.Assert.assertTrue; | |
@RunWith(JMockit.class) | |
public class LogicTestInJMockit { | |
@Test | |
public void testAsUsual() throws Exception { | |
Logic logic = new Logic(new Calc()); | |
int[] result = logic.getDivisibleNumbers(); | |
assertTrue(result[0] % result[1] == 0); | |
} | |
@Test | |
public void testInJMockit(@Mocked Calc calc) throws Exception { | |
new Expectations() {{ | |
calc.remainder(anyInt, 5); | |
result = 0; | |
calc.remainder(anyInt, anyInt); | |
result = 1; | |
}}; | |
Logic logic = new Logic(calc); | |
int[] result = logic.getDivisibleNumbers(); | |
new Verifications() {{ | |
calc.remainder(anyInt, 5); | |
minTimes = 1; | |
}}; | |
assertTrue(result[1] == 5); | |
} | |
} |
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
package org.example; | |
import mockit.Mock; | |
import mockit.MockUp; | |
import org.junit.Test; | |
import static org.junit.Assert.assertTrue; | |
public class LogicTestInJMockitMockups { | |
// このテストが、testInJMockitWithMockups() の後に実行されると | |
// モックしていないCalcを使っているつもりがモックされたものになってしまう様子。 | |
@Test | |
public void testAsUsual() throws Exception { | |
Logic logic = new Logic(new Calc()); | |
int[] result = logic.getDivisibleNumbers(); | |
assertTrue(result[0] % result[1] == 0); | |
} | |
@Test | |
public void testInJMockitWithMockups() throws Exception { | |
new MockUp<Calc>() { | |
@Mock() | |
public int remainder(int dividend, int divisor) { | |
return divisor == 5 ? 0 : 1; | |
} | |
}; | |
Logic logic = new Logic(new Calc()); | |
int[] result = logic.getDivisibleNumbers(); | |
assertTrue(result[1] == 5); | |
} | |
} |
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
package org.example; | |
import org.junit.Before; | |
import org.junit.Test; | |
import org.mockito.InjectMocks; | |
import org.mockito.Mock; | |
import org.mockito.MockitoAnnotations; | |
import static org.junit.Assert.assertTrue; | |
import static org.mockito.Mockito.anyInt; | |
import static org.mockito.Mockito.when; | |
public class LogicTestInMockito { | |
@Mock | |
Calc calc; | |
@InjectMocks | |
Logic logic; | |
@Before | |
public void before() { | |
MockitoAnnotations.initMocks(this); | |
} | |
@Test | |
public void testAsUsual() throws Exception { | |
logic.calc = new Calc(); | |
int[] result = logic.getDivisibleNumbers(); | |
assertTrue(result[0] % result[1] == 0); | |
} | |
@Test | |
public void testInMockito() throws Exception { | |
when(calc.remainder(anyInt(), anyInt())).thenAnswer(i -> { | |
// 公式ドキュメントには書いてありませんが、ここはラムダが使えます。 | |
return ((Integer) i.getArguments()[1]) == 5 ? 0 : 1; | |
}); | |
int[] result = logic.getDivisibleNumbers(); | |
assertTrue(result[1] == 5); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment