[JAVA] Correspondence of the part where Authentication # getDetails is done in the unit test of spring-security

background

In the spring-security unit test, there is a place where the controller method is Authentication # getDetails as shown below, and this is `` `NullPointerException```.

SampleDetails details = (SampleDetails)auth.getDetails();
details.getHoge();

There seemed to be no way to set this for WithMockUser, so I thought of another way.

Coping

pom.xml


	<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>

Below is the test code.

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());
    }
}

Well, I'm sure I'll cast it with the belief that UsernamePasswordAuthenticationToken will be used, and in `` `setDetails``` I'll put in the appropriate values for testing. I dealt with this method.

Recommended Posts

Correspondence of the part where Authentication # getDetails is done in the unit test of spring-security
Where is the home directory location of "Ubuntu"? (Solved in the comment section)
Login fails because the redirect URL of the self-login screen is incorrect in spring-security
[Java] Where is the implementation class of annotation that exists in Bean Validation?
In Time.strptime,% j (total date of the year) is
Get the name of the test case in the JUnit test class
Was done in the base year of the Java calendar week
Determine that the value is a multiple of 〇 in Ruby
What is the representation of domain knowledge in the [DDD] model?
[Rails] Where to be careful in the description of validation
802.1X authentication to the network of Bonding setting in CentOS7
[Android] Try to display the effect on the touched part of the screen, which is common in games.