我想能夠使用測試屬性文件並僅覆蓋一些屬性。不得不重寫每一個屬性會變得很難。在春季環境中僅模擬選定的屬性
這是我使用測試我的嘲笑性質和測試用例使用現有的屬性能力
@RunWith(SpringRunner.class)
@SpringBootTest(classes = MyApp.class)
@TestPropertySource(
locations = { "classpath:myapp-test.properties" },
properties = { "test.key = testValue" })
public class EnvironmentMockedPropertiesTest {
@Autowired private Environment env;
// @MockBean private Environment env;
@Test public void testExistingProperty() {
// some.property=someValue
final String keyActual = "some.property";
final String expected = "someValue";
final String actual = env.getProperty(keyActual);
assertEquals(expected, actual);
}
@Test public void testMockedProperty() {
final String keyMocked = "mocked.test.key";
final String expected = "mockedTestValue";
when(env.getProperty(keyMocked)).thenReturn(expected);
final String actual = env.getProperty(keyMocked);
assertEquals(expected, actual);
}
@Test public void testOverriddenProperty() {
final String expected = "testValue";
final String actual = env.getProperty("test.key");
assertEquals(expected, actual);
}
}
我覺得是代碼:
@Autowired private Environment env;
testExistingProperty()
和testOverriddenProperty()
通testMockedProperty()
失敗
@MockBean private Environment env;
testMockedProperty()
通過testExistingProperty()
和testOverriddenProperty()
失敗
有沒有辦法實現什麼,我去向何方?
依賴關係:
<spring.boot.version>1.4.3.RELEASE</spring.boot.version>
...
<!-- Spring -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
<version>${spring.boot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>${spring.boot.version}</version>
</dependency>
<!-- Starter for testing Spring Boot applications with libraries including JUnit,
Hamcrest and Mockito -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>${spring.boot.version}</version>
</dependency>
我假設你希望只使用其中既有能力上嘲笑和實際數據右一個工作環境環境變量來實現這一目標? –