gpt4 book ai didi

java - 如何模拟 application.properties 文件?

转载 作者:行者123 更新时间:2023-12-01 09:20:35 25 4
gpt4 key购买 nike

我有一个带有 Spring mvc 和 Spring boot 的项目。

该项目部署在 JBoss 服务器上,application.properties 文件位于该服务器上。

现在我想为一个 Spring Controller 编写一个测试。对于这个测试,我需要使用安全配置。在安全配置文件中,我有 @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}"

最佳答案

TestPropertySource 设置属性文件的位置。甚至可以内联特定值:

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

关于java - 如何模拟 application.properties 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33625828/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com