Skip to content
Permalink
Browse files

how to test with Spring Security enabled

  • Loading branch information...
slem1 committed Oct 27, 2017
1 parent 4fc82d4 commit e3d143c349062b32568d5ec47cb336aa25576a29
@@ -23,6 +23,13 @@
<artifactId>spring-boot-starter-security</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<version>4.2.3.RELEASE</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
@@ -26,8 +26,9 @@
private MockMvc mockMvc;

@Before
public void before(){
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
public void before() {
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext)
.build();
}

@Test
@@ -0,0 +1,41 @@
package fr.sle.testhowto.test;

import fr.sle.testhowto.rest.facade.MyRestFacade;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

/**
* @author slemoine
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class MyRestFacadeWithSpringSecurityTest {

@Autowired
private WebApplicationContext webApplicationContext;

private MockMvc mockMvc;

@Before
public void before() {
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext)
.apply(SecurityMockMvcConfigurers.springSecurity())
.build();
}

@Test
public void getAStringTest() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get(MyRestFacade.ROOT_RESOURCE))
.andExpect(MockMvcResultMatchers.status().isOk());
}
}

0 comments on commit e3d143c

Please sign in to comment.
You can’t perform that action at this time.