以下测试的目标是强制模拟对象使用特定的属性文件
@RunWith(MockitoJUnitRunner.class)
@TestPropertySource(locations = "classpath:application-test.properties")
public class CarTestClass {
@InjectMocks
private CarService myCarService;
@Test
public void ReadValueFromTestProperties() {
//when
String color = myCarService.getColor();
//then
assertEquals("red", color);
}
}
模拟对象使用@Value批注读取属性
@Service
public class myCarService {
@Value("${color}")
private String color;
public String getColor() {
return color;
}
}
运行测试时,car的值为空。我该如何解决?