我剛剛更新了我的應用程式以使用spring-test 5.3.12和Junit 5.7.2但現在我的測驗失敗了。
在以前的版本 (5.2.X) 中,回應型別始終為“application/json”,但現在出現此錯誤:
java.lang.AssertionError: Content type expected:<application/json> but
was:<application/json;charset=UTF-8>
Expected :application/json
Actual :application/json;charset=UTF-8
我已經檢查過回應中的內容型別實際上是帶有 Postman、Browser 和 Swagger 的application/json
失敗的測驗示例
@Test
void whenGetReleaseNotesAppVersionWithNoExistingCodeThenNotFoundInJsonFormat() throws Exception {
this.mvc.perform(get(
NEWER_RELEASES_PATH_UPDATE_CONFIG "/"
"6.0.0" "/notes")
.header(AUTH_HEADER, AUTH_TOKEN_SRVCGERETDESAX)
.locale(Locale.ENGLISH)
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isNotFound())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath(
"$.code", is(ApiErrorCode.RELEASE_NOTES_NOT_FOUND.getCode())));
}
無法將 MediaType 更改為APPLICATION_JSON_UTF8因為現在已棄用,所以我不知道如何解決這個問題。
MockMVC 只是自動裝配,無需進一步配置
@Autowired
private MockMvc mvc;
課堂批注
@ExtendWith(SpringExtension.class)
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = {
Application.class })
@ActiveProfiles({ "test" })
@EnableAutoConfiguration
@AutoConfigureMockMvc
class UpdateConfigControllerIT {...}
uj5u.com熱心網友回復:
通過添加字符集來宣告完全預期的內容型別:
.andExpect(content().contentType(new MediaType(MediaType.APPLICATION_JSON, StandardCharsets.UTF_8)))
或使用contentTypeCompatibleWith:
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
只是一個小筆記;您可以安全地洗掉@ExtendWith(SpringExtension.class)注釋,因為@SpringBootTest注釋已經使用給定的注釋進行了元注釋。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/369093.html
上一篇:Sprintboot運行時錯誤:無法為方法publicabstractcom.example.demo.entity.Department創建查詢
