[英]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.