Reputation: 1166
I have Spring Boot project where i am using Mockito for Testing but while Trying to Fetch my Properties File
MyPersonalClass.Java (Service Class)
@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 ( given below property should be come while we run test class but i am not able to get
source.allow = PRIMER
source.value = TYPICAL
MypersonalClassTest.java(Test File)
@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
}
if any body knows about the properties file mocking please let me know
Upvotes: 1
Views: 3452
Reputation: 478
@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
}
Upvotes: 3
Reputation: 39978
Create a properties file with name application-test.properties
in src/main/resources
and then use @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
}
Upvotes: 2