I'm trying to create this JUnit test for Spring Authorization Server:
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import java.util.Map;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
@SpringBootTest
class AuthorizationServerTests {
@Autowired
private WebApplicationContext context;
private MockMvc mockMvc;
@Test
void testTokenEndpoint() throws Exception {
mockMvc = MockMvcBuilders.webAppContextSetup(context).build();
// Test the token endpoint
mockMvc.perform(post("/oauth2/token")
.header("Authorization", "test")
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.param("grant_type", "client_credentials")
.param("scope", "read")
.param("client_id", "test")
.param("client_secret", "test"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.access_token").exists());
}
}
When I run it I get error:
2024-12-30T00:11:41.836+02:00 INFO 8196 --- [test] [ Test worker] o.s.t.web.servlet.TestDispatcherServlet : Completed initialization in 4 ms
2024-12-30T00:11:41.933+02:00 DEBUG 8196 --- [test] [ Test worker] o.s.t.web.servlet.TestDispatcherServlet : POST "/oauth2/token", parameters={masked}
2024-12-30T00:11:41.956+02:00 DEBUG 8196 --- [test] [ Test worker] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped to ResourceHttpRequestHandler [classpath [META-INF/resources/], classpath [resources/], classpath [static/], classpath [public/], ServletContext [/]]
2024-12-30T00:11:41.978+02:00 DEBUG 8196 --- [test] [ Test worker] o.s.w.s.r.ResourceHttpRequestHandler : Resource not found
2024-12-30T00:11:41.987+02:00 DEBUG 8196 --- [test] [ Test worker] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.servlet.resource.NoResourceFoundException: No static resource oauth2/token.]
2024-12-30T00:11:41.988+02:00 DEBUG 8196 --- [test] [ Test worker] o.s.t.web.servlet.TestDispatcherServlet : Completed 404 NOT_FOUND
With Postman with the same params I get successful authorization. Why is the endpoint not listening for requests and how I can fix this issue?
I'm posting here the working solution for Spring boot 3.4.0:
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
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.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import java.util.Map;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
@SpringBootTest
@AutoConfigureMockMvc
class AuthorizationServerTests {
@Autowired
private MockMvc mockMvc;
@Test
void testTokenEndpoint() throws Exception {
// Test the token endpoint
mockMvc.perform(post("/oauth2/token")
.header("Authorization", "test")
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.queryParam("grant_type", "client_credentials")
.queryParam("scope", "read")
.queryParam("client_id", "test")
.queryParam("client_secret", "test"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.access_token").exists());
}
}