我有一个Spring Boot项目,我在其中使用Mockito进行测试,但是尝试获取我的属性文件
MyPersonalClass.Java(服务类)
@Service
public class MyPersonalClass {
public void getData() {
Properties runtimeprop = new Properties();
try {
runtimeprop = PropertyManager.getAllProperties("SimplePropertyFile"); // some other property other thatn simplePropertyFile 58 like sun and all
} catch (Exception e) {
log.logError(e);
}
String myProp = runtimeprop.getProperty("source.allow"); // Null
List<String> srclst = new ArrayList<>(Arrays.asList(allowedsrc.split(",")));// getting null Pointer Exception
}
SimplePropertyFile.properties(在运行测试类时,下面的属性应该出现,但是我无法获取
source.allow = PRIMER
source.value = TYPICAL
MypersonalClassTest.java(测试文件)
@RunWith(PowerMockRunner.class)
@PrepareForTest({ InquireOfferElementsImpl.class, PropertyManager.class })
//@TestPropertySource(locations = "classpath:SimplePropertyFile.properties")
//@PropertySource("classpath:SimplePropertyFile.properties")
//@TestPropertySource(properties = "Ssource.allow = PRIMER")
public class MypersonalClassTest {
// test code go here
// tested with given above Annotation but nothing is work for me
}
如果有人知道有关属性文件模拟的信息,请告诉我
答案 0 :(得分:1)
在application-test.properties
中创建一个名称为src/main/resources
的属性文件,然后使用@ActiveProfiles
@RunWith(PowerMockRunner.class)
@PrepareForTest({ InquireOfferElementsImpl.class, PropertyManager.class })
//@TestPropertySource(locations = "classpath:SimplePropertyFile.properties")
//@PropertySource("classpath:SimplePropertyFile.properties")
//@TestPropertySource(properties = "Ssource.allow = PRIMER")
@ActiveProfiles("test")
public class MypersonalClassTest {
// test code go here
// tested with given above Annotation but nothing is work for me
}
答案 1 :(得分:1)
@RunWith(PowerMockRunner.class)
@PrepareForTest({ InquireOfferElementsImpl.class, PropertyManager.class })
public class MypersonalClassTest {
// Code Down There
@Before
public static void setUpStatic() {
Properties props = System.getProperties();
props.setProperty("source.value", "TYPICAL");
props.setProperty("source.allow", "PRIMER");
} // everything will work file
}