0

I´m trying to unit-test a SpringBoot application. Normal application execution works just fine, but I cannot execute my tests due to missing dependencies. I´ve tried many combinations of annotations and I´ve also experimented with the @ContextConfiguration (which I think might be the "problem" here), but I cannot get it working. I tried to include the Locker.class in my @ContextConfoguration aqnnotation, but this did not help alsp. Can you help me? Here´s an extract of my test class:

@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);
  }

As error message I get an UnsatisfiedDependencyInjection for field locker. Many thanks in advance!

UPDATE

My Locker class was indeed no bean so that this led to the dependency problems I had. Now it seems to resolve direct dependencies, but no transitive dependencies. Eample: In my test I use an object of class LockerService which uses a LockerRepository. These dependencies get resolved, but LockerService also uses an object of class AppProperties which gets not resolved and leads to a NullPointerException.

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

EllisTheEllice
  • 905
  • 3
  • 14
  • 33
  • 1
    In what packages live `LockerServiceImplTest` and the class that has `SpringBootApplication` annotation? – Nikem Apr 30 '18 at 10:22
  • LockerServiceImplTest resides in _my.app.datasupply.dataaccess_ My main application class resides in _my.app.datasupply_ – EllisTheEllice Apr 30 '18 at 10:32
  • 1
    You should remove `ContextConfiguration` from your test and make sure, that you indeed have a bean of type `Locker` – Nikem Apr 30 '18 at 10:36
  • I already tried that, but that did not work also. Locker is an actually existing Bean. It is used by and working in "normal" application execution. – EllisTheEllice Apr 30 '18 at 11:54
  • Then please provide the full exception stack trace – Nikem Apr 30 '18 at 12:27
  • [Nikem](https://stackoverflow.com/users/301650/nikem), I have edited my question and added the last part of my stacktrace. Indeed I´ve made a mistake and my Locker was not a Bean (which I solved now). – EllisTheEllice May 02 '18 at 07:14
  • Please create a new question for this new answer and provide all relevant information. Including the relevant parts of `LockerServiceImpl` – Nikem May 02 '18 at 07:30

0 Answers0