gpt4 book ai didi

java - 对可调用执行器进行单元测试

转载 作者:行者123 更新时间:2023-11-30 02:05:08 25 4
gpt4 key购买 nike

我有一个像这样的 Controller 类,它将一些执行器传递到可运行实例中。这不是它的工作原理,但为了简单起见,我这样做了。

package com.test.executor;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Controller {
private ExecutorService executor;

public Controller() {
executor = Executors.newCachedThreadPool();
}

public void doRun() {
MyRunner runner = new MyRunner(executor);
Thread myRunner = new Thread(runner);
myRunner.start();
}

public static void main(String[] args) {
new Controller().doRun();
}
}

运行程序接收执行程序的实例,然后传递某些可调用对象。现在,可调用对象有点不同,因为某些可调用对象访问数据库/调用某些 Web 服务/文件系统

我在如何为这种情况编写正确的 JUnit 方面遇到一些问题。我希望拥有尽可能多的代码覆盖率。

package com.test.executor;

import java.util.Collections;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class MyRunner implements Runnable {

private ExecutorService executor;

public MyRunner(ExecutorService executor) {
this.executor = executor;
}

@Override
public void run() {
// TODO Auto-generated method stub
Future<Boolean> ret1 = executor.submit(new SampleCall1());

try {
ret1.get(5, TimeUnit.MINUTES);
} catch (InterruptedException | ExecutionException | TimeoutException e) {
e.printStackTrace();
}

// Do other things

Future<List<String>> ret2 = executor.submit(new SampleCall2());

try {
ret2.get(5, TimeUnit.MINUTES);
} catch (InterruptedException | ExecutionException | TimeoutException e) {
e.printStackTrace();
}

// Do other things

Future<Long> ret3 = executor.submit(new SampleCall3());

try {
ret3.get(5, TimeUnit.MINUTES);
} catch (InterruptedException | ExecutionException | TimeoutException e) {
e.printStackTrace();
}
}

public static class SampleCall1 implements Callable<Boolean> {

@Override
public Boolean call() throws Exception {
// Sample Return Only
// This will call JSON web service
return true;
}

}

public static class SampleCall2 implements Callable<List<String>> {

@Override
public List<String> call() throws Exception {
// Sample Return Only
// This will call Database
return Collections.EMPTY_LIST;
}

}

public static class SampleCall3 implements Callable<Long> {

@Override
public Long call() throws Exception {
// Sample Return Only
// This will access some file system
return 1L;
}

}

}

我的问题是为此编写单元测试的正确方法是什么。我正在收集一些关于如何对这个类(class)进行单元测试的建议?我不知道在我的 junit/mockito 实例中要模拟什么。我应该 mock 每个可调用对象吗?然后为 MyRunner 创建一个测试用例。

我担心依赖性......因为我正在连接到数据库/网络服务/和文件系统,所以我想寻求一些建议。

更新2

package com.test.executor;

import java.util.Collections;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class MyRunner implements Runnable {

private ExecutorService executor;

public MyRunner(ExecutorService executor) {
this.executor = executor;
}

@Override
public void run() {
executeTasks1();
// Do other things

executeTasks2();
// Do other things


executeTasks3();

}

public boolean executeTasks1(){
// TODO Auto-generated method stub
Future<Boolean> ret1 = executor.submit(new SampleCall1());

try {
return ret1.get(5, TimeUnit.MINUTES);
} catch (InterruptedException | ExecutionException | TimeoutException e) {
e.printStackTrace();
}

}

public List<String> executeTasks2(){
Future<List<String>> ret2 = executor.submit(new SampleCall2());

try {
return ret2.get(5, TimeUnit.MINUTES);
} catch (InterruptedException | ExecutionException | TimeoutException e) {
e.printStackTrace();
}
}

public Long executeTasks3(){
Future<Long> ret3 = executor.submit(new SampleCall3());

try {
return ret3.get(5, TimeUnit.MINUTES);
} catch (InterruptedException | ExecutionException | TimeoutException e) {
e.printStackTrace();
}
}

public static class SampleCall1 implements Callable<Boolean> {

@Override
public Boolean call() throws Exception {
// Sample Return Only
// This will call JSON web service
return true;
}

}

public static class SampleCall2 implements Callable<List<String>> {

@Override
public List<String> call() throws Exception {
// Sample Return Only
// This will call Database
return Collections.EMPTY_LIST;
}

}

public static class SampleCall3 implements Callable<Long> {

@Override
public Long call() throws Exception {
// Sample Return Only
// This will access some file system
return 1L;
}

}

}

最佳答案

不要在代码覆盖率上花费太多时间。在某种程度上要务实。测试什么是值得测试的。提高测试质量,而不是数量。关于指标的一些旁注:100% 线路覆盖率不如高分支覆盖率那么重要,获得高分支覆盖率可能会花费您大量时间,即使这样:您可能不需要采用所有可能的路线。 PIT (mutation testing)是一个有用的“工具”,它通过更改被测代码来检查测试的实际效果。但请使用该信息作为指导,而不是作为实现更多测试的措施。别夸张! (也许如果你正在开发一个库,你不想再经常改变或者你想让它坚如磐石(并且你有空闲时间),你可以,但否则我不会)

曾经有一段时间,我非常准确地测试了一切。问题是:一旦发生变化(例如:今天我们需要 X,明天是 Y),许多测试就会失败。那么就需要大量的返工。如果您测试最合适的,一旦需求发生巨大变化,您就可以专注于重要的事情,这应该花费更少的时间。

看看您提供的代码,我可能会为每个 Callable 实现编写一个测试(类),只是为了确保它们执行我想要的操作(这可能会导致提取Callable 类作为副作用)。对于 Controller 和运行者,我还不太确定... MyRunner 对我来说似乎已经过时了...但也许不是。所以我可能只会测试 Controller ......

关于模拟:我开始尽可能地省略模拟。大多数时候我还会添加集成测试,我想知道系统作为一个整体如何运行以及它是否按预期工作。我见过很多测试,其中有很多模拟,通过更改任何代码,没有单元测试失败,尽管我认为有些测试应该失败。我还看到很多单元测试,这些单元测试实际上看起来像集成测试,但到处都是模拟。那么模拟首先有什么帮助呢?单独设置模拟可能花费了太多时间。但这又是我的观点。因此,尽管许多人喜欢测试金字塔有一个广泛的单元测试底部,但我认为它更务实,并将一些测试移动到集成测试“层”,而不是使用大量的模拟。最后,这也是可用性和速度的问题。您希望测试能够快速给出结果,但您仍然希望结果具有最大值(value)(模拟仅给出部分结果)。

关于java - 对可调用执行器进行单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51586922/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com