在为Neo4j编写测试用例时,我想继续使用JUnit 5扩展模型,而不是使用 org.junit.vintage
或 junit-jupiter-migrationsupport
. 目前我只能找到使用 TestRule
的JUnit 4的Neo4j测试线束,它依赖于 org.junit.vintage
和 junit-jupiter-migrationsupport
.
是否有使用扩展模型的JUnit 5的Neo4j测试工具?
参考文献:
Neo4j:Home,GitHub
Neo4j test-harness
:Maven,GitHub,pom.xml
JUnit 4:GitHub
JUnit 4 TestRule
:JUnit 4 Guide,JUnit 4.12 API,Neo4jRule GitHub
JUnit 5:GitHub
JUnit 5 Extension Model
:JUnit 5 User Guide,GitHub
JUnit 5 org.junit.vintage
:JUnit 5 User Guide,Test-harness pom.xml
JUnit 5 junit-jupiter-migrationsupport
:JUnit 5 User Guide,Test-harness pom.xml
我知道可以在混合环境中使用JUnit 4和JUnit 5,例如Mixing JUnit 4 and JUnit 5 tests .
我已经开始在A Guide to JUnit 5 Extensions的帮助下编写我自己的Neo4j JUnit 5扩展,但是如果已经存在具有JUnit 5扩展模型的标准Neo4j测试工具,为什么要创建我自己的 .
可能是因为我只是使用错误的关键字来查询,这些关键字只是 neo4j
和 JUnit 5
但是相同的结果不断出现,这些都没有导致我所寻求的 .
检查了JUnit Jupiter Extensions,发现没有Neo4j .
编辑
概念证明
由于以下代码仅是概念证明,因此不会将其作为已接受的答案发布,但希望在几天内完成 .
事实证明,将JUnit 5 Jupiter Extensions添加到现有的JUnit TestRlue并不是那么糟糕 . 沿途有一些粗糙的地方,如果你像我一样,不会生活和呼吸一种编程语言或一套工具,你必须花一些时间来理解这种精神;如果你问我,那应该是SO标签 .
注意:此代码是Neo4j TestRule和A Guide to JUnit 5 Extensions的一些代码的组合
从Neo4j开始TestRule只需更改工具:
删除 TestRule
添加 BeforeEachCallback
和 AfterEachCallback
注意:使用 BeforeEach
和 AfterEach
代替 BeforeAll
和 AfterAll
与Neo4j,因为在创建节点时每次新测试,如果创建的新节点与上一个测试相同,并且数据库不是新数据库,则检查节点的id会有所不同,因为为每个测试创建了一个新节点并获得了不同的id . 因此,要避免此问题并按照与Neo4j TestRule相同的方式执行此操作,将为每个测试实例创建一个新数据库 . 我确实考虑过在测试之间重置数据库,但似乎唯一的方法是删除构成数据库的所有文件 . :(
/*
* Copyright (c) 2002-2018 "Neo4j,"
* Neo4j Sweden AB [http://neo4j.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//package org.neo4j.harness.junit;
package org.egt.neo4j.harness.example_002.junit;
// References:
// GitHub - junit-team - junit5 - junit5/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine - https://github.com/junit-team/junit5/tree/releases/5.3.x/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/extension
// Notes:
// With JUnit 4 TestRule there was basically one rule that was called at multiple points and for multiple needs.
// With JUnit 5 Extensions the calls are specific to a lifecycle step, e.g. BeforeAll, AfterEach,
// or specific to a need, e.g. Exception handling, maintaining state across test,
// so in JUnit 4 where a single TestRule could be created in JUnit5 many Extensions need to be created.
// Another major change is that with JUnit 4 a rule would wrap around a test which would make
// implementing a try/catch easy, with JUnit 5 the process is broken down into a before and after callbacks
// that make this harder, however because the extensions can be combined for any test,
// adding the ability to handle exceptions does not require adding the code to every extension,
// but merely adding the extension to the test. (Verify this).
import java.io.File;
import java.io.PrintStream;
import java.util.function.Function;
import org.junit.jupiter.api.extension.*;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.config.Setting;
import org.egt.neo4j.harness.example_002.ServerControls;
import org.egt.neo4j.harness.example_002.TestServerBuilder;
import org.egt.neo4j.harness.example_002.TestServerBuilders;
/**
* A convenience wrapper around {@link org.neo4j.harness.TestServerBuilder}, exposing it as a JUnit
* {@link org.junit.Rule rule}.
*
* Note that it will try to start the web server on the standard 7474 port, but if that is not available
* (typically because you already have an instance of Neo4j running) it will try other ports. Therefore it is necessary
* for the test code to use {@link #httpURI()} and then {@link java.net.URI#resolve(String)} to create the URIs to be invoked.
*/
//public class Neo4jRule implements TestRule, TestServerBuilder
public class Neo4jDatabaseSetupExtension implements BeforeEachCallback, AfterEachCallback, TestServerBuilder
{
private TestServerBuilder builder;
private ServerControls controls;
private PrintStream dumpLogsOnFailureTarget;
Neo4jDatabaseSetupExtension(TestServerBuilder builder )
{
this.builder = builder;
}
public Neo4jDatabaseSetupExtension( )
{
this( TestServerBuilders.newInProcessBuilder() );
}
public Neo4jDatabaseSetupExtension(File workingDirectory )
{
this( TestServerBuilders.newInProcessBuilder( workingDirectory ) );
}
@Override
public void afterEach(ExtensionContext context) throws Exception {
if (controls != null)
{
controls.close();
}
}
@Override
public void beforeEach(ExtensionContext context) throws Exception {
controls = builder.newServer();
}
@Override
public ServerControls newServer() {
throw new UnsupportedOperationException( "The server cannot be manually started via this class, it must be used as a JUnit 5 Extension." );
}
@Override
public TestServerBuilder withConfig(Setting<?> key, String value) {
builder = builder.withConfig( key, value );
return this;
}
@Override
public TestServerBuilder withConfig(String key, String value) {
builder = builder.withConfig( key, value );
return this;
}
@Override
public TestServerBuilder withExtension(String mountPath, Class<?> extension) {
builder = builder.withExtension( mountPath, extension );
return this;
}
@Override
public TestServerBuilder withExtension(String mountPath, String packageName) {
builder = builder.withExtension( mountPath, packageName );
return this;
}
@Override
public TestServerBuilder withFixture(File cypherFileOrDirectory) {
builder = builder.withFixture( cypherFileOrDirectory );
return this;
}
@Override
public TestServerBuilder withFixture(String fixtureStatement) {
builder = builder.withFixture( fixtureStatement );
return this;
}
@Override
public TestServerBuilder withFixture(Function<GraphDatabaseService, Void> fixtureFunction) {
builder = builder.withFixture( fixtureFunction );
return this;
}
@Override
public TestServerBuilder copyFrom(File sourceDirectory) {
builder = builder.copyFrom( sourceDirectory );
return this;
}
@Override
public TestServerBuilder withProcedure(Class<?> procedureClass) {
builder = builder.withProcedure( procedureClass );
return this;
}
@Override
public TestServerBuilder withFunction(Class<?> functionClass) {
builder = builder.withFunction( functionClass );
return this;
}
@Override
public TestServerBuilder withAggregationFunction(Class<?> functionClass) {
builder = builder.withAggregationFunction( functionClass );
return this;
}
}
接下来,为了让每个测试实例都有一个新的 GraphDatabaseService
,它是用 ServerControls
创建的,它实现了一个JUnit 5 ParameterResolver .
package org.egt.neo4j.harness.example_002.junit;
import org.egt.neo4j.harness.example_002.ServerControls;
import org.egt.neo4j.harness.example_002.TestServerBuilders;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.ParameterContext;
import org.junit.jupiter.api.extension.ParameterResolutionException;
import org.junit.jupiter.api.extension.ParameterResolver;
public class Neo4jDatabaseParameterResolver implements ParameterResolver {
@Override
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException {
boolean result = parameterContext.getParameter()
.getType()
.equals(ServerControls.class);
return result;
}
@Override
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException {
Object result = (ServerControls)TestServerBuilders.newInProcessBuilder().newServer();
return result;
}
}
最后剩下的就是使用带有 @ExtendWith
和 @Test
的Neo4j JUnit 5扩展模型:
package org.egt.example_002;
import org.egt.neo4j.harness.example_002.ServerControls;
import org.egt.neo4j.harness.example_002.junit.Neo4jDatabaseParameterResolver;
import org.egt.neo4j.harness.example_002.junit.Neo4jDatabaseSetupExtension;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Transaction;
import static org.junit.jupiter.api.Assertions.assertEquals;
@ExtendWith({ Neo4jDatabaseSetupExtension.class, Neo4jDatabaseParameterResolver.class })
public class Neo4jUnitTests {
private ServerControls sc;
private GraphDatabaseService graphDb;
public Neo4jUnitTests(ServerControls sc) {
this.sc = sc;
this.graphDb = sc.graph();
}
@Test
public void shouldCreateNode()
{
// START SNIPPET: unitTest
Node n;
try ( Transaction tx = graphDb.beginTx() )
{
n = graphDb.createNode();
n.setProperty( "name", "Nancy" );
tx.success();
}
long id = n.getId();
// The node should have a valid id
assertEquals(0L, n.getId());
// Retrieve a node by using the id of the created node. The id's and
// property should match.
try ( Transaction tx = graphDb.beginTx() )
{
Node foundNode = graphDb.getNodeById( n.getId() );
assertEquals( foundNode.getId(), n.getId() );
assertEquals( "Nancy" , (String)foundNode.getProperty("name") );
}
// END SNIPPET: unitTest
}
}
我在这方面学到的一个重要的事情就是TestRule代码似乎是 do everything in one class
而新的扩展模型使用许多扩展来做同样的事情 . 因此,Neo4j TestRule所具有的日志记录,异常处理和其他功能都不在此概念证明中 . 但是,因为扩展模型允许您混合和匹配扩展,所以添加日志记录和异常处理可以像使用来自其他地方的扩展一样简单,只需添加 @ExtendWith
这就是我没有为此概念证明创建它们的原因 .
另外你会注意到我改变了我所做的包名,只是为了避免与同一个项目中的其他代码冲突,以独立的方式实现代码的其他部分,这样我就可以走到这个工作的概念证明 .
最后,如果JUnit 4 Neo4j TestRule类和JUnit 5扩展模型类都可以从基类继承然后在相同的测试工具中可用,我不会感到惊讶 . fingers crossed . 显然,大多数基类都是从Neo4j TestRule类中提取的 .
1 回答
不是一个答案,但我有一个Neo4jExtension类,它是一个JUnit 5扩展 .
我的大多数都是硬编码的,因为我想要一些对我有用的东西 .
它创建了一个带螺栓连接器的嵌入式Neo4j数据库 .
它还加载一些apoc过程和函数,并加载测试的初始数据 .
你的方法更有趣 .