8

I'm trying to use application.properties profiles for integration tests using JUnit in order to check two different platforms.

I tried doing so with basic configuration file application.properties which contains the common configurations for both platforms, and on top of that I've added properties files application-tensorflow.properties application-caffe.properties for each platform, which have specific platform configurations, but I found out that it works differently in JUnit than the approach I used to use in the main application.

my test configuration class looks like this:

@Configuration
@PropertySource("classpath:application.properties")
@CompileStatic
@EnableConfigurationProperties
class TestConfig {...}

I'm using @PropertySource("classpath:application.properties") so it will recognize my basic configurations, there I also write spring.profiles.active=tensorflow, in hope that it will recognize the tensorflow application profile however it doesn't read from the file: /src/test/resources/application-tensorflow.properties, nor from /src/main/resources/application-tensorflow.properties as it does in main app.

Is there special a way to specify a spring profile in JUnit test? What is the best practice to achieve what I'm trying to do?

2 Answers 2

15

First: Add @ActiveProfiles to your test class to define the active profiles.

Also, you need to configure that config files should be loaded. There are two options:

  • In a simple integration test with @ContextConfiguration(classes = TheConfiguration.class, initializers = ConfigFileApplicationContextInitializer.class)
  • In a full Spring Boot test with @SpringBootTest

Example test class:

@RunWith(SpringRunner.class)
@SpringBootTest
@ActiveProfiles({ "test" })
public class DummyTest {

    @Autowired
    private Environment env;

    @Test
    public void readProps() {
        String value = env.getProperty("prop1") + " " + env.getProperty("prop2");
        assertEquals("Hello World", value);
    }
}

Now the files src/test/resources/application.properties and src/test/resources/application-test.properties are evaluated.

Sign up to request clarification or add additional context in comments.

Comments

2

Have you tried annotating your test with

@RunWith(SpringRunner.class)
@SpringBootTest
@ActiveProfiles(profiles = {"tensorflow"})

Also make sure application-tensorflow.properties is under /src/test/resources

3 Comments

yes, and I don't understand why it doesn't access the tensorflow.application file after I specify the profile in many variations.
Have you annotated your test with @SpringBootTest and @RunWith(SpringRunner.class) ? Is application-tensorflow.properties under /src/test/resources?
Yes that's it. if you want, write this suggestion as an answer, and i'll mark it as a problem solver.

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.