|
| 1 | +package com.baeldung.thymeleaf.controller; |
| 2 | + |
| 3 | +import com.baeldung.thymeleaf.config.InitSecurity; |
| 4 | +import com.baeldung.thymeleaf.config.WebApp; |
| 5 | +import com.baeldung.thymeleaf.config.WebMVCConfig; |
| 6 | +import com.baeldung.thymeleaf.config.WebMVCSecurity; |
| 7 | +import org.junit.Before; |
| 8 | +import org.junit.Test; |
| 9 | +import org.junit.runner.RunWith; |
| 10 | +import org.springframework.beans.factory.annotation.Autowired; |
| 11 | +import org.springframework.mock.web.MockHttpSession; |
| 12 | +import org.springframework.test.context.ContextConfiguration; |
| 13 | +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
| 14 | +import org.springframework.test.context.web.WebAppConfiguration; |
| 15 | +import org.springframework.test.web.servlet.MockMvc; |
| 16 | +import org.springframework.test.web.servlet.request.RequestPostProcessor; |
| 17 | +import org.springframework.test.web.servlet.setup.MockMvcBuilders; |
| 18 | +import org.springframework.web.context.WebApplicationContext; |
| 19 | + |
| 20 | +import javax.servlet.Filter; |
| 21 | + |
| 22 | +import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf; |
| 23 | +import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user; |
| 24 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
| 25 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
| 26 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view; |
| 27 | + |
| 28 | +@RunWith(SpringJUnit4ClassRunner.class) |
| 29 | +@WebAppConfiguration |
| 30 | +@ContextConfiguration(classes = { WebApp.class, WebMVCConfig.class, WebMVCSecurity.class, InitSecurity.class }) |
| 31 | +public class FunctionCallIntegrationTest { |
| 32 | + |
| 33 | + @Autowired |
| 34 | + WebApplicationContext wac; |
| 35 | + @Autowired |
| 36 | + MockHttpSession session; |
| 37 | + |
| 38 | + private MockMvc mockMvc; |
| 39 | + |
| 40 | + @Autowired |
| 41 | + private Filter springSecurityFilterChain; |
| 42 | + |
| 43 | + private RequestPostProcessor testUser() { |
| 44 | + return user("user1").password("user1Pass").roles("USER"); |
| 45 | + } |
| 46 | + |
| 47 | + @Before |
| 48 | + public void setup() { |
| 49 | + mockMvc = MockMvcBuilders.webAppContextSetup(wac).addFilters(springSecurityFilterChain).build(); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void testGetDates() throws Exception { |
| 54 | + mockMvc.perform(get("/function-call").with(testUser()).with(csrf())).andExpect(status().isOk()).andExpect(view().name("functionCall.html")); |
| 55 | + } |
| 56 | + |
| 57 | +} |
0 commit comments