ykt*_*too 5 json unit-testing spring-mvc mockito
我正在为 Spring MVC 4.2.x REST 控制器编写单元测试,并且对 Jackson (2.6.x) 序列化的日期有问题。运行应用程序时,日期 ( java.util.Date) 默认以默认格式序列化yyyy-MM-dd(无需额外配置),这就是我想要的。
然而,在测试期间,由于未知原因,日期被序列化为时间戳。
这是测试类的一些示例代码:
public class OrderControllerTest {
@Mock
private OrderService service;
@InjectMocks
private OrderController controller;
private MockMvc mockMvc;
private List<Order> orders = new ArrayList<>();
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
// Initialize dummy order list
orders.add(...);
orders.add(...);
orders.add(...);
}
@Test
public void list() throws Exception {
// Stub the service find method
when(service.find(asOfDate, null)).thenReturn(orders);
// Run the controller and check the result
MvcResult result =
mockMvc
.perform(get("/admin/order/"))
.andExpect(status().isOk())
.andExpect(content().contentType(TestUtil.APPLICATION_JSON_UTF8))
.andExpect(jsonPath("$[0].startDate", is(SOME_DATE)))
.andReturn();
System.out.println(result.getResponse().getContentAsString());
}
Run Code Online (Sandbox Code Playgroud)
所以在这里我看到返回的日期都是时间戳。
确定输出:
Expected: is <Thu Jan 01 00:00:00 CET 2015>
but: was <1420066800000L>
Run Code Online (Sandbox Code Playgroud)
我错过了什么吗?为什么运行应用程序和运行测试的默认序列化格式不同?
| 归档时间: |
|
| 查看次数: |
2266 次 |
| 最近记录: |