我尝试使用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'
}
测试类;
@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);
}
}
在这里,我已经尝试使用嵌入式驱动程序测试上述代码,但是出现了类似上面的错误。我没有要测试的特定属性(测试application.properties)。使用neo4j-desktop螺栓驱动程序进行测试。
有什么建议吗?
答案 0 :(得分:1)
您应该在测试中使用@SpringBootTest
。 @DataNeo4jTest
未加载任何必需的服务。