Spring bean from test profile in JUnit 5 test class is not injected

1668 views java

I would like to use in my jUnit 5 test class

class WorkreportDbRepositoryTest {

  private SystemPriceSettingService systemPriceSettingService;

// the rest omitted .... 


a bean created in configuration for testing environment:

public class TestConfig {

  public SystemPriceSettingService systemPriceSettingService(){
    return new MemoryPriceSettingService();


But the SystemPriceSettingService bean is not injected. What's wrong with my setup?

answered question

You forgot to annotate it with @ExtendWith(SpringExtension.class)

1 Answer


You don't use a JUnit Runner that is Spring aware. So no Spring context is created.
You should replace it the annotations on the test class such as :

import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.test.context.junit.jupiter.SpringExtension;

class WorkreportDbRepositoryTest { ...}

And add this dependency to be able to use SpringExtension :

    <version>2.18.0</version> <!-- your mockito version-->

posted this

Have an answer?


Please login first before posting an answer.