【发布时间】:2018-12-24 01:10:00
【问题描述】:
我正在使用 Spring Boot Test 框架 + Mockito 进行控制器单元测试。我发现测试失败是因为一个奇怪的错误:当我告诉 Mockito 返回一个带有java.util.Date 字段的对象时,响应总是有一个比预期早一天的日期字符串。
我的测试是这样的:
package com.mycompany.myapp.controller;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.MockitoAnnotations;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import com.privalia.binlookup.model.BinInfo;
import com.privalia.binlookup.repo.BinInfoRepository;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment=WebEnvironment.MOCK, properties="classpath:test.properties")
@AutoConfigureMockMvc(secure=false)
public class BinInfoControllerUnitTests {
@InjectMocks
private BinInfoController controller;
private MockMvc mockMvc;
@MockBean
private BinInfoRepository repository;
private BinInfo mockBinInfo;
@Before
public void init() throws ParseException{
SimpleDateFormat fmt = new SimpleDateFormat("dd/MM/yyyy");
MockitoAnnotations.initMocks(this);
mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
this.mockBinInfo = new BinInfo();
this.mockBinInfo.setId(new Long(42));
this.mockBinInfo.setBin("touhou");
this.mockBinInfo.setJson_full("{is_json:true}");
this.mockBinInfo.setCreateAt(fmt.parse("18/08/2018"));
}
@Test
public void testBinInfoControllerSearchBIN() throws Exception {
when(this.repository.findByBin("touhou")).thenReturn(mockBinInfo);
this.mockMvc.perform(get("/search/touhou")
.accept(MediaType.APPLICATION_JSON_UTF8_VALUE)
.contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
.andDo(print())
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
.andExpect(content().string(not("")))
.andExpect(jsonPath("$.bin", is("touhou")))
.andExpect(jsonPath("$.json_full", is("{is_json:true}")))
.andExpect(jsonPath("$.createAt", is("18/08/2018")));
}
}
我的控制器是:
package com.mycompany.myapp.controller;
import java.awt.MediaTracker;
import java.util.List;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.ResponseEntity.BodyBuilder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.privalia.binlookup.model.BinInfo;
import com.privalia.binlookup.repo.BinInfoRepository;
@RestController
@RequestMapping(value="/")
public class BinInfoController {
@Autowired
private BinInfoRepository repository;
@RequestMapping(value="/search/{bin}", method=RequestMethod.GET)
public ResponseEntity<BinInfo> searchBIN(@Valid @PathVariable String bin) {
BinInfo found = repository.findByBin(bin);
BodyBuilder builder = ResponseEntity.status(HttpStatus.OK);
builder.contentType(MediaType.APPLICATION_JSON_UTF8);
return (found == null ? builder.body(null) : builder.body(found));
}
}
测试失败,因为返回的createAt字符串是17/08/2018,而不是18/08/2018。
我在时区“马德里/欧洲(格林威治标准时间 +0200,CEST)”,因为我们在夏季和 DST(夏令时),我怀疑某些时区的东西会干扰。
我的猜测:
模拟对象有一个没有时区信息的java.util.Date 对象,并且是8 月18 日00:00;当在perform() 中传递 Spring 上下文或 Jackson 或 Mockito 时,解析的 JSON 被视为 GMT,因此是负 2 小时,因此返回到 8 月 17 日。
但是我不知道在哪里可以找到相关的配置,是告诉Jackson,还是给Spring,或者Mockito不来做转换。
编辑:
原因确认:当我将日期字符串更改为18/08/2018 02:00:00 时,返回的日期为18/08/2018 00:00:00。
【问题讨论】:
标签: java unit-testing spring-boot jackson mockito