Я не очень хорошо разбираюсь в Mockito, но пытаюсь использовать mocks для проверки поведения симуляции, это класс:
package simulator;
import java.util.Map;
import org.apache.commons.lang3.Validate;
import simulator.enums.Team;
import simulator.fixtures.Fixture;
public class SimulateBasketballMatchResult implements Simulation<Team> {
private final Fixture fixture;
public SimulateBasketballMatchResult(Fixture fixture) {
Validate.notNull(fixture, "fixture cannot be null");
this.fixture = fixture;
}
@Override
public Team simulate(Map<Team, Double> outcomeProbabilityMap) {
Validate.notNull(outcomeProbabilityMap, "outcomeProbabilityMap cannot be null");
final Team homeTeam = fixture.getHomeTeam();
final Team awayTeam = fixture.getAwayTeam();
double random = randomDoubleGenerator();
double homeWinProbability = outcomeProbabilityMap.get(homeTeam);
return random < homeWinProbability ? homeTeam : awayTeam;
}
public Double randomDoubleGenerator() {
return Math.random();
}
}
Ниже приведен класс тестирования:
@RunWith(MockitoJUnitRunner.class)
public class SimulateBasketballMatchResultTest {
@Rule
public ExpectedException expectedException = ExpectedException.none();
private static final Map<Team, Double> MATCH_RESULT_PROBABILITY_MAP = new HashMap<>();
private static final Fixture FIXTURE = new Fixture(GOLDEN_STATE_WARRIORS, HOUSTON_ROCKETS, REGULAR_SEASON);
static {
MATCH_RESULT_PROBABILITY_MAP.put(FIXTURE.getHomeTeam(), 0.7d);
MATCH_RESULT_PROBABILITY_MAP.put(FIXTURE.getAwayTeam(), 0.3d);
}
@Mock
private SimulateBasketballMatchResult simulateBasketballMatchResult;
@Test
public void shouldReturnGoldenStateWarriorsAsWinner() {
when(simulateBasketballMatchResult.randomDoubleGenerator()).thenReturn(0.5d);
assertThat(simulateBasketballMatchResult.simulate(MATCH_RESULT_PROBABILITY_MAP), is(GOLDEN_STATE_WARRIORS));
}
}
Я хотел бы заявить, что GOLDEN_STATE_WARRIORS
возвращается, когда диапазон вероятности находится в диапазоне от 0 до 0,7, однако я получаю нулевую ошибку утверждения с моим тестовым кодом.
java.lang.AssertionError:
Expected: is <GOLDEN_STATE_WARRIORS>
but: was null
Expected :is <GOLDEN_STATE_WARRIORS>