在Spring Security的单元测试中,处理Authentication#getDetails的部分
中国的背景是丰富多彩的。
在Spring Security的单元测试中,控制器方法有一个地方以Authentication#getDetails的形式进行调用,并且这个地方出现了NullPointerException。
SampleDetails details = (SampleDetails)auth.getDetails();
details.getHoge();
在WithMockUser中找不到设置这个的方法,所以考虑了另一种方法。
应对
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
</parent>
<properties>
<java.version>10.0</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
以下是测试代码。
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import kagamihoge.springmvcsample.controller.SampleDetails;
@RunWith(SpringRunner.class)
@SpringBootTest(
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
classes = App.class)
@AutoConfigureMockMvc
public class SecurityTest {
@Autowired
private MockMvc mvc;
@Test
@WithMockUser
public void whenGetCustomers_thenStatus200() throws Exception {
UsernamePasswordAuthenticationToken auth = (UsernamePasswordAuthenticationToken)SecurityContextHolder.getContext().getAuthentication();
auth.setDetails(new SampleDetails());
mvc.perform(get("/hoge"))
.andExpect(status().isOk());
}
}
嗯,我相信很可能会使用UsernamePasswordAuthenticationToken,并进行了类型转换,然后使用setDetails方法将一个测试用的适当值放入。我用这种方法解决了这个问题。