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

标签 java spring mocking mockito

我有一个带有 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/

相关文章:

php - 如何将 Guzzle Mock Handler 传递给 PHP 类以测试具有 json 响应的 API 调用

c# - 可以模拟自定义属性 c# 吗?

Java:内部类传值的线程安全

java - 将 Jtable 保存到我使用 boolean 列创建的文件中

java - 使用命令行参数将值注入(inject)spring

java - AnnotationConfigApplicationContext 尚未刷新 - 怎么了?

c# - 有没有人成功地模拟了 .NET 中的 Socket 类?

java - HttpURLConnection 类的 setDoOutput 和 setDoInput 方法有什么用

java - JAXB - 解码时遵循 keyref 的示例

java - 需要 Spring 事务的帮助来进行巨大的日期操作