Erro单元测试[通缉但未调用] mockito

时间:2012-10-13 05:09:50

标签: java testing junit mockito

的疑问: 晚安,

我正在对我的服务进行一些测试,而我在删除方法中执行测试时遇到了问题。

我想知道是否有人有过这个错误并且可以帮助我。

执行扫描时发生错误。报告的是该方法没有使用。

例外:

Wanted but not invoked:
usuariosRepository.findRow(5);
-> at br.com.fortalezaserv.services.impl.tests.RepositoryUsuariosServiceTest.deveExcluirPorId(RepositoryUsuariosServiceTest.java:74)

However, there were other interactions with this mock:
-> at br.com.fortalezaserv.services.impl.RepositoryUsuariosService.excluir(RepositoryUsuariosService.java:74)

at br.com.fortalezaserv.services.impl.tests.RepositoryUsuariosServiceTest.deveExcluirPorId(RepositoryUsuariosServiceTest.java:74)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

代码:

@ContextConfiguration(classes = TestContext.class)
@RunWith(SpringJUnit4ClassRunner.class)    
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)
public class RepositoryUsuariosServiceTest {

private RepositoryUsuariosService usuarioService;
private UsuariosRepository usuariosRepositoryMock;

private Usuarios usuario;
private Long ID = Long.valueOf(5l);

/**
 * 
 */
@Before
public void setUp() {
    usuario = new Usuarios();
    usuario.setEmail("email@me.com");
    usuario.setHash("nuadw");
    usuario.setLogin("Login");
    usuario.setNome("Name");
    usuario.setPassword("89331n9183n193n1n9319n3189");
    usuarioService = new RepositoryUsuariosService();

    usuariosRepositoryMock = mock(UsuariosRepository.class);
    usuarioService.setUsuarioRepository(usuariosRepositoryMock);
}


/**
 * @throws QualityException
 */
@Test
public void deveExcluirPorId() throws QualityException {
    when(usuariosRepositoryMock.findRow(ID)).thenReturn(null);
    usuarioService.excluir(ID);
    verify(usuariosRepositoryMock, times(1)).findRow(ID);
    verify(usuariosRepositoryMock, times(1)).delete(ID);
    verifyNoMoreInteractions(usuariosRepositoryMock);
}
 }

2 个答案:

答案 0 :(得分:1)

您尚未在MockitoJUnitRunner子句中添加@RunWith。由于您已经有SpringJunitRunner,因此无法添加另一个(link for details)。

作为替代方案,请在MockitoAnnotations.initMocks(this);的setUp方法中尝试@Before

还有一个建议,看起来您已在RepositoryUsuariosService服务中添加了setter进行测试。避免使用@Mock注释来模拟UsuariosRepository类,然后使用@InjectMocks RepositoryUsuariosService来自动插入所有模拟。

答案 1 :(得分:0)

我在这里解决了这个问题。

这是我的逻辑失败,我的删除功能没有返回。该方法不执行任何删除查询。

然后我转到测试:

@Test
public void deveExcluirPorId() throws QualityException {
    usuarioService.excluir(ID);
    verify(usuariosRepositoryMock, times(1)).delete(ID);
    verifyNoMoreInteractions(usuariosRepositoryMock);
}

谢谢!