Java - Testing Simulation With Mockito
I'm not very well-versed with Mockito but am trying to use mocks to test behaviour of a simulation, this is the class:
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();
}
}
Below is the test class:
@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));
}
}
I would like to assert that GOLDEN_STATE_WARRIORS
is returned when the probability range is between 0 and 0.7- however I get an assertion error of null with my test code.
java.lang.AssertionError:
Expected: is <GOLDEN_STATE_WARRIORS>
but: was null
Expected :is <GOLDEN_STATE_WARRIORS>
java unit-testing junit mocking mockito
add a comment |
I'm not very well-versed with Mockito but am trying to use mocks to test behaviour of a simulation, this is the class:
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();
}
}
Below is the test class:
@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));
}
}
I would like to assert that GOLDEN_STATE_WARRIORS
is returned when the probability range is between 0 and 0.7- however I get an assertion error of null with my test code.
java.lang.AssertionError:
Expected: is <GOLDEN_STATE_WARRIORS>
but: was null
Expected :is <GOLDEN_STATE_WARRIORS>
java unit-testing junit mocking mockito
add a comment |
I'm not very well-versed with Mockito but am trying to use mocks to test behaviour of a simulation, this is the class:
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();
}
}
Below is the test class:
@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));
}
}
I would like to assert that GOLDEN_STATE_WARRIORS
is returned when the probability range is between 0 and 0.7- however I get an assertion error of null with my test code.
java.lang.AssertionError:
Expected: is <GOLDEN_STATE_WARRIORS>
but: was null
Expected :is <GOLDEN_STATE_WARRIORS>
java unit-testing junit mocking mockito
I'm not very well-versed with Mockito but am trying to use mocks to test behaviour of a simulation, this is the class:
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();
}
}
Below is the test class:
@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));
}
}
I would like to assert that GOLDEN_STATE_WARRIORS
is returned when the probability range is between 0 and 0.7- however I get an assertion error of null with my test code.
java.lang.AssertionError:
Expected: is <GOLDEN_STATE_WARRIORS>
but: was null
Expected :is <GOLDEN_STATE_WARRIORS>
java unit-testing junit mocking mockito
java unit-testing junit mocking mockito
edited Nov 20 '18 at 23:31
ETO
2,435425
2,435425
asked Nov 20 '18 at 21:22
Clatty CakeClatty Cake
3293511
3293511
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Try this:
@Mock
private Fixture fixture;
private SimulateBasketballMatchResult simulator;
@Before
public void setUp() {
simulator = spy(new SimulateBasketballMatchResult(fixture));
doCallRealMethod().when(simulator).simulate();
}
@Test
public void shouldReturnGoldenStateWarriorsAsWinner() {
doReturn(0.5).when(simulator).randomDoubleGenerator();
when(fixture.getHomeTeam()).thenReturn(GOLDEN_STATE_WARRIORS);
when(fixture.getAwayTeam()).thenReturn(HOUSTON_ROCKETS);
assertThat(simulator.simulate(MATCH_RESULT_PROBABILITY_MAP), is(GOLDEN_STATE_WARRIORS));
}
Mockito.spy
and @Spy
allow you to mock some methods of a real object, but Mockito.mock
and @Mock
mock the whole object.
A mock in mockito is a normal mock (allows you to stub invocations; that is, return specific values out of method calls).
A spy in mockito is a partial mock (part of the object will be mocked and part will use real method invocations).
Read more
This worked! Thanks so much. What doesspy
actually do and how is it different to mocking?
– Clatty Cake
Nov 20 '18 at 22:00
add a comment |
simulateBasketballMatchResult
is a mock object, so by default, it will return null
for all its methods (that have a non-primitive return value, of course).
Instead of mocking that object, you should probably spy it:
@Spy
private SimulateBasketballMatchResult simulateBasketballMatchResult =
new SimulateBasketballMatchResult(Fixture);
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53401736%2fjava-testing-simulation-with-mockito%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Try this:
@Mock
private Fixture fixture;
private SimulateBasketballMatchResult simulator;
@Before
public void setUp() {
simulator = spy(new SimulateBasketballMatchResult(fixture));
doCallRealMethod().when(simulator).simulate();
}
@Test
public void shouldReturnGoldenStateWarriorsAsWinner() {
doReturn(0.5).when(simulator).randomDoubleGenerator();
when(fixture.getHomeTeam()).thenReturn(GOLDEN_STATE_WARRIORS);
when(fixture.getAwayTeam()).thenReturn(HOUSTON_ROCKETS);
assertThat(simulator.simulate(MATCH_RESULT_PROBABILITY_MAP), is(GOLDEN_STATE_WARRIORS));
}
Mockito.spy
and @Spy
allow you to mock some methods of a real object, but Mockito.mock
and @Mock
mock the whole object.
A mock in mockito is a normal mock (allows you to stub invocations; that is, return specific values out of method calls).
A spy in mockito is a partial mock (part of the object will be mocked and part will use real method invocations).
Read more
This worked! Thanks so much. What doesspy
actually do and how is it different to mocking?
– Clatty Cake
Nov 20 '18 at 22:00
add a comment |
Try this:
@Mock
private Fixture fixture;
private SimulateBasketballMatchResult simulator;
@Before
public void setUp() {
simulator = spy(new SimulateBasketballMatchResult(fixture));
doCallRealMethod().when(simulator).simulate();
}
@Test
public void shouldReturnGoldenStateWarriorsAsWinner() {
doReturn(0.5).when(simulator).randomDoubleGenerator();
when(fixture.getHomeTeam()).thenReturn(GOLDEN_STATE_WARRIORS);
when(fixture.getAwayTeam()).thenReturn(HOUSTON_ROCKETS);
assertThat(simulator.simulate(MATCH_RESULT_PROBABILITY_MAP), is(GOLDEN_STATE_WARRIORS));
}
Mockito.spy
and @Spy
allow you to mock some methods of a real object, but Mockito.mock
and @Mock
mock the whole object.
A mock in mockito is a normal mock (allows you to stub invocations; that is, return specific values out of method calls).
A spy in mockito is a partial mock (part of the object will be mocked and part will use real method invocations).
Read more
This worked! Thanks so much. What doesspy
actually do and how is it different to mocking?
– Clatty Cake
Nov 20 '18 at 22:00
add a comment |
Try this:
@Mock
private Fixture fixture;
private SimulateBasketballMatchResult simulator;
@Before
public void setUp() {
simulator = spy(new SimulateBasketballMatchResult(fixture));
doCallRealMethod().when(simulator).simulate();
}
@Test
public void shouldReturnGoldenStateWarriorsAsWinner() {
doReturn(0.5).when(simulator).randomDoubleGenerator();
when(fixture.getHomeTeam()).thenReturn(GOLDEN_STATE_WARRIORS);
when(fixture.getAwayTeam()).thenReturn(HOUSTON_ROCKETS);
assertThat(simulator.simulate(MATCH_RESULT_PROBABILITY_MAP), is(GOLDEN_STATE_WARRIORS));
}
Mockito.spy
and @Spy
allow you to mock some methods of a real object, but Mockito.mock
and @Mock
mock the whole object.
A mock in mockito is a normal mock (allows you to stub invocations; that is, return specific values out of method calls).
A spy in mockito is a partial mock (part of the object will be mocked and part will use real method invocations).
Read more
Try this:
@Mock
private Fixture fixture;
private SimulateBasketballMatchResult simulator;
@Before
public void setUp() {
simulator = spy(new SimulateBasketballMatchResult(fixture));
doCallRealMethod().when(simulator).simulate();
}
@Test
public void shouldReturnGoldenStateWarriorsAsWinner() {
doReturn(0.5).when(simulator).randomDoubleGenerator();
when(fixture.getHomeTeam()).thenReturn(GOLDEN_STATE_WARRIORS);
when(fixture.getAwayTeam()).thenReturn(HOUSTON_ROCKETS);
assertThat(simulator.simulate(MATCH_RESULT_PROBABILITY_MAP), is(GOLDEN_STATE_WARRIORS));
}
Mockito.spy
and @Spy
allow you to mock some methods of a real object, but Mockito.mock
and @Mock
mock the whole object.
A mock in mockito is a normal mock (allows you to stub invocations; that is, return specific values out of method calls).
A spy in mockito is a partial mock (part of the object will be mocked and part will use real method invocations).
Read more
edited Nov 20 '18 at 22:13
answered Nov 20 '18 at 21:53
ETOETO
2,435425
2,435425
This worked! Thanks so much. What doesspy
actually do and how is it different to mocking?
– Clatty Cake
Nov 20 '18 at 22:00
add a comment |
This worked! Thanks so much. What doesspy
actually do and how is it different to mocking?
– Clatty Cake
Nov 20 '18 at 22:00
This worked! Thanks so much. What does
spy
actually do and how is it different to mocking?– Clatty Cake
Nov 20 '18 at 22:00
This worked! Thanks so much. What does
spy
actually do and how is it different to mocking?– Clatty Cake
Nov 20 '18 at 22:00
add a comment |
simulateBasketballMatchResult
is a mock object, so by default, it will return null
for all its methods (that have a non-primitive return value, of course).
Instead of mocking that object, you should probably spy it:
@Spy
private SimulateBasketballMatchResult simulateBasketballMatchResult =
new SimulateBasketballMatchResult(Fixture);
add a comment |
simulateBasketballMatchResult
is a mock object, so by default, it will return null
for all its methods (that have a non-primitive return value, of course).
Instead of mocking that object, you should probably spy it:
@Spy
private SimulateBasketballMatchResult simulateBasketballMatchResult =
new SimulateBasketballMatchResult(Fixture);
add a comment |
simulateBasketballMatchResult
is a mock object, so by default, it will return null
for all its methods (that have a non-primitive return value, of course).
Instead of mocking that object, you should probably spy it:
@Spy
private SimulateBasketballMatchResult simulateBasketballMatchResult =
new SimulateBasketballMatchResult(Fixture);
simulateBasketballMatchResult
is a mock object, so by default, it will return null
for all its methods (that have a non-primitive return value, of course).
Instead of mocking that object, you should probably spy it:
@Spy
private SimulateBasketballMatchResult simulateBasketballMatchResult =
new SimulateBasketballMatchResult(Fixture);
answered Nov 20 '18 at 21:36
MureinikMureinik
181k22131200
181k22131200
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53401736%2fjava-testing-simulation-with-mockito%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown