- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个像这样的 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/
我正在编写一个具有以下签名的 Java 方法。 void Logger(Method method, Object[] args); 如果一个方法(例如 ABC() )调用此方法 Logger,它应该
我是 Java 新手。 我的问题是我的 Java 程序找不到我试图用作的图像文件一个 JButton。 (目前这段代码什么也没做,因为我只是得到了想要的外观第一的)。这是我的主课 代码: packag
好的,今天我在接受采访,我已经编写 Java 代码多年了。采访中说“Java 垃圾收集是一个棘手的问题,我有几个 friend 一直在努力弄清楚。你在这方面做得怎么样?”。她是想骗我吗?还是我的一生都
我的 friend 给了我一个谜语让我解开。它是这样的: There are 100 people. Each one of them, in his turn, does the following
如果我将使用 Java 5 代码的应用程序编译成字节码,生成的 .class 文件是否能够在 Java 1.4 下运行? 如果后者可以工作并且我正在尝试在我的 Java 1.4 应用程序中使用 Jav
有关于why Java doesn't support unsigned types的问题以及一些关于处理无符号类型的问题。我做了一些搜索,似乎 Scala 也不支持无符号数据类型。限制是Java和S
我只是想知道在一个 java 版本中生成的字节码是否可以在其他 java 版本上运行 最佳答案 通常,字节码无需修改即可在 较新 版本的 Java 上运行。它不会在旧版本上运行,除非您使用特殊参数 (
我有一个关于在命令提示符下执行 java 程序的基本问题。 在某些机器上我们需要指定 -cp 。 (类路径)同时执行java程序 (test为java文件名与.class文件存在于同一目录下) jav
我已经阅读 StackOverflow 有一段时间了,现在我才鼓起勇气提出问题。我今年 20 岁,目前在我的家乡(罗马尼亚克卢日-纳波卡)就读 IT 大学。足以介绍:D。 基本上,我有一家提供簿记应用
我有 public JSONObject parseXML(String xml) { JSONObject jsonObject = XML.toJSONObject(xml); r
我已经在 Java 中实现了带有动态类型的简单解释语言。不幸的是我遇到了以下问题。测试时如下代码: def main() { def ks = Map[[1, 2]].keySet()
一直提示输入 1 到 10 的数字 - 结果应将 st、rd、th 和 nd 添加到数字中。编写一个程序,提示用户输入 1 到 10 之间的任意整数,然后以序数形式显示该整数并附加后缀。 public
我有这个 DownloadFile.java 并按预期下载该文件: import java.io.*; import java.net.URL; public class DownloadFile {
我想在 GUI 上添加延迟。我放置了 2 个 for 循环,然后重新绘制了一个标签,但这 2 个 for 循环一个接一个地执行,并且标签被重新绘制到最后一个。 我能做什么? for(int i=0;
我正在对对象 Student 的列表项进行一些测试,但是我更喜欢在 java 类对象中创建硬编码列表,然后从那里提取数据,而不是连接到数据库并在结果集中选择记录。然而,自从我这样做以来已经很长时间了,
我知道对象创建分为三个部分: 声明 实例化 初始化 classA{} classB extends classA{} classA obj = new classB(1,1); 实例化 它必须使用
我有兴趣使用 GPRS 构建车辆跟踪系统。但是,我有一些问题要问以前做过此操作的人: GPRS 是最好的技术吗?人们意识到任何问题吗? 我计划使用 Java/Java EE - 有更好的技术吗? 如果
我可以通过递归方法反转数组,例如:数组={1,2,3,4,5} 数组结果={5,4,3,2,1}但我的结果是相同的数组,我不知道为什么,请帮助我。 public class Recursion { p
有这样的标准方式吗? 包括 Java源代码-测试代码- Ant 或 Maven联合单元持续集成(可能是巡航控制)ClearCase 版本控制工具部署到应用服务器 最后我希望有一个自动构建和集成环境。
我什至不知道这是否可能,我非常怀疑它是否可能,但如果可以,您能告诉我怎么做吗?我只是想知道如何从打印机打印一些文本。 有什么想法吗? 最佳答案 这里有更简单的事情。 import javax.swin
我是一名优秀的程序员,十分优秀!