本文整理汇总了Java中org.mockito.Mock类的典型用法代码示例。如果您正苦于以下问题:Java Mock类的具体用法?Java Mock怎么用?Java Mock使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Mock类属于org.mockito包,在下文中一共展示了Mock类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testGetEnergyStatisticsEmptyEnergyList
import org.mockito.Mock; //导入依赖的package包/类
@Test
public void testGetEnergyStatisticsEmptyEnergyList() throws Exception {
Random rand = new Random();
int randomNumberOfDays = rand.nextInt(10) + 1;;
ZonedDateTime startDate = ZonedDateTime.now();
ZonedDateTime endDate = startDate.plusDays(randomNumberOfDays);
new MockUp<AnalyticsServiceImpl>() {
@mockit.Mock
List<AnalyticsServiceImpl.Energy> getEnergyList (String start, String end) {
List<AnalyticsServiceImpl.Energy> emptyList = new ArrayList<>();
return emptyList;
}
};
List<Double> expected = new ArrayList<>();
for (int i = 0; i < randomNumberOfDays + 1; i++) {
expected.add(0.00);
}
List<Double> actual = analyticsService.getEnergyStatistics(startDate, endDate);
assertEquals(expected.size(), actual.size());
assertTrue(expected.equals(actual));
}
示例2: turnMeterGetsIncreasedPerTurnBySpeedOfBot
import org.mockito.Mock; //导入依赖的package包/类
@Test
@DisplayName("increases per turn by the speed of the bot")
void turnMeterGetsIncreasedPerTurnBySpeedOfBot(@Mock UserInterface ui) {
Bot bot1 = aBot().withSpeed(30).build();
Bot bot2 = aBot().withSpeed(45).build();
game = new Game(ui, aPlayer().withTeam(bot1, bot2, anyBot()).build(), anyPlayer());
game.turn();
assertEquals(30, bot1.getTurnMeter());
assertEquals(45, bot2.getTurnMeter());
game.turn();
assertEquals(60, bot1.getTurnMeter());
assertEquals(90, bot2.getTurnMeter());
}
示例3: botAttacksWhenMakingMove
import org.mockito.Mock; //导入依赖的package包/类
@Test
@DisplayName("is performed when a bot makes its move")
void botAttacksWhenMakingMove(@Mock UserInterface ui) {
Bot bot = aBot().withSpeed(500).build();
Bot opponent = aBot().withIntegrity(100).build();
when(ui.selectTarget(eq(bot), anyListOf(Bot.class))).thenReturn(Optional.of(opponent));
game = new Game(ui, aPlayer().withTeam(bot, anyBot(), anyBot()).build(),
aPlayer().withTeam(opponent, anyBot(), anyBot()).build());
game.turn();
assertEquals(100, opponent.getIntegrity(), "Bot should not attack in first turn");
game.turn();
assertTrue(opponent.getIntegrity() < 100, "Bot should attack and damage opponent in second turn");
}
示例4: botAttacksOnlyTheSelectedTarget
import org.mockito.Mock; //导入依赖的package包/类
@Test
@DisplayName("is only performed against the selected target")
void botAttacksOnlyTheSelectedTarget(@Mock UserInterface ui) {
Bot bot = aBot().withSpeed(1000).build();
Bot opponent1 = aBot().withIntegrity(100).build();
Bot opponent2 = aBot().withIntegrity(100).build();
Bot opponent3 = aBot().withIntegrity(100).build();
when(ui.selectTarget(eq(bot), anyListOf(Bot.class))).thenReturn(Optional.of(opponent1));
game = new Game(ui, aPlayer().withTeam(bot, anyBot(), anyBot()).build(),
aPlayer().withTeam(opponent1, opponent2, opponent3).build());
game.turn();
assertAll(
() -> assertTrue(opponent1.getIntegrity() < 100),
() -> assertTrue(opponent2.getIntegrity() == 100, "Opponent 2 should not be attacked"),
() -> assertTrue(opponent3.getIntegrity() == 100, "Opponent 3 should not be attacked")
);
}
示例5: botDestroyedFromAttackIsRemovedFromTeam
import org.mockito.Mock; //导入依赖的package包/类
@Test
@DisplayName("destroying a bot gets it removed from its team")
void botDestroyedFromAttackIsRemovedFromTeam(@Mock UserInterface ui) {
Bot bot = aBot().withPower(100).withSpeed(1000).build();
Bot opponent = aBot().withIntegrity(1).build();
when(ui.selectTarget(eq(bot), anyListOf(Bot.class))).thenReturn(Optional.of(opponent));
game = new Game(ui, aPlayer().withTeam(bot, anyBot(), anyBot()).build(),
aPlayer().withTeam(opponent, anyBot(), anyBot()).build());
assertEquals(3, opponent.getOwner().getTeam().size());
game.turn();
assertAll(
() -> assertEquals(2, opponent.getOwner().getTeam().size()),
() -> assertFalse(opponent.getOwner().getTeam().contains(opponent))
);
}
示例6: botDestroyedFromContinuousDamageWillBeRemovedBeforeItsAttack
import org.mockito.Mock; //导入依赖的package包/类
@Test
@DisplayName("that destroys its target will have that bot removed from its team before it can attack")
void botDestroyedFromContinuousDamageWillBeRemovedBeforeItsAttack(@Mock UserInterface ui) {
Effect continuousDamage = createEffectFactoryFor(aBot().withPower(9999).build(),
1, ContinuousDamage.class).newInstance();
Bot bot = aBot().withIntegrity(1).withStatusEffects(continuousDamage).withPower(100).withSpeed(1000).build();
Bot target = aBot().withIntegrity(100).build();
when(ui.selectTarget(eq(bot), anyListOf(Bot.class))).thenReturn(Optional.of(target));
Game game = new Game(ui, aPlayer().withTeam(bot, anyBot(), anyBot()).build(),
aPlayer().withTeam(target, anyBot(), anyBot()).build());
game.turn();
assertEquals(100, target.getIntegrity(),
"Bot should have been destroyed by Continuous Damage before is had a chance to attack");
}
示例7: stunnedBotMissesNextMove
import org.mockito.Mock; //导入依赖的package包/类
@Test
@DisplayName("lets the affected bot miss its next move")
void stunnedBotMissesNextMove(@Mock UserInterface ui) {
Effect effect = createEffectFactoryFor(anyBot(),
1, Stun.class).newInstance();
Bot stunnedBot = aBot().withSpeed(1000).withStatusEffects(effect).build();
Bot target = aBot().withIntegrity(100).build();
when(ui.selectTarget(eq(stunnedBot), anyListOf(Bot.class))).thenReturn(Optional.of(target));
Game game = new Game(ui,
aPlayer().withTeam(stunnedBot, anyBot(), anyBot()).build(),
aPlayer().withTeam(target, anyBot(), anyBot()).build());
game.turn();
assertEquals(100, target.getIntegrity(), "Stunned bot should not damage opponent bot");
assertEquals(0, stunnedBot.getEffects().size());
}
示例8: cannotCreateGameWithIncompleteTeamSetup
import org.mockito.Mock; //导入依赖的package包/类
@SuppressWarnings("ThrowableResultOfMethodCallIgnored")
@Test
@DisplayName("when a player has a team of less than 3 bots")
void cannotCreateGameWithIncompleteTeamSetup(@Mock UserInterface ui) {
Player playerWithCompleteTeam = aPlayer().withTeam(anyBot(), anyBot(), anyBot()).build();
Player playerWithIncompleteTeam = aPlayer().withTeam(anyBot(), anyBot()).build();
Throwable exception = expectThrows(IllegalArgumentException.class,
() -> new Game(ui, playerWithCompleteTeam, playerWithIncompleteTeam));
assertAll(
() -> assertTrue(exception.getMessage().contains(playerWithIncompleteTeam.toString())),
() -> assertFalse(exception.getMessage().contains(playerWithCompleteTeam.toString()))
);
}
示例9: cannotCreateGameWithDuplicateBotInTeam
import org.mockito.Mock; //导入依赖的package包/类
@SuppressWarnings("ThrowableResultOfMethodCallIgnored")
@Test
@DisplayName("when a player has the same bot twice in his team")
void cannotCreateGameWithDuplicateBotInTeam(@Mock UserInterface ui) {
Bot duplicateBot = anyBot();
Player playerWithDuplicateBotInTeam = aPlayer().withTeam(duplicateBot, duplicateBot, anyBot()).build();
Player playerWithValidTeam = aPlayer().withTeam(anyBot(), anyBot(), anyBot()).build();
Throwable exception = expectThrows(IllegalArgumentException.class,
() -> new Game(ui, playerWithValidTeam, playerWithDuplicateBotInTeam));
assertAll(
() -> assertTrue(exception.getMessage().contains(playerWithDuplicateBotInTeam.toString())),
() -> assertTrue(exception.getMessage().contains(duplicateBot.toString())),
() -> assertFalse(exception.getMessage().contains(playerWithValidTeam.toString()))
);
}
示例10: configure
import org.mockito.Mock; //导入依赖的package包/类
@Override
protected void configure() {
bindFactory(new InstanceFactory<User>(test.getCurrentUser())).to(User.class);
bindFactory(new InstanceFactory<Env>(test.getEnv())).to(Env.class);
for (Field field : fields) {
if (field.isAnnotationPresent(Mock.class)) {
try {
field.setAccessible(true);
bindFactory(new InstanceFactory<Object>(field.get(test))).to(field.getType());
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
}
示例11: printInvocationsOnAllMockedFields
import org.mockito.Mock; //导入依赖的package包/类
default void printInvocationsOnAllMockedFields() {
new MockitoDebuggerImpl()
.printInvocations(
Arrays.asList(this.getClass().getDeclaredFields())
.stream()
.filter(
field -> {
return field.isAnnotationPresent(Mock.class);
})
.map(
field -> {
try {
field.setAccessible(true);
return field.get(this);
} catch (IllegalArgumentException | IllegalAccessException e) {
e.printStackTrace();
return null;
}
})
.filter(field -> field != null)
.toArray(Object[]::new));
}
示例12: instanceMocksIn
import org.mockito.Mock; //导入依赖的package包/类
private Set<Object> instanceMocksIn(Object instance, Class<?> clazz) {
Set<Object> instanceMocks = new HashSet<Object>();
Field[] declaredFields = clazz.getDeclaredFields();
for (Field declaredField : declaredFields) {
if (declaredField.isAnnotationPresent(Mock.class) || declaredField.isAnnotationPresent(Spy.class)) {
declaredField.setAccessible(true);
try {
Object fieldValue = declaredField.get(instance);
if (fieldValue != null) {
instanceMocks.add(fieldValue);
}
} catch (IllegalAccessException e) {
throw new MockitoException("Could not access field " + declaredField.getName());
}
}
}
return instanceMocks;
}
示例13: process
import org.mockito.Mock; //导入依赖的package包/类
public Object process(Mock annotation, Field field) {
MockSettings mockSettings = Mockito.withSettings();
if (annotation.extraInterfaces().length > 0) { // never null
mockSettings.extraInterfaces(annotation.extraInterfaces());
}
if ("".equals(annotation.name())) {
mockSettings.name(field.getName());
} else {
mockSettings.name(annotation.name());
}
if(annotation.serializable()){
mockSettings.serializable();
}
// see @Mock answer default value
mockSettings.defaultAnswer(annotation.answer().get());
return Mockito.mock(field.getType(), mockSettings);
}
示例14: buildConstructorArgs
import org.mockito.Mock; //导入依赖的package包/类
private static Object[] buildConstructorArgs(Object test) throws IllegalAccessException {
Map<Class<?>, List<Object>> result = new TreeMap<Class<?>, List<Object>>(new SimpleClassComparator());
for (Field field : test.getClass().getDeclaredFields()) {
field.setAccessible(true);
if (field.getAnnotation(Mock.class) != null) {
if (result.get(field.getType()) == null) {
result.put(field.getType(), new ArrayList<Object>());
}
result.get(field.getType()).add(field.get(test));
}
}
List<Object> combined = new ArrayList<Object>();
for (List<Object> objects : result.values()) {
combined.addAll(objects);
}
combined.add(new Object());
return combined.toArray(new Object[result.size()]);
}
示例15: process
import org.mockito.Mock; //导入依赖的package包/类
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
for (Element rootElement : roundEnv.getRootElements()) {
if (rootElement.getAnnotation(MockModule.class) != null) {
TypeElement type = (TypeElement) rootElement;
PackageElement packageElement = (PackageElement) type.getEnclosingElement();
PendingModule module = new PendingModule(type.getSimpleName() + "MockModule", packageElement.getQualifiedName().toString(), type.getQualifiedName().toString(), getInjectsElement(type));
for (Element element : type.getEnclosedElements()) {
if (element.getAnnotation(Mock.class) != null) {
TypeMirror fieldType = element.asType();
module.addMock(new MockField(fieldType.toString(), element.getSimpleName().toString(), element.getAnnotationMirrors()));
}
}
writeModuleSource(module);
}
}
return true;
}