【问题标题】:Unsatisfied Dependency Exception in Springboot junit testSpring Boot junit测试中不满足的依赖异常
【发布时间】:2018-06-10 23:01:17
【问题描述】:

我使用的是 spring-boot 1.5.9。我创建了一个简单的 spring-boot 应用程序,它具有以下目录结构。

LibraryService.java 自动连接存储库接口,并具有将书籍保存到图书馆的方法。

图书馆服务.java

@Service
public class LibraryService  {
  private static final Logger logger = LoggerFactory.getLogger(LibraryService.class);       

  @Autowired
  BookRepository bookRepository;

  public void save(Book book){
    bookRepository.save(book);  
  } 

  public long count(){
    return bookRepository.count();
  }
}

下面是BookRepository接口

public interface BookRepository extends MongoRepository<Book, String> { }

当我在测试类中自动装配LibraryService 时,我发现它没有正确注入并且我得到了不满足的依赖异常。下面是 LibraryServiceTest 类

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
@DataMongoTest
public class LibraryServiceTest {

@Autowired
LibraryService libraryService;

@Test
public void testSave(){
Book aBook = new Book("Harry Potter" , "J. K. Rowling");
libraryService.save(aBook); 
assertEquals(1, libraryService.count());;
 }  
}

我在测试类中尝试了 @MockBean 而不是 @Autowired 并且没有异常,但是存储库实例被评估为 null 导致测试失败。使用junit时注入依赖项的正确方法是什么?

编辑

以下是堆栈跟踪:

2017-12-31 09:48:00.112 ERROR 57648 --- [           main] 
o.s.test.context.TestContextManager      : Caught exception while allowing 
TestExecutionListener
[org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener@32464a14] to prepare test instance [com.example.demo.service.LibraryServiceTest@56193c7d]

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.example.demo.service.LibraryServiceTest': Unsatisfied dependency expressed through field 'libraryService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.example.demo.service.LibraryService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:386) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:118) ~[spring-test-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83) ~[spring-test-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener.prepareTestInstance(SpringBootDependencyInjectionTestExecutionListener.java:44) ~[spring-boot-test-autoconfigure-1.5.9.RELEASE.jar:1.5.9.RELEASE]
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:230) ~[spring-test-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:228) [spring-test-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:287) [spring-test-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) [junit-4.12.jar:4.12]
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:289) [spring-test-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:247) [spring-test-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94) [spring-test-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) [junit-4.12.jar:4.12]
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) [junit-4.12.jar:4.12]
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) [junit-4.12.jar:4.12]
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) [junit-4.12.jar:4.12]
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) [junit-4.12.jar:4.12]
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) [spring-test-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) [spring-test-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.junit.runners.ParentRunner.run(ParentRunner.java:363) [junit-4.12.jar:4.12]
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191) [spring-test-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86) [.cp/:na]
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) [.cp/:na]
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) [.cp/:na]
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675) [.cp/:na]
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) [.cp/:na]
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192) [.cp/:na]
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.example.demo.service.LibraryService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1493) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
... 29 common frames omitted

【问题讨论】:

  • 当您询问异常时,请始终发布准确且完整的异常堆栈跟踪。你应该模拟你的 DAO 来测试服务。这基本上是 DI 有用的第一个原因。
  • @JBNizet 嗨,我已经添加了堆栈跟踪。

标签: java spring mongodb spring-boot junit


【解决方案1】:

来自the documentation:

可以与@RunWith(SpringRunner.class) 结合使用的注释,用于典型的 MongoDB 测试。当测试仅关注 MongoDB 组件时可以使用。

使用此注解将禁用完全自动配置,而是仅应用与 MongoDB 测试相关的配置。

因此,这会将您的服务从 spring 上下文中排除(并且无论如何都应该使用@RunWith(SpringRunner.class) 进行注释)。

使用它来测试您的 Mongo 存储库。要测试您的服务,请模拟存储库(这就是 DI 首先很有用的原因),并在模拟存储库上测试服务内部的业务逻辑。

【讨论】:

  • 谢谢。我删除了 @DataMongoTest 注释,它成功了。
猜你喜欢
  • 2018-09-29
  • 1970-01-01
  • 2018-10-03
  • 2018-12-19
  • 2021-04-08
  • 1970-01-01
  • 1970-01-01
  • 2022-11-11
  • 2018-08-27
相关资源
最近更新 更多