Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mock an application.properties file?

I have a project with Spring mvc and Spring boot.

This project is deployed on a JBoss server and the application.properties file is on this server.

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.

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}"
like image 591
YLombardi Avatar asked Dec 19 '22 22:12

YLombardi


1 Answers

TestPropertySource sets a location to a property file. It is even possible to inline specific values:

@TestPropertySource(properties = { "task.enabled = false" })
like image 144
ksokol Avatar answered Dec 21 '22 12:12

ksokol