简体   繁体   中英

How To Mock .properties file in Spring Boot project

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

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
 }
@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 
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM