Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何验证在spring集成测试中调用了void函数_Java_Spring_Java 8_Integration Testing_Verify - Fatal编程技术网

Java 如何验证在spring集成测试中调用了void函数

Java 如何验证在spring集成测试中调用了void函数,java,spring,java-8,integration-testing,verify,Java,Spring,Java 8,Integration Testing,Verify,我编写了一个集成来测试delete函数,如下所示 @Test void deleteUsersTest() { Map<String, String> params = new HashMap<>(); params.put("id", "21"); this.template.delete("/v1/users/{id}", params); : } 在单元测试中,我通常使用这

我编写了一个集成来测试delete函数,如下所示

@Test
void deleteUsersTest() {
    Map<String, String> params = new HashMap<>();
    params.put("id", "21");
    this.template.delete("/v1/users/{id}", params);
    :
}
在单元测试中,我通常使用这样的东西

verify(userRepository, times(1)).deleteById((long) 21);
但是上面的一个是基于mockito的函数,我不能在集成测试中使用它

有人能帮我在Spring集成测试中验证这个功能吗

我使用的是Spring5,SpringBoot2.1,我建议使用,下面是使用的示例

Spy包装了真实bean,但允许您验证方法调用和模拟单个方法,而不会影响真实bean的任何其他方法。因此,通过使userRepository成为SpyBean,我们可以只模拟我们想在测试用例中模拟的方法,而不影响其他方法

另一种方法是,您还可以使用@MockBean创建一个mock,并使用callrealmethod调用real方法

@MockBean
private UserRepository userRepository
// Call a real method of a Mocked object
when(userRepository.deleteById(21l)).thenCallRealMethod();
然后说调用一个真正的方法

@MockBean
private UserRepository userRepository
// Call a real method of a Mocked object
when(userRepository.deleteById(21l)).thenCallRealMethod();
因此,使用上面的语句,它实际上调用了real方法,现在您可以验证它了

verify(userRepository, times(1)).deleteById(21l);
我建议使用,下面是使用的示例

Spy包装了真实bean,但允许您验证方法调用和模拟单个方法,而不会影响真实bean的任何其他方法。因此,通过使userRepository成为SpyBean,我们可以只模拟我们想在测试用例中模拟的方法,而不影响其他方法

另一种方法是,您还可以使用@MockBean创建一个mock,并使用callrealmethod调用real方法

@MockBean
private UserRepository userRepository
// Call a real method of a Mocked object
when(userRepository.deleteById(21l)).thenCallRealMethod();
然后说调用一个真正的方法

@MockBean
private UserRepository userRepository
// Call a real method of a Mocked object
when(userRepository.deleteById(21l)).thenCallRealMethod();
因此,使用上面的语句,它实际上调用了real方法,现在您可以验证它了

verify(userRepository, times(1)).deleteById(21l);

集成测试在真实数据库上进行。只需确保实体在调用delete之前存储在数据库中,而不是在调用delete之后存储在数据库中


集成测试在真实数据库上进行。只需确保实体在调用delete之前存储在数据库中,而不是在调用delete之后存储在数据库中


你能显示代码吗?你在模仿userRepository吗?@Deadpool我不是在模仿“userRepository”,它是在Spring上下文下运行的集成测试userRepository'已在测试类中“@Autowired”您可以显示代码吗?你在模仿userRepository吗?@Deadpool我不是在模仿“userRepository”,它是在Spring上下文下运行的集成测试userRepository“已在测试类中“@Autowired”仍然不能不执行集成验证测试以检查函数是否被调用…检查我的更新答案@AlexMan直到我们不能不执行集成验证测试以检查函数是否被调用…检查我的更新答案@AlexMan