2015-11-10 11 views
0

У меня есть проект с весной mvc и весной.Как издеваться над файлом 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}" 

ответ

4

TestPropertySource задает местоположение в файле свойств. Можно даже ввести конкретные значения:

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

Спасибо, это работает. – YLombardi

Смежные вопросы