Как макетировать файл .properties в проекте Spring Boot - PullRequest
0 голосов
/ 23 апреля 2019

У меня есть проект 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
}

, если какое-либо тело знает о файле свойствиздеваться, пожалуйста, дайте мне знать

Ответы [ 2 ]

1 голос
/ 23 апреля 2019
@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 
 }
1 голос
/ 23 апреля 2019

Создайте файл свойств с именем 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
 }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...