Java 使用MockitoJUnitRunner时加载属性
我试图从我的服务单元测试中的application-test.properties加载属性值(使用MockitoJUnitRunner.class),但它总是使用@value注释获取空值Java 使用MockitoJUnitRunner时加载属性,java,spring,spring-boot,mockito,Java,Spring,Spring Boot,Mockito,我试图从我的服务单元测试中的application-test.properties加载属性值(使用MockitoJUnitRunner.class),但它总是使用@value注释获取空值 @RunWith(MockitoJUnitRunner.class) public class AlertQueryServiceImplTest { @Value("${raw.path}") private static String rawJson; @InjectMocks private Aler
@RunWith(MockitoJUnitRunner.class)
public class AlertQueryServiceImplTest {
@Value("${raw.path}")
private static String rawJson;
@InjectMocks
private AlertQueryServiceImpl alertQueryServiceImpl;
@Mock
private AlertCBDAOImpl alertCBDAOImpl;
private List<Alert> alertListExpected;
@Before
public void setUp() {
ReflectionTestUtils.setField(alertQueryServiceImpl, "url", "http://someurl");
alertListExpected = JsonUtils.
loadObjectList(Alert.class, JsonUtils.ALERTS_FILE);
assertFalse(alertListExpected.isEmpty());
}
@Test
public void shouldReturnAlerts() {
User userTest = new User("108998", "3000747091");
JsonDocument jsonDocExpected = JsonUtils.stringToJsonDocument(rawJson, userTest.getAssociatedBE());
when(alertCBDAOImpl.getDoc(userTest.getAssociatedBE())).thenReturn(jsonDocExpected);
when(alertCBDAOImpl.getAlerts(jsonDocExpected)).thenReturn(alertListExpected);
}
}
@RunWith(MockitoJUnitRunner.class)
公共类AlertQueryServiceImpletTest{
@值(“${raw.path}”)
私有静态字符串;
@注射模拟
私有AlertQueryServiceImpl AlertQueryServiceImpl;
@嘲弄
私有警报CBDAIMPL警报CBDAIMPL;
私有列表alertListExpected;
@以前
公共作废设置(){
ReflectionTestUtils.setField(alertQueryServiceImpl,“url”http://someurl");
alertListExpected=JsonUtils。
loadObjectList(Alert.class、JsonUtils.ALERTS\u文件);
assertFalse(alertListExpected.isEmpty());
}
@试验
public void shouldReturnAlerts(){
用户测试=新用户(“108998”、“3000747091”);
JsonDocument jsonDocExpected=JsonUtils.stringToJsonDocument(rawJson,userTest.getAssociatedBE());
当(alertCBDAOImpl.getDoc(userTest.getAssociatedBE())。然后返回(jsonDocExpected);
当(alertCBDAOImpl.getAlerts(jsonDocExpected))。然后返回(alertlisexpected);
}
}
如何将
raw.path
属性加载到rawJson
字段中?首先,您需要设置活动配置文件以访问application-test.properties文件,您可以在测试类上使用@ActiveProfiles(“test”)注释来完成此操作。
我认为@Value annotation不能与static关键字一起使用。您找到解决方案了吗?我也有同样的问题。