Override application.properties for integration tests in spring-boot app - java

I have a standard spring-boot app and I want to use MS SQL database for the production environment, whereas for integration tests I'd like to use h2 databse. The problem is that I wasn't able to find out, how to override the default application.properties file. Even though I was trying to follow some tutorials, I didn't come up with the right solution...maybe I'm just missing something...
The main class:
#SpringBootApplication
#EnableTransactionManagement
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication .class, args);
}
}
and the class with tests:
#RunWith(SpringJUnit4ClassRunner.class)
#SpringApplicationConfiguration(classes = MyApplication.class)
#WebIntegrationTest
public class MessageControllerTest {
#Autowired
MessageRepository messageRepository;
...
...
...
#Test
public void testSomething(){
...
...
...
...
}
}
So the question is, how to force the spring-boot to use application-test.properties file when running the tests, instead of application.properties, which should be used during the run time.
I tried for example to replace #WebIntegrationTest annotation with #TestPropertySource(locations="classpath:application-test.properties"), but this results in java.lang.IllegalStateException: Failed to load ApplicationContext.

Assuming you have a application-test.properties file in your app.
I do it in two ways :
1.CLI JVM Args
mvn spring-boot:run -Drun.jvmArguments="-Dspring.profiles.active=test
add the application-test.properties as an active profile.
add the spring.profiles.active=test in the application.properties and it will load your application-test.properties file.
As you pointed to in your answer annotate a class test with a specific active profile ( which is not suitable when having a large test classes i think ) #ActiveProfiles("test")

Actually it was pretty easy...after several hours of trying, I've realized that I just needed to annotate my test class with #ActiveProfiles("test") annotation.
#ActiveProfiles("test")
#RunWith(SpringJUnit4ClassRunner.class)
#SpringApplicationConfiguration(classes = MyApplication.class)
#WebIntegrationTest
public class MessageControllerTest {
#Autowired
MessageRepository messageRepository;
...
...
...
#Test
public void testSomething(){
...
...
...
...
}
}

Related

ServletContext cannot open properties file when executing JUnit test

I'm testing a REST controller using JUnit 4 and MockMvc. When I've written the test a few weeks ago, everything worked as expected. I've done some modifications in my code but I didn't change the JUnit test. Now, when I'm trying to run my tests, I have the error:
Caused by: java.io.FileNotFoundException: Could not open ServletContext resource [/application.properties]
Here is my code:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(classes = MyServerApplication.class)
#SpringBootTest
#Transactional
public class MovieControllerTest {
private MockMvc mockMvc;
#Autowired
private MovieRepository movieRepository;
#Autowired
private WebApplicationContext wac;
#Before
public void setup() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
}
// Some tests
}
And my main class:
#SpringBootApplication
public class MyServerApplication{
public static void main(String[] args) {
SpringApplication.run(MyServerApplication.class, args);
}
}
My application.properties file is located in src/main/resources. I didn't move this file, I didn't do anything but add some code in my services and add some properties in my file.
I read SO questions & doc, and tried these solutions:
Check that src/main/resources is still in my test classpath
Add #PropertySource("classpath:application.properties") under the annotations in my test ; it didn't work so I tried to create a src/test/resources with a copy of application.properties inside, as suggested in one post
Add #PropertySource("classpath:application.properties") in the main class instead of the test class
Add #WebAppConfiguration annotation
Add #WebMvcTest annotation
I didn't try all of these solutions at the same time of course, I removed the added code after each failure.
I can still run my code without any issue though, only the test class results in FileNotFoundException.
How to solve this? And why do I have an issue with the test class but everything working fine when I run my server?
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(classes = MyServerApplication.class)
#SpringBootTest
#Transactional
public class MovieControllerTest { ... }
This is what you have on your test class. When using #SpringBootTest you shouldn't be using #ContextConfiguration (see testing chapter of the Spring Boot Reference Guide).
#RunWith(SpringJUnit4ClassRunner.class)
#SpringBootTest
#Transactional
public class MovieControllerTest { ... }
I would also suggest you use Spring Boot for testing instead of trying to do things manually. For mock mvc testing Spring Boot applications there are special slices and setup already done for you.
To enable this add #AutoConfigureMockMvc to your test and put #Autowired on the MockMvc field (and remove the setup in your #Before method).

What is the preferred way to configure a Spring Boot Test application?

I have configured my application using #Configuration annotated classes in the config package:
main
java
com.ourcompany
config
PersistenceConfig
JacksonConfig
persistence
...
Application
test
java
com.ourcompany
persistence
PersistenceTest
The configuration gets picked up by the Application class without a problem:
#SpringBootApplication
public class Application {
public static void main( String[] args ) {
SpringApplication.run( Application.class, args );
}
}
However, the Test class:
#RunWith(SpringRunner.class)
#DataMongoTest
public class PersistenceTest {
...
}
does not pick up the configuration, unless I specify the name of the configuration class in the annotation:
#SpringBootTest(classes = PersistenceConfig.class)
I find this quite unintuitive, since I have to explicitly list all my config classes.
What is the idiomatic way to share the configuration in Spring Boot?
EDIT
In the end the problem was with including both #DataMongoTest and #SpringBootTest at the same time. Removing the #DataMongoTest and annotating the test class as:
#RunWith(SpringRunner.class)
#SpringBootTest(classes = Application.class)
public class PersistenceTest {
...
}
Solved the problem. However, the question as to what is the best practice remains.

Disable Spring #EnableScheduling in Junit tests

