亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

無法使用具有異步 + 彈簧安全性的彈簧測試

無法使用具有異步 + 彈簧安全性的彈簧測試

元芳怎么了 2021-08-19 13:34:22
我有一個從其端點@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只是我實現基本身份驗證的類)。我也試過只使用@SpringBootTestand @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()));    }}這是我的休息控制器:@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;    }
查看完整描述

2 回答

?
米脂

TA貢獻1836條經驗 獲得超3個贊

這是 Core Spring 中已修復的錯誤。

有關詳細信息,請參閱SPR-16695。

如果升級到 Spring Framework 4.3.16、5.0.6 或更高版本,問題應該會消失。

如果升級后它沒有消失,請創建一個新的JIRA 問題來描述問題。


查看完整回答
反對 回復 2021-08-19
?
胡子哥哥

TA貢獻1825條經驗 獲得超6個贊

更新你的測試


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

 }

}


查看完整回答
反對 回復 2021-08-19
  • 2 回答
  • 0 關注
  • 241 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號