0

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.

3
  • Have you tried to use specific profiles ? Commented Apr 19, 2016 at 13:38
  • No, since I'm pretty new to this technology and I actually don't know what is the best way to achieve this.. Commented Apr 19, 2016 at 13:56
  • 1
    The above comment is the answer to your question, as per the documentation: docs.spring.io/spring-boot/docs/current/reference/html/… Commented Apr 19, 2016 at 14:03

2 Answers 2

2

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

  1. 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")
Sign up to request clarification or add additional context in comments.

1 Comment

Please validate the answer if it suits to you
1

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(){
        ...
        ...
        ...
        ...
        }
    }

1 Comment

A slightly better way would be to do something like this: @ActiveProfiles(AppProfile.TEST) .ie create an enum with all the possible states. It saves time on changes and make tracking profile types easier. Also create an abstract BaseTest class and put all these settings there. Now extend all your test cases from it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.