I want to create JUnit 5 test for Rest API which uses JWT token validation: This jwt token is getting generated on The UI from different(Authentication server) and this token i am using in my API to protect how can i mock this in my Test classes. The RestController is annotated with @PreAuthorize("isAuthenticated()")
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc
@ActiveProfiles("local")
class ManagementSoftwareControllerTest {
@Autowired
private MockMvc mockMvc;
@Autowired
private WebApplicationContext context;
@BeforeEach
void init() {
mockMvc = MockMvcBuilders
.webAppContextSetup(context)
.apply(springSecurity())
.build();
}
@Test
public void testGetWidgetsSuccess() throws Exception {
// Execute the GET request
mockMvc.perform(MockMvcRequestBuilders.get("/vcf/v1/test")
.with(csrf())
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andReturn();
}
}