当前位置: 首页 > 知识库问答 >
问题:

Neo4j的Spring boot集成测试失败

鲁涵映
2023-03-14

我对Spring靴还不熟悉。我使用Spring靴1.5.1 GA和Spring靴的Neo4j启动器。我试图创建我的第一个集成测试,以确定是否可以将新对象插入到图形数据库中。这是我的测试课:

package hu.bookandwalk;
import static org.junit.Assert.assertEquals;
import java.time.LocalDateTime;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.neo4j.ogm.testutil.TestServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import hu.bookandwalk.domain.Address;
import hu.bookandwalk.domain.InvoiceData;
import hu.bookandwalk.domain.User;
import hu.bookandwalk.repository.UserRepository;
import java.util.HashSet;
import java.util.Set;

@ContextConfiguration(classes = {PersistenceTestContext.class})
@RunWith(SpringRunner.class)
@SpringBootTest
public class BnWEbookstoreApplicationTests {

    private static TestServer testServer;

    @Autowired
    UserRepository userRepository;

    @BeforeClass
    public static void setupTestServer() {
        /* just a port without any intention */
        testServer = new TestServer.Builder().build();
    }

    @AfterClass
    public static void stopTestServer() {
       testServer.shutdown();
    }

    @Test
    public void ShouldInsertUser() {
        Address a=new Address("Hungary","Budapest","1092","Erkel street 9",LocalDateTime.now());
        InvoiceData invoiceData=new InvoiceData("BandW", "233456", true, a, LocalDateTime.now());

        Set<InvoiceData> invoiceDatas=new HashSet<InvoiceData>();
        invoiceDatas.add(invoiceData);
        Set<Address> addresses=null;

        User u=new User(1l,"Roland","email@gmail.com","pwd",true,LocalDateTime.now(), true,invoiceDatas,addresses);
        userRepository.save(u);
        assertEquals(1l, userRepository.count());
    }

PeristentTestContext专门用于测试数据库操作。在这个类中,我设置了用于测试的嵌入式驱动程序:

package hu.bookandwalk;

import org.neo4j.ogm.drivers.embedded.driver.EmbeddedDriver;
import org.neo4j.ogm.session.SessionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.neo4j.repository.config.EnableNeo4jRepositories;
import org.springframework.data.neo4j.transaction.Neo4jTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableNeo4jRepositories("org.neo4j.cineasts.repository")
@EnableTransactionManagement
@ComponentScan("hu.bookandwalk")
public class PersistenceTestContext {
    @Bean
    public org.neo4j.ogm.config.Configuration configuration() {
        final org.neo4j.ogm.config.Configuration configuration = new org.neo4j.ogm.config.Configuration();
        configuration.driverConfiguration().setDriverClassName(EmbeddedDriver.class.getName());
        return configuration;
    }
@Bean
public SessionFactory sessionFactory() {
    return new SessionFactory(configuration(), "hu.bookandwalk.domain");
}

@Bean
public PlatformTransactionManager transactionManager() {
    return new Neo4jTransactionManager(sessionFactory());
}

}

不幸的是,当我从STS IDE运行测试时,甚至在测试可以验证之前,我就收到了以下错误消息:

java.lang.NoClassDefFoundError: org/eclipse/jetty/server/SessionManager
    at org.neo4j.server.CommunityNeoServer.createWebServer(CommunityNeoServer.java:89)
    at org.neo4j.server.AbstractNeoServer.init(AbstractNeoServer.java:181)
    at org.neo4j.server.AbstractNeoServer.start(AbstractNeoServer.java:196)
    at org.neo4j.harness.internal.InProcessServerControls.start(InProcessServerControls.java:72)
    at org.neo4j.harness.internal.AbstractInProcessServerBuilder.newServer(AbstractInProcessServerBuilder.java:137)
    at org.neo4j.ogm.testutil.TestServer.startServer(TestServer.java:73)
    at org.neo4j.ogm.testutil.TestServer.<init>(TestServer.java:55)
    at org.neo4j.ogm.testutil.TestServer.<init>(TestServer.java:37)
    at org.neo4j.ogm.testutil.TestServer$Builder.build(TestServer.java:239)
    at hu.bookandwalk.BnWEbookstoreApplicationTests.setupTestServer(BnWEbookstoreApplicationTests.java:36)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.lang.ClassNotFoundException: org.eclipse.jetty.server.SessionManager
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 29 more

java.lang.NullPointerException
    at hu.bookandwalk.BnWEbookstoreApplicationTests.stopTestServer(BnWEbookstoreApplicationTests.java:41)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:33)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

