SpringBoot2.0单元测试中缺少依赖项

时间:2018-04-30 10:19:06

标签: unit-testing spring-boot dependency-injection spring-boot-test

我正在尝试对SpringBoot应用程序进行单元测试。正常的应用程序执行工作正常,但由于缺少依赖项,我无法执行我的测试。我尝试了很多注释组合,我也尝试了@ContextConfiguration(我认为这可能是“问题”),但我无法让它工作。我试图在我的@ContextConfoguration aqnnotation中包含Locker.class,但这对alsp没有帮助。你能帮助我吗? 这是我的测试类的摘录:

@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration
public class LockerServiceImplTest {

  @InjectMocks
  private LockerServiceImpl service;

  @Mock
  private LockerRepository repo;

  @Autowired
  private Locker locker;

  @Test
  public void testPersist() {

    this.locker.setAppId("mytestappid");
    this.locker.setId(0);
    this.locker.setLastActivityTimestamp(System.currentTimeMillis());
    this.locker.setLocked(true);

    // configure the mocks
    Mockito.doNothing().when(this.repo).update(this.locker.getLastActivityTimestamp(), true);
    Mockito.doNothing().when(this.repo).update(this.locker.getLastActivityTimestamp(), false);
    Mockito.when(this.repo.save(this.locker)).thenReturn(this.locker);

    Locker persisted = this.service.persist(this.locker);
    assertEquals(this.locker, persisted);
  }

作为错误消息,我得到了一个针对字段锁定器的UnsatisfiedDependencyInjection。 非常感谢提前!

更新

My Locker类确实没有bean,所以这导致了我的依赖性问题。现在它似乎解决了直接依赖关系,但没有传递依赖关系。 例如:在我的测试中,我使用LockerService类的对象,它使用LockerRepository。这些依赖项得到解决,但LockerService也使用了类AppProperties的对象,该对象无法解析并导致NullPointerException。

  

java.lang.NullPointerException at   my.app.datasupply.dataaccess.LockerServiceImpl.persist(LockerServiceImpl.java:39)     在   my.app.datasupply.dataaccess.LockerServiceImplTest.testPersist(LockerServiceImplTest.java:48)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)     在   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在java.lang.reflect.Method.invoke(Method.java:498)at   org.junit.runners.model.FrameworkMethod $ 1.runReflectiveCall(FrameworkMethod.java:50)     在   org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)     在   org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)     在   org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)     在   org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:73)     在   org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:83)     在   org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)     在   org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)     在   org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)     在org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)at   org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)     在   org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)     在org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:290)at at   org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:71)at at   org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)at at   org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:58)at at   org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:268)at at   org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)     在   org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)     在org.junit.runners.ParentRunner.run(ParentRunner.java:363)at   org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)     在   org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)     在   org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)     在   org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)     在   org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)     在   org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)     在   org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

0 个答案:

没有答案