Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法将spring测试用于异步+;春季安全_Java_Spring Mvc_Spring Boot_Spring Security_Spring Test - Fatal编程技术网

Java 无法将spring测试用于异步+;春季安全

Java 无法将spring测试用于异步+;春季安全,java,spring-mvc,spring-boot,spring-security,spring-test,Java,Spring Mvc,Spring Boot,Spring Security,Spring Test,我有一个@RestController类,该类从其端点返回DeferredResult对象。在到达这些端点之前,我使用@EnableWebSecurity类来设置基本身份验证。我能够使用正确的基本身份在本地卷曲这些端点并使其工作。但是,通过spring测试测试这些端点会导致以下异常: java.lang.ClassCastException:org.springframework.security.web.servletapi.HttpServlet3RequestFactory$Securit

我有一个
@RestController
类,该类从其端点返回
DeferredResult
对象。在到达这些端点之前,我使用
@EnableWebSecurity
类来设置基本身份验证。我能够使用正确的基本身份在本地卷曲这些端点并使其工作。但是,通过spring测试测试这些端点会导致以下异常:

java.lang.ClassCastException:org.springframework.security.web.servletapi.HttpServlet3RequestFactory$SecurityContextAsyncContext无法强制转换为org.springframework.mock.web.MockAsyncCont

我尝试在我的测试类上使用
@WebAppConfiguration
@ContextConfiguration(classes=SpringSecurityConfig.class)
注释,并自己设置
MockMvc
SpringSecurityConfig
只是我实现基本身份验证的类)。我也尝试过只使用
@springbootest
@AutoConfigureMockMvc

以下是我当前的测试课程:

@RunWith(SpringRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes = SpringSecurityConfig.class)
public class PushNotificationControllerSpringTest {

    @MockBean
    BucketDetailsService bucketDetailsService;

    @MockBean
    NotificationService notificationService;

    @Autowired
    protected WebApplicationContext wac;

    private MockMvc mockMvc;

    @Before
    public void prepare() {
        mockMvc = MockMvcBuilders.webAppContextSetup(wac).apply(springSecurity()).build();
    }

    @Test
    public void testListBulletins() throws Exception {
        mockMvc.perform(asyncDispatch(
                mockMvc.perform(get("/admin/bulletins").header("Authorization", "Basic YWRtaW46cGFzc3cwcmQ="))
                        .andExpect(status().isOk()).andReturn()));
    }
}
这是我的rest控制器:

@RestController
@RequestMapping("/admin")
public class PushNotificationController {

    protected static final Logger logger = LoggerFactory.getLogger(PushNotificationController.class);

    @Autowired
    BucketDetailsService bucketDetailsService;

    @GetMapping("/bulletins")
    public DeferredResult<List<BulletinDetails>> listBulletins() {
        DeferredResult<List<BulletinDetails>> deferredResult = new DeferredResult<>();
        logger.debug("Fetching bulletins...");
        bucketDetailsService.listBulletins()
                .whenComplete((res, ex) -> setResult(deferredResult, res, ex, "List bulletins"));
        return deferredResult;
    }

    private Throwable getRootCause(@NotNull Throwable throwable) {
        Throwable ex = ExceptionUtils.getRootCause(throwable);
        return ex == null ? throwable : ex;
    }

    protected <T> void setResult(DeferredResult<T> deferredResult, T res, Throwable ex, String prefix) {
        if (ex != null) {
            ex = getRootCause(ex);
            logger.debug("{} failure - exception: ", prefix, ex);
            deferredResult.setErrorResult(ex);
        } else {
            logger.debug("{} successfully completed - response: {}", prefix, res);
            deferredResult.setResult(res);
        }
    }

}
更新您的测试

 @Test
 public void PushNotificationControllerSpringTest() throws Exception {

 ....

 MvcResult mvcResult = this.mockMvc.perform(get("/admin/bulletins").header("Authorization", "Basic YWRtaW46cGFzc3cwcmQ="))
    .andExpect(request().asyncStarted()).andReturn();

     this.mockMvc.perform(asyncDispatch(mvcResult)).andExpect(status().isOk());
 }
}
更新您的测试

 @Test
 public void PushNotificationControllerSpringTest() throws Exception {

 ....

 MvcResult mvcResult = this.mockMvc.perform(get("/admin/bulletins").header("Authorization", "Basic YWRtaW46cGFzc3cwcmQ="))
    .andExpect(request().asyncStarted()).andReturn();

     this.mockMvc.perform(asyncDispatch(mvcResult)).andExpect(status().isOk());
 }
}

这是一个已在Core Spring中修复的错误

有关详细信息,请参阅

如果升级到SpringFramework4.3.16、5.0.6或更高版本,问题应该会消失


如果升级后仍无法解决问题,请创建一个新版本来描述问题。

这是一个已在Core Spring中修复的错误

有关详细信息,请参阅

如果升级到SpringFramework4.3.16、5.0.6或更高版本,问题应该会消失


如果升级后仍无法解决此问题,请创建一个新的来描述此问题。

您可以使用整个stacktrace更新您的帖子吗?现在添加,感谢回复您可以使用整个stacktrace更新您的帖子吗?现在添加,感谢您的回复,这并没有改变错误。我相信Sam是正确的。尝试了这个,但没有改变错误。我相信Sam是正确的。我还没有升级到尝试这个,但是我在你的链接中看到了问题和解决方案,所以我接受这个作为答案。我还没有升级到尝试这个,但是我在你的链接中看到了问题和解决方案,所以我接受这个作为答案。