We are arguing about this approach with my colleagues. They say to use SpringRunner only on integration or functional levels.
The question is what pros and cons of using it in level below?
For example I have simple bean:
public class RewardDurationCalculator {
private Clock clock;
public OptionalLong calculate(DurationType durationType, List<Pass> passes) {
long now = Instant.now(clock).getEpochSecond();
switch (durationType) {
case FULL_PASS:
return getCurrentPassDuration(passes, now);
case TILL_THE_END_OF_THE_CURRENT_ACTIVE_PASS:
return getTimeInCurrentPassLeft(passes, now);
}
return OptionalLong.empty();
}
private OptionalLong getCurrentPassDuration(List<Pass> passes, long now) {
return passes.stream()
.filter(currentPass(now))
.mapToLong(Pass::getDuration)
.findFirst();
}
private OptionalLong getTimeInCurrentPassLeft(List<Pass> passes, long now) {
return passes.stream()
.filter(currentPass(now))
.mapToLong(pass -> getEndTs(pass) - now)
.findFirst();
}
private Predicate<Pass> currentPass(long now) {
return pass -> pass.getStartTs() >= now && now <= getEndTs(pass);
}
private long getEndTs(Pass pass) {
return pass.getStartTs() + pass.getDuration();
}
}
that is doing some calculation logic. For it I have also spring config:
@Configuration
public class RewardDurationCalculatorConfiguration {
@Bean
public RewardDurationCalculator rewardDurationCalculator(Clock clock) {
return new RewardDurationCalculator(clock);
}
}
So why can't I write unit test for it like this:
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = RewardDurationCalculatorConfiguration.class)
public class RewardDurationCalculatorTest {
@MockBean
private Clock clock;
@Autowired
private RewardDurationCalculator rewardDurationCalculator;
@Test
public void testCalculateCurrentPassDurationShouldBeReturnedIfPassWasCreatedRightNow() {
rewardDurationCalculator.calculate(DurationType.FULL_PASS, Collections.emptyList());
}
}
What cons I can face with using such approach?