在更复杂的单元测试中,我经常需要存在一组特定的规则。其中一些规则与另一个规则有依赖关系。由于排序是相关的,因此我使用 RuleChains。到目前为止一切都很好。
然而,这在大多数测试中都是重复的(偶尔会使用附加规则)。这种重复不仅感觉没有必要,重复起来也很麻烦,而且很多地方都需要调整,什么时候应该集成一个额外的Rule。
我想要的是规则规则,即包含或聚合其他(特定于应用程序和测试的)规则的(预定义)规则。
我将举例说明目前的情况:
public LoggingRule logRule = new LogRule();
public ConfigurationRule configurationRule = new ConfigurationRule();
public DatabaseConnectionRule dbRule = new DatabaseConnectionRule();
public ApplicationSpecificRule appRule = new ApplicationSpecificRule();
@Rule
RuleChain chain = RuleChain.outerRule(logRule)
.around(configurationRule)
.around(dbRule)
.around(appRule);
Run Code Online (Sandbox Code Playgroud)
假设给定的规则相互依赖,例如 ApplicationSpecificRule 要求首先执行 DatabaseConnectionRule 以建立连接,ConfigurationRule 已经初始化了一个空配置等。还假设对于这个(相当复杂的测试)所有规则都是实际需要。
到目前为止,我能想到的唯一解决方案是创建返回预定义 RuleChain 的工厂方法:
public class ApplicationSpecificRule extends ExternalResource
{
public static RuleChain basicSet()
{
return RuleChain.outerRule(new LogRule())
.around(new ConfigurationRule())
.around(new DatabaseConnectionRule())
.around(new ApplicationSpecificRule());
}
}
Run Code Online (Sandbox Code Playgroud)
在测试中,这可以按如下方式使用:
@Rule
RuleChain chain = ApplicationSpecificRule.basicSet();
Run Code Online (Sandbox Code Playgroud)
这样就消除了重复,并且可以轻松集成其他规则。甚至可以向该规则链添加特定于测试的规则。但是,当需要进行额外设置时,您无法访问包含的规则(假设您需要ApplicationSpecificRule为了创建某些域对象等)。
理想情况下,这将扩展为还支持使用其他预定义的集合,例如 …
在使用 Java 的相等性检查(直接或间接)时,我遇到了德语“Umlaute”(ä、ö、ü、ß)的奇怪行为。从 Eclipse 运行、调试或测试时,一切都按预期工作,并且包含“Umlaute”的输入是被视为平等或不符合预期。
但是,当我使用 Spring Boot 构建应用程序并运行它时,对于包含“Umlaute”的单词(即像“Nationalität”这样的单词),这些相等性检查将失败。
通过 Jsoup 从网页中检索输入,并为某些关键字提取表格内容。页面的编码是 UTF-8,如果不是这种情况,我已经为 Jsoup 进行了处理以将其转换。源文件的编码也是 UTF-8。
Connection connection = Jsoup.connect(url)
.header("accept-language", "de-de, de, en")
.userAgent("Mozilla/5.0")
.timeout(10000)
.method(Method.GET);
Response response = connection.execute();
if(logger.isDebugEnabled())
logger.debug("Encoding of response: " +response.charset());
Document doc;
if(response.charset().equalsIgnoreCase("UTF-8"))
{
logger.debug("Response has expected charset");
doc = Jsoup.parse(response.body(), baseURL);
}
else
{
logger.debug("Response doesn't have exepcted charset and is converted");
doc = Jsoup.parse(new String(response.bodyAsBytes(), "UTF-8"), baseURL);
}
logger.debug("Encoding of document: " +doc.charset());
if(!doc.charset().equals(Charset.forName("UTF-8")))
{
logger.debug("Changing encoding of document from " …Run Code Online (Sandbox Code Playgroud) 我需要将数字属性的类型迁移为字符串类型。为此,我编写了以下简单的查询:
MATCH (n:Entity) SET n.id=toString(n.id) RETURN n
Run Code Online (Sandbox Code Playgroud)
它匹配了大约 120 万个实体(根据 EXPLAIN),所以我没想到它会这么快。但5个多小时后仍未结束。与此同时,neo4j 服务器(社区,3.0.4)以接近 100% 的负载运行。
我在相应的 neo4j.conf 中进行了配置:
dbms.memory.heap.initial_size=4g
dbms.memory.heap.max_size=4g
dbms.jvm.additional=-XX:+UseG1GC
Run Code Online (Sandbox Code Playgroud)
仅运行几分钟后,我就可以在日志中看到有关 GarbageCollection 的报告:
[o.n.k.i.c.MonitorGc] GC Monitor: Application threads blocked for 277ms.
Run Code Online (Sandbox Code Playgroud)
后来情况变得更糟:
[o.n.k.i.c.MonitorGc] GC Monitor: Application threads blocked for 53899ms.
Run Code Online (Sandbox Code Playgroud)
最终出现了以下内容:
[o.n.b.v.r.i.c.SessionWorker] Worker for session '10774fef-eed2-4593-9a20-732d9103e576' crashed: Java heap space Java heap space
java.lang.OutOfMemoryError: Java heap space
[o.n.b.v.r.i.c.SessionWorker] Fatal, worker for session '10774fef-eed2-4593-9a20-732d9103e576' crashed. Please contact your support representative if you are unable to resolve this. Java heap space java.lang.OutOfMemoryError: …Run Code Online (Sandbox Code Playgroud) 我有以下函数,它尝试逐步缩小输入集合的范围,直到找到单个元素,即,当找到单个项目时,过滤应该停止,因为应用附加过滤器可能会导致根本不匹配。
public List<MyObject> determinePotentialCandidates(List<MyObject> allCandidates) {
List<MyObject> candidates = allCandidates.stream()
.filter(this::firstCondition)
.toList();
if (candidates.size() > 1) {
candidates = candidates.stream()
.filter(this::secondCondition)
.toList();
if (candidates.size() > 1) {
candidates = candidates.stream()
.filter(this::thirdCondition)
.collect(Collectors.toList());
}
// ... and so on
}
logResult(candidates);
return candidates;
}
Run Code Online (Sandbox Code Playgroud)
由于随着每个附加嵌套级别的增加,这变得更难阅读,我想知道是否有更简洁的方法来编写它。
优选地,该方法应该最多执行每个过滤步骤一次(尽管输入大小很小且过滤成本低廉——这可能可以对同一输入执行多次)并且包含单个出口点。
java ×3
aspectj ×1
cypher ×1
eclipse ×1
encoding ×1
gradle ×1
java-stream ×1
junit ×1
junit-rule ×1
neo4j ×1
unit-testing ×1