我尝试用Spring 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);
}
}在这里,我尝试用嵌入式驱动程序测试上面的代码,但是我发现了一个类似于上面的错误。我没有用于测试的特定属性(test application.properties)。测试工作与新4j桌面螺栓驱动程序。
有什么建议吗?
发布于 2020-02-21 07:57:46
您应该在测试中使用@SpringBootTest。@DataNeo4jTest不加载任何必需的服务。
https://stackoverflow.com/questions/60332869
复制相似问题