我有模型模拟类,我在其中验证来自另一个DAO模拟对象的调用。但是这个调用不会发生在模型类中,只会在DAO类上进行验证。
public class ApplicationTests {
@Mock
private Card card;
@Mock
private JourneyDAO journeyDAO;
@InjectMocks
private JourneyServiceImpl journeyServiceImpl;
private static final Double ZERO_VALUE = 0.0;
private static final Double INITIAL_BALANCE = 30D;
private static final String NAME = "name";
@Before
public void setUp() {
when(card.getBalance()).thenReturn(INITIAL_BALANCE);
when(card.getName()).thenReturn(NAME);
}
@Test
public void startJourneyInZone1_Test() {
barrier = new Barrier(card, direction.IN, journeyType.TUBE, Stations.Holborn);
journeyServiceImpl.startTubeJourney(barrier);
verify(card, times(1)).addBalance(MAX_FARE * -1);// this call is not heppening
verify(journeyDAO, times(1)).startTubeJourney(barrier);// this is working
}
}
DAO类方法如下所示
public void startTubeJourney(Barrier barrier) {
if(barrier.getModeOfJourney() == TravelMode.TUBE) {
if(barrier.getDirection() == Direction.IN) {
boardingAtZone = barrier.getZone();
barrier.getCard().addBalance(MAX_FARE * -1);
}
}
}
我在运行测试时遇到错误。请忽略包名和类名不匹配,因为我正在做一些示例案例。
Wanted but not invoked:
card.addBalance(0.7000000000000002d);
-> at com.rcard.travel.ApplicationTests.endTubeJourneyTest(ApplicationTests.java:69)
Actually, there were zero interactions with this mock.
at com.travel.ApplicationTests.endTubeJourneyTest(ApplicationTests.java:69)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:73)
at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:83)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
障碍类似乎
public class Barrier {
private Card card;
private Direction direction;
private TravelMode modeOfJourney;
private Stations zone;
public Barrier(Card card, Direction direction, TravelMode mode, Stations zone) {
this.card = card;
this.direction = direction;
this.modeOfJourney = mode;
this.zone = zone;
}
public String getZone() {
return Stations.elementOf(this.zone);
}
public void setZone(Stations zone) {
this.zone = zone;
}
public Card getCard() {
return card;
}
public void setCard(Card card) {
this.card = card;
}
public Direction getDirection() {
return direction;
}
public void setDirection(Direction direction) {
this.direction = direction;
}
public TravelMode getModeOfJourney() {
return modeOfJourney;
}
public void setModeOfJourney(TravelMode modeOfJourney) {
this.modeOfJourney = modeOfJourney;
}
}
答案 0 :(得分:1)
因为journeyDAO
是模拟,这意味着没有调用真实的journeyDAO.startTubeJourney()
。
一种可能的解决方法是使用真正的JourneyDAO
,因此调用card.addBalance()
,但这确实意味着一个测试正在测试服务和DAO类。
或者,我很想为JourneyDAO
编写单独的测试。这可以调用其startTubeJourney()
,并验证card.addBalance()
。这意味着针对模拟verify()
的当前journeyDAO.startTubeJourney()
可以保留。
答案 1 :(得分:0)
问题在于
行@Mock
private JourneyDAO journeyDAO;
您正在尝试验证属于模拟对象journeyDAO
的方法。如果您希望when...then
以及在可用时调用实际方法,则需要使用@Spy
注释。
@Spy
private JourneyDAO journeyDAO;
将解决问题。