Spring boot 如何使用SpringBoot在Junit测试类中禁用SpringSecurity
我已经使用spring boot 2.2.5.RELEASE创建了简单的Rest Api服务,我刚刚在应用程序中启用了spring安全性。少年们没有工作。我尝试了一些方法来解决这个问题,但没有成功。 通过阅读书籍和在线参考资料(包括堆栈溢出中回答的问题),我了解了两种在测试中禁用安全性的方法:Spring boot 如何使用SpringBoot在Junit测试类中禁用SpringSecurity,spring-boot,spring-security,Spring Boot,Spring Security,我已经使用spring boot 2.2.5.RELEASE创建了简单的Rest Api服务,我刚刚在应用程序中启用了spring安全性。少年们没有工作。我尝试了一些方法来解决这个问题,但没有成功。 通过阅读书籍和在线参考资料(包括堆栈溢出中回答的问题),我了解了两种在测试中禁用安全性的方法: @WebMvcTest(值=MyController.class,安全=false) @AutoConfigureMockMvc(安全=false) @EnableAutoConfiguration(ex
Code Snippet
package com.akarsh.controller;
import static org.junit.Assert.*;
@RunWith(SpringRunner.class)
@AutoConfigureMockMvc
@EnableAutoConfiguration(exclude = {SecurityAutoConfiguration.class})
@SpringBootTest(classes = SpringBootProj2Application.class,webEnvironment =SpringBootTest.WebEnvironment.RANDOM_PORT)
public class SurveyControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private SurveyService surveyService;
@Test
public void retrieveDetailsForQuestion_Test() throws Exception {
Question mockQuestion = new Question("Question1",
"Largest Country in the World", "Russia", Arrays.asList(
"India", "Russia", "United States", "China"));
Mockito.when(
surveyService.retrieveQuestion(Mockito.anyString(), Mockito
.anyString())).thenReturn(mockQuestion);
RequestBuilder requestBuilder = MockMvcRequestBuilders.get(
"/surveys/Survey1/questions/Question1").accept(
MediaType.APPLICATION_JSON);
MvcResult result = mockMvc.perform(requestBuilder).andReturn();
String expected = "{\"id\":\"Question1\",\"description\":\"Largest Country in the World\",\"correctAnswer\":\"Russia\",\"options\":[\"India\",\"Russia\",\"United States\",\"China\"]}";
String actual=result.getResponse().getContentAsString();
JSONAssert.assertEquals(expected,actual , false);
}
\\
\
获取以下异常
Description:
A component required a bean of type 'org.springframework.security.config.annotation.ObjectPostProcessor' that could not be found.
Action:
Consider defining a bean of type 'org.springframework.security.config.annotation.ObjectPostProcessor' in your configuration.
2020-04-13 14:51:15.659 ERROR 5128 --- [ main] o.s.test.context.TestContextManager : Caught exception while allowing TestExecutionListener [org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener@36902638] to prepare test instance [com.akarsh.controller.SurveyControllerTest@3eb8057c]
\\
@RestController
公共类测量控制器{
@自动连线
调查服务调查服务;
@GetMapping(“/surveys/{surveyId}/questions”)
公共列表检索QuestionForServey(@PathVariable String surveyId)
{
if(surveyId!=null)
{
返回调查服务检索问题(surveyId);
}
返回null;
}
@GetMapping(“/surveys/{surveyId}/questions/{questionId}”)
公共问题检索问题(@PathVariable String surveyId,@PathVariable String questionId)
{
if(surveyId!=null)
{
returnsurveyservice.retrieveQuestion(surveyId,questionId);
}
返回null;
}
@邮戳(“/surveys/{surveyId}/questions”)
public ResponseEntity AddQuestionForServey(@PathVariable String surveyId,@RequestBody Question-Question){
Question createdTodo=surveyService.addQuestion(surveyId,问题);
if(createdTodo==null){
返回ResponseEntity.noContent().build();
}
URI位置=ServletUriComponentsBuilder.fromCurrentRequest().path(“/{id}”)
.buildAndExpand(createdTodo.getId()).toUri();
返回ResponseEntity.created(location.build();
}
这是否回答了您的问题?@AutoConfigureMockMvc(secure=false)未在Spring Boot 2.2.5.RELEASE中编译。您为什么说“未编译”,更准确地说,确切的错误是什么?然后用此信息更新您的问题。获取编译时错误。。。使用@AutoConfigureMockMvc(secure=false)时,“注释类型AutoConfigureMockMvc的属性secure未定义”这是否回答了您的问题?@AutoConfigureMockMvc(secure=false)未在Spring Boot 2.2.5.RELEASE中编译。您为什么说“未编译”“,更准确地说,确切的错误是什么?然后使用此信息更新您的问题。获取编译时错误…”使用@AutoConfigureMockMvc(secure=false)时,“注释类型AutoConfigureMockMvc的属性secure未定义”
Description:
A component required a bean of type 'org.springframework.security.config.annotation.ObjectPostProcessor' that could not be found.
Action:
Consider defining a bean of type 'org.springframework.security.config.annotation.ObjectPostProcessor' in your configuration.
2020-04-13 14:51:15.659 ERROR 5128 --- [ main] o.s.test.context.TestContextManager : Caught exception while allowing TestExecutionListener [org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener@36902638] to prepare test instance [com.akarsh.controller.SurveyControllerTest@3eb8057c]
@RestController
public class SurveyController {
@Autowired
SurveyService surveyService;
@GetMapping("/surveys/{surveyId}/questions")
public List<Question> retrieveQuestionForSrvey(@PathVariable String surveyId)
{
if(surveyId!=null)
{
return surveyService.retrieveQuestions(surveyId);
}
return null;
}
@GetMapping("/surveys/{surveyId}/questions/{questionId}")
public Question retrieveQuestion(@PathVariable String surveyId,@PathVariable String questionId)
{
if(surveyId!=null)
{
return surveyService.retrieveQuestion(surveyId, questionId);
}
return null;
}
@PostMapping("/surveys/{surveyId}/questions")
public ResponseEntity<?> addQuestionForSrvey(@PathVariable String surveyId, @RequestBody Question question) {
Question createdTodo = surveyService.addQuestion(surveyId, question);
if (createdTodo == null) {
return ResponseEntity.noContent().build();
}
URI location = ServletUriComponentsBuilder.fromCurrentRequest().path("/{id}")
.buildAndExpand(createdTodo.getId()).toUri();
return ResponseEntity.created(location).build();
}