Skip to content

Instantly share code, notes, and snippets.

@MaciejDobrowolski
Created December 6, 2016 08:44
Show Gist options
  • Save MaciejDobrowolski/1c3a80564158da54fb9346e1a80fbf7d to your computer and use it in GitHub Desktop.
Save MaciejDobrowolski/1c3a80564158da54fb9346e1a80fbf7d to your computer and use it in GitHub Desktop.
Setting properties before application context starts
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!");
}
}
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