I want to disable #Schedule in Spring tests but i can`t find a way to do it.
I have tried to make different config class for test environment but tasks are still fired.
This is config:
#Configuration
#EnableTransactionManagement
#EnableScheduling
#ComponentScan({"de.package"})
#PropertySource(name="application.properties", value="classpath:application.properties")
public class PersistenceJPAConfig {
...
}
This is test emvironment config.Just removed #EnableScheduling annotation
#Configuration
#EnableTransactionManagement
#ComponentScan({"de.package"})
#PropertySource(name="application.properties", value="classpath:application.properties")
public class PersistenceJPATestConfig {
...
}
In test i use:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(classes = { PersistenceJPATestConfig.class }, loader = AnnotationConfigContextLoader.class)
#FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class GetArticlesTest {
...
}
But tasks are still fired when i run the test..Is there any way to stop executing tasks while running tests ?
As you're using #ComponentScan on the same package both time, it seems spring is loading the other configuration too.
You could use some profile to filter that, like adding this on your PersistenceJPATestConfig
#Profile("test")
add this annotation on your JUnit class so it will be executed with the "test" profile
#ActiveProfiles("test")
Edit :
Your main config should also be profiled so it is ignored when its profile is not active, so you should add another #Profile on the main config class with a different profile than "test"
Quick solution based on other answers(for spring boot users), just add below code to main configuration so that it wont run on test profiles and test cases! No other changes!
#Profile(!test)
public class Config{
......
}

#Value "Could not resolve placeholder" in Spring Boot Test

I want to take a Junit test for Spring-boot as below:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(classes = {ApplicationTest.class})
public class TestOnSpring {
#Value("${app.name}")
private String appName;
#Test
public void testValue(){
System.out.println(appName);
}
}
and ApplicationTest.java like this
#ComponentScan("org.nerve.jiepu")
#EnableAutoConfiguration()
public class ApplicationTest {
public static void main(String[] args) {
SpringApplication.run(ApplicationTest.class, args);
}
}
and my POM like this:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.0.BUILD-SNAPSHOT</version>
</parent>
When I run the test, I got below error information
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'app.name' in string value "${app.name}"
at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:174)
at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126)
at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:204)
at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:178)
at org.springframework.context.support.PropertySourcesPlaceholderConfigurer$2.resolveStringValue(PropertySourcesPlaceholderConfigurer.java:172)
at org.springframework.beans.factory.support.AbstractBeanFactory.resolveEmbeddedValue(AbstractBeanFactory.java:807)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1027)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:543)
... 31 more
But When I run this application as normal Java Application
#SpringBootApplication
public class Application {
public static void main(String[] args){
SpringApplication.run(Application.class, args);
}
}
It work well!
What's wrong with it ? How should I take the junit test with Spring-boot?
Thanks a lot!
You need to add
#PropertySource("classpath:application.properties")
to your class, so it will pick your normal configurations.
If you need different configurations for test you can add
#TestPropertySource(locations="classpath:test.properties")
If not just copy paste your config file to test/resources folder, then boot will pick from there.
See this.
You can use the #SpringBootTest that will do create the PropertySourcesPlaceholderConfigurer automatically.
This is described in the Testing chapter of the Spring Boot documentation.
http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-testing.html#boot-features-configfileapplicationcontextinitializer-test-utility
You have annotated your test class with #ContextConfiguration(classes = {ApplicationTest.class}). Wherein ApplicationTest.class does the component scan on a mentioned package. When you run your test it tries to find the configuration from the resources folder in 'main' instead of 'test'. If you annotate your class with #SpringBootTest(classes = {ClassToBeTested.class}) or just #SpringBootTest in this particular case, I think (not 100% sure) it will create a limited context and pick up the properties from test/resources.
If your properties are test specific, you can name your properties/yml file as application-test.properties or application-test.yml. And use #ActiveProfiles("test") in your test class so that it will always read test specific properties file.
I usually use this solution which works for me.
For me the issue was related to the profile set for the application properties.
application.yml:
spring:
config:
activate:
on-profile: local
mongodb:
uri: mongodb+srv:/
Class with reference to value:
#Value("${mongodb.uri}")
private String uri;
Resolution in test class method:
#SpringBootTest
#ActiveProfiles("local")
class myTestClass {

How to run code before SpringJUnit4ClassRunner context initialization?

In my application I initialize a property before spring application startup as follows:
MapLookup.setMainArguments(new String[] {"logging.profile", profile}); //from args
SpringApplication.run(source, args);
(just for reference: it is used for log4j2 logging, which must be set before spring starts to initialize).
Now I want to run an #IntegrationTest, but use the same logging configuration. Obviously I cannot use the code above, as a JUnit test is not executed using SpringApplication.run.
So, how could I initialize code before a #RunWith(SpringJUnit4ClassRunner.class) starts?
Note: BeforeClass does not work as this is executed after spring context startup.
You can run the initialization in a static initializer. Static initializer will run after JUnit loads the test class and before JUnit reads any annotations on it.
Alternatively you can extend SpringJUnit4ClassRunner with your own Runner initialize in it first and then run SpringJUnit4ClassRunner
I had a slightly different problem. I need to deploy something to my service after the Spring context is loaded. Solution use a custom config class for the test and run the deployment within a #PostConstruct Method.
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(classes = TestConfig.class, loader = AnnotationConfigContextLoader.class)
public class JunitTest {
#Configuration
#ComponentScan(basePackages = { "de.foo })
public static class TestMConfig {
#Autowired
private DeploymentService service;
#PostConstruct
public void init() {
service.deploy(...);
}
}
#Test
public void test() {
...
}
}
Maybe this helps, someone, sometime, somewhere ;)

Categories