英文:
Spock test cases fail when multiple tests involved with async client calls , but runs successfully when individually run
问题
测试场景是从一个名为异步调用的模拟客户端服务中获取结果。在这里,如果我尝试单独运行上述测试,它们会成功执行。但是当尝试通过```mvn clean install```构建项目时,成功的测试用例会出错。
如何处理异常情况,以使其不影响预期的方法行为测试用例?或者可能是什么原因导致多个异步调用出现问题?
如何处理/关闭生成的线程,或者在开始下一个客户端调用之前等待其完成?
// 服务类
public String serviceMethod(String arg1, String arg2) {
return mockedclient.fetchDetails(arg1, arg2)
.toCompletableFuture()
.thenapplyasync(HashSet::new) // 假设收到的响应是一个列表
.get(3,TimeUnit.SECONDS);
}
// 测试类
def "应该返回从客户端检索到的结果" () {
given:
def arg1 = "value1"
def arg2 = "value2"
def futureList = CompletableFuture.completedFuture(Collections.toList("RESULT"))
def futureSet = new BlockingVariable<Set
when:
def result = serviceMethod(arg1, arg2)
then:
1 * mockedClient.fetchDetails() >> futureList
0 * futureList.thenApplyAsync() >> futureSet
0 * futureSet.get(,) >> Collections.toSet("RESULT")
}
def "应该在从客户端检索结果时返回空响应" () {
given:
def arg1 = "value1"
def arg2 = "value2"
def futureList = CompletableFuture.completedFuture(Collections.toList("RESULT"))
def futureSet = new BlockingVariable<Set
when:
def result = serviceMethod(arg1, arg2)
then:
1 * mockedClient.fetchDetails() >> CompletableFuture.runAsync( { throw new Exception("无法获取经纪人分析师详情") } )
0 * futureList.thenApplyAsync() >> futureSet
0 * futureSet.get(,) >> Collections.toSet("RESULT")
}
<details>
<summary>英文:</summary>
The test scenario is to get the result from a mocked client service called asynchronously. Here, if I try and run the mentioned tests individually, they execute successfully. But when trying to build the project via ```mvn clean install``` , the success test case gives error.
How do I handle the exception case scenario so that it does not affect the expected method behavior test cases? Or possibly what is causing the issue with multiple async calls?
How do I handle/shutdown the spawned up thread, or wait for its completion before starting the next client call?
// SERVICE CLASS
public String serviceMethod(String arg1, String arg2) {
return mockedclient.fetchDetails(arg1, arg2)
.toCompletableFuture()
.thenapplyasync(HashSet::new) // assuming the received response is a list
.get(3,TimeUnit.SECONDS);
}
//TEST CLASS
def " should return result retrieved from the client" () {
given:
def arg1 = "value1"
def arg2 = "value2"
def futureList = CompletableFuture.completedFuture(Collections.toList("RESULT"))
def futureSet = new BlockingVariable<Set<String>>()
when:
def result = serviceMethod(arg1, arg2)
then:
1 * mockedClient.fetchDetails() >> futureList
0 * futureList.thenApplyAsync() >> futureSet
0 * futureSet.get(,) >> Collections.toSet("RESULT")
}
def " should return empty response when result retrieved from the client" () {
given:
def arg1 = "value1"
def arg2 = "value2"
def futureList = CompletableFuture.completedFuture(Collections.toList("RESULT"))
def futureSet = new BlockingVariable<Set<String>>()
when:
def result = serviceMethod(arg1, arg2)
then:
1 * mockedClient.fetchDetails() >> CompletableFuture.runAsync( { throw new Exception("Failed to fetch broker Analyst details " } )
0 * futureList.thenApplyAsync() >> futureSet
0 * futureSet.get(,) >> Collections.toSet("RESULT")
}
</details>
# 答案1
**得分**: 0
这不是一个完整的回答,因为您也没有提供完整的[MCVE](https://stackoverflow.com/help/mcve)。因此,我无法通过复制、编译和运行您的代码片段来重现您的问题。但有一些事情让我感到奇怪:
* 您只是说某些东西不起作用,但没有显示任何错误消息和堆栈跟踪。
* 您的生产服务类使用了`mockedclient`,至少名称很奇怪。为什么在生产中使用模拟,或者使用模拟来测试某些东西呢?
* `futureList`在两个特性方法中都不是模拟,而是一个真实的对象。因此,您不能对其进行交互检查和返回类似于`0 * futureList.thenApplyAsync(_) >> futureSet`的存根结果。
* `futureSet`在两个特性方法中也不是模拟,而是一个真实的对象。因此,您不能对其进行交互检查和返回类似于`0 * futureSet.get(_, _) >> Collections.toSet("RESULT")`的存根结果。
* 在两个特性方法中,您使用了一个名为`mockedClient`的变量,但忘记了显示它在哪里以及如何定义/初始化。
我猜您的测试不仅在Maven上抛出错误,而且在本地也是如此,因为它们包含错误。
<details>
<summary>英文:</summary>
This is not a full answer because you didn't provide a full [MCVE](https://stackoverflow.com/help/mcve) either. So I cannot reproduce your problem by copying, compiling and running your code fragments. But a few things strike me as odd:
* You just say something is not working but do not show any error messages and stacktraces.
* Your production service class uses `mockedclient`, at least the name is strange. Why would you use a mock in production or test something using a mock?
* `futureList` in both feature methods is not a mock but a real object. Thus, you cannot check interactions and return stubbed results like `0 * futureList.thenApplyAsync(_) >> futureSet` on it.
* `futureSet` in both feature methods is not a mock but a real object. Thus, you cannot check interactions and return stubbed results like `0 * futureSet.get(_,_) >> Collections.toSet("RESULT")` on it.
* In both feature methods you use a variable `mockedClient` but forgot to show where and how it is defined/initialised.
I assume your tests are throwing errors not just on Maven but also locally because, well - they contain bugs.
</details>
专注分享java语言的经验与见解,让所有开发者获益!
评论