@DataNeo4jTest 的 UnsatisfiedDependencyException

UnsatisfiedDependencyException for @DataNeo4jTest

我尝试使用 Spring-Boot Neo4j 测试我的代码,但我遇到了类似 org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.sha.neo4j.service.UserServiceTest': Unsatisfied dependency expressed through field 'userService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.sha.neo4j.service.UserService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

的错误

依赖项;

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-neo4j'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    compileOnly 'org.projectlombok:lombok'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
    testImplementation 'org.neo4j.test:neo4j-harness:4.0.0'
    testImplementation 'org.neo4j:neo4j-ogm-embedded-driver:3.2.8'
}

并测试class;

@RunWith(SpringRunner.class)
@DataNeo4jTest
public class UserServiceTest
{
    @Autowired
    private UserService userService;

    @Test
    public void testIt()
    {
        User user = new User();
        user.setLastName("Test");
        user.setName("Test");
        userService.saveUser(user);

        List<User> users = userService.findAll();

        assertThat(users).hasSize(1);
    }
}

在这里,我只是尝试使用嵌入式驱动程序测试上面的代码,但出现了如上所示的错误。我没有用于测试的特定属性 (test application.properties)。测试适用于 neo4j-desktop 螺栓驱动器。

有什么建议吗?

您应该在测试中使用 @SpringBootTest@DataNeo4jTest 未加载任何必需的服务。