|
| 1 | +package com.baeldung.oauth2extractors; |
| 2 | + |
| 3 | +import com.baeldung.oauth2extractors.configuration.SecurityConfig; |
| 4 | +import org.junit.Before; |
| 5 | +import org.junit.Test; |
| 6 | +import org.junit.runner.RunWith; |
| 7 | +import org.springframework.beans.factory.annotation.Autowired; |
| 8 | +import org.springframework.boot.test.context.SpringBootTest; |
| 9 | +import org.springframework.test.context.ContextConfiguration; |
| 10 | +import org.springframework.test.context.junit4.SpringRunner; |
| 11 | +import org.springframework.test.web.servlet.MockMvc; |
| 12 | +import org.springframework.test.web.servlet.setup.MockMvcBuilders; |
| 13 | +import org.springframework.web.context.WebApplicationContext; |
| 14 | + |
| 15 | +import javax.servlet.Filter; |
| 16 | + |
| 17 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
| 18 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
| 19 | + |
| 20 | + |
| 21 | +@RunWith(SpringRunner.class) |
| 22 | +@SpringBootTest(classes = ExtractorsApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) |
| 23 | +@ContextConfiguration(classes = {SecurityConfig.class}) |
| 24 | +public class ExtractorsUnitTest { |
| 25 | + |
| 26 | + @Autowired |
| 27 | + private WebApplicationContext context; |
| 28 | + |
| 29 | + @Autowired |
| 30 | + private Filter springSecurityFilterChain; |
| 31 | + |
| 32 | + private MockMvc mvc; |
| 33 | + |
| 34 | + @Before |
| 35 | + public void setup() { |
| 36 | + mvc = MockMvcBuilders |
| 37 | + .webAppContextSetup(context) |
| 38 | + .addFilters(springSecurityFilterChain) |
| 39 | + .build(); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + public void contextLoads() throws Exception { |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + public void givenValidRequestWithoutAuthentication_shouldFailWith302() throws Exception { |
| 48 | + mvc |
| 49 | + .perform(get("/")) |
| 50 | + .andExpect(status().isFound()) |
| 51 | + .andReturn(); |
| 52 | + } |
| 53 | + |
| 54 | +} |
0 commit comments