我试图通过模拟一个类来使用weld-junit5。我嘲笑这门课,因为我想知道它多久会被调用一次。
但是每次我尝试 Mockito.verify() 这个模拟类时,它都会抛出一个“NotAMockException”
Intellij 调试器正在验证该字段:"Mock for MessageService, hashCode: XY"
我已经尝试将我的测试类添加到 WeldInitiator 中,但它不想工作。
“MessageService”是一个真正的类,而不是一个接口(接口也不起作用)
@EnableWeld
class GameServiceTest {
@WeldSetup
public WeldInitiator weld = WeldInitiator.from(GameService.class, GameCache.class,
/** Some More **/,
GameServiceTest.class).build();
@Produces
@ApplicationScoped
public MessageService createMessageServiceMock() {
return mock(MessageService.class);
}
@Inject
private MessageService messageService;
@Inject
private GameService gameService;
@Test
void handleRunningGames() {
this.gameService.handleRunningGames(null, mock(Session.class));
// This will throw a org.mockito.exceptions.misusing.NotAMockException
Mockito.verify(messageService, Mockito.times(1)).writeMessage(any(), any());
}
}
我希望 Injected MessageService 是一个真正的模拟,我可以在其上调用每个 Mockito 函数,但似乎并非如此。
我有什么问题吗,或者这样做的正确方法是什么?
我想我刚刚解决了这个问题:
private static final MessageService messageService = mock(MessageService.class);
@Produces
@ApplicationScoped
public MessageService createMessageServiceMock() {
return messageService;
}