Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SpringRunner unable to detect configuration

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...

like image 865
Raghuveer Avatar asked Sep 11 '18 10:09

Raghuveer


People also ask

What does @RunWith SpringRunner class mean?

@RunWith(SpringRunner. class) tells JUnit to run using Spring's testing support. SpringRunner is the new name for SpringJUnit4ClassRunner , it's just a bit easier on the eye. @SpringBootTest is saying “bootstrap with Spring Boot's support” (e.g. load application.

What is @ContextConfiguration in spring?

@ContextConfiguration defines class-level metadata that is used to determine how to load and configure an ApplicationContext for integration tests.

What is the use of @SpringBootTest?

Spring Boot provides a @SpringBootTest annotation, which can be used as an alternative to the standard spring-test @ContextConfiguration annotation when you need Spring Boot features. The annotation works by creating the ApplicationContext used in your tests through SpringApplication .

How do you use <UNK>RunWith SpringRunner class?

SpringRunner is an alias for the SpringJUnit4ClassRunner . To use this class, simply annotate a JUnit 4 based test class with @RunWith(SpringRunner. class) . If you would like to use the Spring TestContext Framework with a runner other than this one, use org.


2 Answers

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

@ContextConfiguration(classes = ElastSearchBootApplication.class)
like image 110
Gaurav Srivastav Avatar answered Oct 28 '22 07:10

Gaurav Srivastav


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()));
  }
like image 34
borino Avatar answered Oct 28 '22 08:10

borino