How to use @JsonTest when there is no @SpringBootApplication?

Viewed 257

I want to use @JsonTest to write tests on JSON serialization for my library. However, when I add the annotation to a test, I get:

Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test

As this is a library, I indeed don't have a main class annotated with @SpringBootConfiguration. How can I avoid that I need to introduce this just to get the testing framework going?

2 Answers

Seems the only way is to actually add a dummy application. In my case, I added this into src/test/java to avoid it will end up in the production sources. So something like this:

import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * Just created this here to make @JsonTest work
 */
@SpringBootApplication
public class DummyApplication {
}
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan
public class SpringTestContext {

}

Put this in your library module, located in root test dir, This will supply a ApplicationContext for your tests.

Related