我的单元测试有问题,这意味着我的应用程序中包含Dao的服务类。
@Mock(answer = Answers.RETURNS_DEEP_STUBS) private AvisDao avisDao; @InjectMocks private ApiPortalsService service = new ApiPortalsServiceImpl(); @Before public void initMocksWS() throws Exception{ MockitoAnnotations.initMocks(this); } Factory factoria = FactoryImpl.getInstance(); @Test public void testGuardarAvisos() throws Exception{ Mockito.when(avisDao.existsPortalContingut(AvisPortal.class.cast(Matchers.anyObject()))).thenAnswer(new Answer<boolean>(){ @Override public boolean answer(InvocationOnMock invocation) throws Throwable{ return true; } }); }
这是测试类,当我尝试设置 the thenAnswer 使用布尔值。方法 existsPortalContingut 包含对象的返回 布尔型 。这是我第一次尝试创建测试类。如果我忘记了什么,请告诉我,我会编辑它。
the thenAnswer
existsPortalContingut
我在说什么错了?
boolean existsPortalContingut(final AvisPortal portalContenido);
这就是我试图在Dao中调用的方法
答案很简单,不是布尔型的,它是布尔型的,因为它返回一个对象。
应该是这样的:
Mockito.when(avisDao.existsPortalContingut(AvisPortal.class.cast(Matchers.anyObject()))).thenAnswer(new Answer<Boolean>(){ @Override public Boolean answer(InvocationOnMock invocation) throws Throwable { return true; } });