简体   繁体   English

如何模拟一个application.properties文件?

[英]How to mock an application.properties file?

I have a project with Spring mvc and Spring boot. 我有一个使用Spring MVC和Spring Boot的项目。

This project is deployed on a JBoss server and the application.properties file is on this server. 该项目部署在JBoss服务器上,而application.properties文件位于该服务器上。

Now I want to write a test for a spring controller. 现在,我想为弹簧控制器编写一个测试。 For this test, I need to use the security configuration. 对于此测试,我需要使用安全性配置。 And in the security configuration file, I have @Value annotation to get values from the application.properties file. 在安全配置文件中,我具有@Value批注以从application.properties文件获取值。

Given that this file is not in the project, how can I mock it to run my test ? 鉴于此文件不在项目中,我如何模拟它以运行测试?

Here is my test class : 这是我的测试课:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {PortalWebSecurityConfig.class})
@WebAppConfiguration
public class DashboardControllerTests {

    @Mock
    private IssueNotificationManager issueNotificationManager;

    @InjectMocks
    private DashboardController dashboardController;

    private MockMvc mockMvc;

    @Before
    public void setup() {
        MockitoAnnotations.initMocks(this);
        mockMvc = MockMvcBuilders.standaloneSetup(dashboardController).build();
    }

    @Test
    @WithMockCustomZenithUser(customUser = "LOGIN")
    public void dashboardControllerTestOk() throws Exception {

        when(issueNotificationManager.findIssueNotifications("User"))
                .thenReturn(new BusinessObjectCollection<>());

        mockMvc.perform(get("/").with(testSecurityContext()))
                .andDo(print())
                .andExpect(status().isOk())
                .andExpect(view().name("index"))
                .andExpect(model().size(4))
                .andExpect(model().attributeExists("issueNotifications"));
            verify(issueNotificationManager).findIssueNotifications("User");
    }
}

I have this error in my log file : 我的日志文件中有此错误:

09:16:19.899 [main] DEBUG o.s.c.e.PropertySourcesPropertyResolver - Searching for key 'ad.domain' in [environmentProperties]
09:16:19.899 [main] DEBUG o.s.c.e.PropertySourcesPropertyResolver - Searching for key 'ad.domain' in [servletConfigInitParams]
09:16:19.899 [main] DEBUG o.s.c.e.PropertySourcesPropertyResolver - Searching for key 'ad.domain' in [servletContextInitParams]
09:16:19.900 [main] DEBUG o.s.c.e.PropertySourcesPropertyResolver - Searching for key 'ad.domain' in [systemProperties]
09:16:19.900 [main] DEBUG o.s.c.e.PropertySourcesPropertyResolver - Searching for key 'ad.domain' in [systemEnvironment]
09:16:19.900 [main] DEBUG o.s.c.e.PropertySourcesPropertyResolver - Could not find key 'ad.domain' in any property source. Returning [null]
09:16:19.900 [main] DEBUG o.s.c.e.PropertySourcesPropertyResolver - Searching for key 'ad.domain' in [localProperties]
09:16:19.900 [main] DEBUG o.s.c.e.PropertySourcesPropertyResolver - Could not find key 'ad.domain' in any property source. Returning [null]
09:16:19.903 [main] WARN  o.s.w.c.s.GenericWebApplicationContext - Exception encountered during context initialization - cancelling refresh attempt
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'portalWebSecurityConfig': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: protected java.lang.String com.geodis.rt.zenith.framework.webui.authentification.WebSecurityConfig.domain; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'ad.domain' in string value "${ad.domain}"

TestPropertySource sets a location to a property file. TestPropertySource将位置设置为属性文件。 It is even possible to inline specific values: 甚至可以内联特定值:

@TestPropertySource(properties = { "task.enabled = false" })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何更改application.properties文件中的配置文件 - How to change profile in application.properties file 如何在 spring 应用程序中动态修改 application.properties 文件? - How to Modify application.properties file dynamically in spring application? Spring Boot 2:如何使用application.properties文件配置HikariCP - Spring Boot 2: How to configure HikariCP using application.properties file java中如何将数据写入application.properties文件 - How to write a data into the application.properties file in java java 如何从我的战争文件中外部化 application.properties - java how externalise application.properties from my war file 如何在 JAVA 的 application.properties 文件中动态更改值 - How to change values dynamically in application.properties file in JAVA 如何在 Spring Boot 中访问 application.properties 文件中定义的值 - How to access a value defined in the application.properties file in Spring Boot 我应该如何为Spring创建一个Java application.properties文件? - How should I create a Java application.properties file for Spring? 如何在SpringBoot application.properties文件中对映射进行软编码,而不是对硬编码进行映射? - How to softcode map in SpringBoot application.properties file instead of hardcoding? 如何调用 logback.xml 文件中的 application.properties 值? - How to call application.properties value in logback.xml file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM