SpringRunner unable to detect configuration

Viewed 32548

I have a spring-boot application for which am trying to create unit testcases. Below is the code that I am trying to run, I don't have any configuration file that I have (used only annotations) so the main class that loads all the configuration is ElastSearchBootApplication class. For some reason I see the below error.

@ComponentScan(basePackages = "com.somename")
@SpringBootApplication
@EnableScheduling
public class ElastSearchBootApplication {

    private static final Logger LOG = LoggerFactory.getLogger(ElastSearchBootApplication.class);

    public static void main(String[] args) {
        SpringApplication.run(ElastSearchBootApplication.class, args);
    }

    @Autowired
    private ElastSearchLogLevel logsSearch;

    @Scheduled(fixedRate = 120000)
public void scheduledSearchLogs() {
        ...

Test class :

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = ElastSearchBootApplication.class)
public class LogSearchTest {

    @Mock
    private RestHighLevelClient client;
    @Mock
      private ExecutorService ALERT_POOL;

    @Before
    public void setUp() throws Exception {
          client = mock(RestHighLevelClient.class);
        ALERT_POOL = mock(ExecutorService.class);

        try {
            when(client.search(anyObject())).thenReturn(getResponse());
        } catch (Exception e) {
            // I see NullPointerException but both the instances are available here
            e.printStackTrace();
        }
        doNothing().when(ALERT_POOL.invokeAll(anyObject()));
    }

I see the below error when trying to run the spring-boot test :

org.springframework.boot.test.context.SpringBootTestContextBootstrapper buildDefaultMergedContextConfiguration
INFO: Neither @ContextConfiguration nor @ContextHierarchy found for test class [com.somename.search.LogSearchTest], using SpringBootContextLoader
org.springframework.test.context.support.AbstractContextLoader generateDefaultLocations
INFO: Could not detect default resource locations for test class [com.somename.search.LogSearchTest]: no resource found for suffixes {-context.xml, Context.groovy}.
org.springframework.test.context.support.AnnotationConfigContextLoaderUtils detectDefaultConfigurationClasses
INFO: Could not detect default configuration classes for test class [com.somename.search.LogSearchTest]: LogSearchTest does not declare any static, non-private, non-final, nested classes annotated with @Configuration.
  1. I see that @SpringBootTest is used for integration tests, so can I use it for unit tests ? If I remove it then I get another set of exception that looks similar though. I would be more interested in running this testcase without SpringBootTest

  2. Why my test case say some configuration is missing. The samples online talk about xml files which I don't have. So what am I missing here ?

  3. Can I dynamically pass the value for fixedRate from Environment and put it like @Scheduled(fixedRate = ${some.value.defined})

UPDATE

I can run the test but without the proper order. Ideally i expect setUp to run first. But its running second. Also the line when(client.search(anyObject())).thenReturn(getResponse()); is failing and i dont get the reason...

2 Answers

You have to add the annotation @ContextConfiguration to your test class to specify configuration file.

@ContextConfiguration(classes = ElastSearchBootApplication.class)

Try this:

@RunWith(SpringRunner.class)
@SpringBootTest
public class LogSearchTest {

  @MockBean
  private RestHighLevelClient client;
  @MockBean
  private ExecutorService ALERT_POOL;

  @Before
  public void setUp() throws Exception {
   try {
        when(client.search(anyObject())).thenReturn(getResponse());
    } catch (Exception e) {
        // I see NullPointerException but both the instances are available here
        e.printStackTrace();
    }
    doNothing().when(ALERT_POOL.invokeAll(anyObject()));
  }
Related