当我创建我的项目时,我使用了Spring Starter项目模板。据我所知,除了Spring Boot放置的依赖项之外,我没有将任何其他依赖项放入我的pom.xml中。我只添加了一些属性,例如让thymeleaf使用版本3而不是2。您可以在此处看到我的pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>BnWEbookstore</name>
<description>Book and Walk e-book webáruház</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.1.RELEASE</version>
    <relativePath /> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
    <thymeleaf.version>3.0.3.RELEASE</thymeleaf.version>
    <thymeleaf-layout-dialect.version>2.1.2</thymeleaf-layout-dialect.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-cache</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-neo4j</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-rest</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-rest-hal-browser</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-hateoas</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-mail</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-mobile</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-social-facebook</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-social-twitter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.thymeleaf</groupId>
        <artifactId>thymeleaf-spring4</artifactId>
    </dependency>
    <dependency>
        <groupId>org.thymeleaf.extras</groupId>
        <artifactId>thymeleaf-extras-java8time</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-validation</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.neo4j</groupId>
        <artifactId>neo4j-ogm-test</artifactId>
        <version>2.1.1</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

<repositories>
    <repository>
        <id>spring-snapshots</id>
        <name>Spring Snapshots</name>
        <url>https://repo.spring.io/snapshot</url>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>
    <repository>
        <id>spring-milestones</id>
        <name>Spring Milestones</name>
        <url>https://repo.spring.io/milestone</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
</repositories>
<pluginRepositories>
    <pluginRepository>
        <id>spring-snapshots</id>
        <name>Spring Snapshots</name>
        <url>https://repo.spring.io/snapshot</url>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </pluginRepository>
    <pluginRepository>
        <id>spring-milestones</id>
        <name>Spring Milestones</name>
        <url>https://repo.spring.io/milestone</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </pluginRepository>
</pluginRepositories>

当我检查传递依赖关系时,我看到neo4j ogm测试间接拉动了jetty服务器9.4.1。知道是什么导致了这个问题吗?

共有3个答案

微生耘豪
2023-03-14

Spring Boot的默认版本Jetty较新,并且与Neo4j的嵌入式Jetty服务器所需的版本不兼容。您可以通过强制使用Spring Boot使用的Jetty版本来解决此问题。要使用Gradle执行此操作,您需要在build.gradle中指定以下内容:

ext['jetty.version'] = '9.2.9.v20150224'

对马文来说,我想应该是:

<jetty.version>9.2.9.v20150224</jetty.version>

请注意<代码>9.2.9。v20150224应替换为您使用的Neo4j版本所需的Jetty的任何可传递版本。

漆雕深
2023-03-14

获取有效设置的快捷方式是在本地安装neo4j并检查 /lib目录以找到neo4j版本和jetty-server之间的良好匹配

我在运行spring-data-neo4j的“香草”安装时也遇到了同样的错误。根据neo4j的默认安装,我可以发现以下兼容版本:

  • neo4j-*版本3.1.2

Spring Boot希望我运行jetty-*2.3.*,SessionManager由于某种原因被删除了。

方权
2023-03-14

似乎缺少对嵌入式驱动程序的依赖。如果您将以下内容添加到POM中,是否有效?

    <dependency>
        <groupId>org.neo4j</groupId>
        <artifactId>neo4j-ogm-embedded-driver</artifactId>
        <version>${neo4j-ogm.version}</version>
        <scope>test</scope>
    </dependency>
 类似资料:
  • 我有一个SpringBoot应用程序,它有一个控制器类、一个服务和一个存储库,运行得非常好。我已经为相同的应用程序添加了Junit测试用例,而且效果也非常好。 下面是测试类。 测试用例一直通过,但目前我正在添加另一个API,如下所示,所有测试都失败了。 下面是测试类。 添加部门服务后,测试用例失败,错误如下: 干杯!

  • 我正在使用一个带有spring boot 2.0.0.rc1的多项目分级器。我的子项目之一是SpringBoot应用程序,其中包含了我的集成测试。 集成测试用WebEnvironment.random_port标记为@springboottest。由于未解析的依赖关系(在另一个子项目中声明的服务,的同级),测试失败,使用了gradle命令行,但在Eclipse IDE中成功。 如果有人有主意?如何

  • 我有几个繁重的Spring集成测试(是的,这不是最好的方法,我没有时间正确地模拟所有外部dep) 下面是测试的典型注释 由于以下原因,测试会定期失败: 这里有两个问题:1、让测试共存的正确方式是什么?我在surefire插件中设置了forkCount=0。好像有帮助 2.1. 在每次测试期间,我实际上不需要启动所有的

  • 已删除MyTestConfig.class,但问题仍然相同。即使我使用@SpringBootTest(classes={Application.Class,MyProblematicServiceImpl.Class}),它仍然在自动连线的地方返回模拟对象。MyProblematicServiceImpl是用@Service注释的空类。

  • 与@mockbean和@spybean一样,有没有类似于@fakebean/@dummybean的东西? 其思想是,该实例是100%真实的(具有预期的生产内部状态),并且它覆盖(或者添加bean,以防在配置中没有声明)上下文中的bean。理想情况下,您不需要创建TestConfiguration类并将其设置为Primary,因为这样可以在每个测试的基础上控制假冒,只有在需要时才可以。否则它使用主的