[英]@Caching method called from spring boot test [annotated with @Transactional] not working
[英]Spring Boot Controller differs in behaviour when called from @MvcTest annotated test
我在Spring(2.0.5)中具有以下控制器
@RestController
public class FaqController {
@GetMapping("/faqs")
public void get(@RequestParam("language") final Locale locale) {
System.out.println(locale);
}
}
當我使用URL http://localhost:8080/faqs?language
通過Postman / cURL執行GET請求時,將調用控制器方法,並且locale
為null
。
當我使用@MvcTest注釋的JUnit測試執行相同的URL時,如下所示:
@RunWith(SpringRunner.class)
@WebMvcTest
public class FaqControllerTest {
@Autowired
private MockMvc mockMvc;
@Test
public void test() throws Exception {
this.mockMvc.perform(get("/faqs?language"));
}
}
然后不調用該方法,而是拋出org.springframework.web.bind.MissingServletRequestParameterException
並顯示以下消息: 必需的語言環境參數'language'不存在 。
為什么有區別呢? 我想念什么嗎?
您是否希望它為空? 如果不需要,則將其刪除或將其設置為不需要。
@RestController
public class FaqController {
@GetMapping("/faqs")
public void get(@RequestParam( value = "language", required = false ) final Locale locale) {
System.out.println(locale);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.