1

我是弹簧靴的新手。我使用 Spring Boot 1.5.1 GA 和 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());
    }

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

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-test 间接拉动了码头服务器 9.4.1。知道什么会导致这个问题吗?

4

3 回答 3

1

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

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

对于 Maven,我想它会是:

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

请注意,9.2.9.v20150224应替换为您正在使用的 Neo4j 版本所需的任何可传递版本的 Jetty。

于 2017-08-15T13:25:45.987 回答
1

似乎您缺少对嵌入式驱动程序的依赖。如果您将以下内容添加到您的 POM 中,它会起作用吗?

    <dependency>
        <groupId>org.neo4j</groupId>
        <artifactId>neo4j-ogm-embedded-driver</artifactId>
        <version>${neo4j-ogm.version}</version>
        <scope>test</scope>
    </dependency>
于 2017-02-15T22:57:41.357 回答
1

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

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

  • neo4j-* 版本 3.1.2
  • org.eclipse.jetty.* 版本 2.9.2 - spring 坚持让我使用 2.3.*

Spring-boot 想让我运行 jetty-* 2.3.* SessionManager 因某种原因被删除。

于 2017-03-29T13:06:31.180 回答