Spring boot test configuration

I have a spring boot application with a main class as shown below:

@SpringBootApplication public class Application { public static void main(String[] args) throws Exception { SpringApplication.run(Application.class, args); } } 

Now I want to test my services and create a base test class:

 @SpringApplicationConfiguration(Application.class) public abstract class TestBase { } 

When I run my test, I get an exception:

 Caused by: java.lang.IllegalArgumentException: Can not load an ApplicationContext with a NULL 'contextLoader'. Consider annotating your test class with @ContextConfiguration. at org.springframework.util.Assert.notNull(Assert.java:115) at org.springframework.test.context.TestContext.loadApplicationContext(TestContext.java:117) at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:148) 

Then I change the base test class using ContextConfiguration

 @ContextConfiguration(classes = Application.class) public abstract class TestBase { } 

This time I get a DataSource initialization error. I wonder why this happens in the first case and why in the second case it does not load my .properties applications, where I configured the data source.

Thanks!

+5
source share
3 answers
 @RunWith(SpringRunner.class) @SpringBootTest(classes="Application.class") public class ApplicationTest{ @Autowire Foo foo //whatever you are testing @Test public void FooTest() throws Exception{ Foo f = foo.getFooById("22"); assertEquals("9B". f.getCode); } Something like that //TODO look into MockMVC for testing services } 
+2
source

I ran into the same problem because my ServletInitializer was in a different package. The issue is resolved after fixing the package structure.

0
source

All Articles