当我测试一些琐碎的POST请求时,我可以获得MvcResult并从中获得一些东西: MvcResult result = mockMvc.perform(post("/api/register")
.contentType(MEDIA_TYPE_JSON_UTF8)
.content(new Gson().toJson(request)))
.andExpect(status().isCreated())
.andReturn();
Long tem
我试图使用com.jayway.jsonpath.JsonPath从JSON字符串读取键/值:
String contentAsString = mvcResult.getResponse().getContentAsString();
System.out.println(contentAsString);
Object value = JsonPath.read(contentAsString, "$.key");
但我知道错误是:
Expected to find an object with property ['key'] in path $ but
我曾经尝试过在objectMapper上使用@Autowired,也尝试过伪装它,但没有成功,我只是想使用writeValueAsStringMethod,这样我就不必向下面的content方法传递长的json字符串了。
如果我用@SpringBootTest和@AutoconfigureMockMvc标记类,那么它可以工作(对象映射器不是null),但是我认为必须有另一种方法,这样就不会强制使用这个注释。
TestClass:
@ExtendWith(MockitoExtension.class)
public class CarControllerTest {
private M
我正在学习如何使用JUnit和Mockito在Spring Boot JPA中创建单元测试
我做了一个测试类,
public void testInsertionMethod() throws Exception {
String URI = "/insertionURL";
ShoppingList list = new ShoppingList (3, new Fruit(1), new Vegetable(1));
String inputJson= this.jsonConversionMethod(list);
Assert.ass
我有一个带有服务的简单RestController,并且希望通过为服务提供一个模拟实现来测试控制器。但是,在运行测试时,我在resultActions = mockMvc.perform(get("/user"));对象中得到了一个空响应。下面是我的代码:Controller
@RestController
public class UserController {
@Autowired
UserService userService;
@GetMapping("/user")
public ResponseEntity<
我的SpringBoot应用程序使用身份验证和授权筛选器来接受对'/api/v1/login‘URL的请求,检查请求的凭据,并在成功的情况下将承载JWT令牌添加到响应头。
当我运行我的API和独立的CORS反应项目时,我能够以一个用户的身份注册、登录和访问站点功能,然后再按一下注册和再次登录(不需要注销),它就可以正常工作了。
然而,在我的集成测试中,我调用了一种方法来注册多个用户并作为成功的方法登录,但是当我试图在joinLeague()测试中发送另一个登录调用时,服务器会响应401错误:
错误信息:未经授权。
AuthorizationFilter:
public class Au
代码:
@RequiredArgsConstructor
@RestController
public class UserController {
private final UserService userService;
@PostMapping(value="/v1/user")
public ResponseEntity<ResponseDto> postUser(@RequestBody UserSaveRequestDto userSaveRequestDto) {
Long userId = userService.ins
我在我的新项目中测试新的servlet 3异步请求,但在测试控制器时卡住了。
我有一个这样的控制器方法:
@RequestMapping(value = "/thumbnails", method = RequestMethod.GET)
public Callable<ResponseEntity<List<Thumbnail>>> getAllThumbnails() {
//at this point I get results from the repository
return () -> {
LocalDateTime反序列化在Junit测试中存在问题。我有一个简单的REST API,它返回一些DTO对象。当我调用端点时,响应没有问题--它是正确的。然后,我尝试编写单元测试,获得MvcResult,并使用ObjectMapper将其转换为我的DTO对象。但我仍然收到:
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.time.LocalDateTime` out of START_ARRAY token
at [Source: (St