0

疑问:晚安,

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

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

当我进行扫描时发生错误。报告的是该方法未使用。

例外:

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);
}
 }
4

2 回答 2

1

您还没有添加MockitoJUnitRunnerin@RunWith子句。由于您已经拥有SpringJunitRunner,因此无法添加另一个(详细信息链接)。

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

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

于 2012-10-13T09:40:27.607 回答
0

我在这里解决了问题。

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

然后我切换到测试:

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

谢谢!

于 2012-10-13T13:20:19.447 回答