0
我有一個Spring MVC和Spring啓動項目。如何模擬一個application.properties文件?
該項目部署在JBoss服務器上,並且application.properties文件位於此服務器上。
現在我想寫一個彈簧控制器的測試。對於這個測試,我需要使用安全配置。並且在安全性配置文件中,我有@Value批註以從application.properties文件中獲取值。
鑑於該文件不在項目中,我該如何模擬它來運行我的測試?
這裏是我的測試類:
@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");
}
}
我在日誌文件中有這樣的錯誤:
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}"
謝謝,它的工作原理。 – YLombardi