-
-
Save MaciejDobrowolski/1c3a80564158da54fb9346e1a80fbf7d to your computer and use it in GitHub Desktop.
Setting properties before application context starts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example; | |
import org.junit.runner.notification.RunNotifier; | |
import org.junit.runners.model.InitializationError; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | |
public class MyRunner extends SpringJUnit4ClassRunner { | |
public MyRunner(Class<?> clazz) throws InitializationError { | |
super(clazz); | |
System.setProperty("sample.property", "hello world!"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example; | |
import static org.assertj.core.api.Assertions.assertThat; | |
import org.assertj.core.api.Assertions; | |
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.boot.test.context.SpringBootTest; | |
import org.springframework.core.env.Environment; | |
import org.springframework.test.context.junit4.SpringRunner; | |
@RunWith(MyRunner.class) | |
@SpringBootTest | |
public class SpringBootStackApplicationTests { | |
@Autowired | |
Environment environment; | |
@Test | |
public void testIfPropertyPresent() { | |
assertThat(environment.getProperty("sample.property")).isEqualTo("hello world!"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment