Cha*_*ire 1 rest spring mockito mongodb modelmapper
我是 MongoDB 新手,正在为 Mongo 支持的 REST Web 服务编写一系列单元测试。这是 /clients/{id} enpoint 的简单测试:
@RunWith(MockitoJUnitRunner.class)
public class ClientsControllerMockMvcStandaloneTest {
private MockMvc mvc;
@Mock
private ClientsRepository clientsRepository;
@Mock
private ModelMapper modelMapper;
@InjectMocks
private ClientsController clientsController;
private ExceptionHandlerExceptionResolver createExceptionResolver() {
ExceptionHandlerExceptionResolver exceptionResolver = new ExceptionHandlerExceptionResolver() {
@SuppressWarnings("ConstantConditions")
@Override
protected ServletInvocableHandlerMethod getExceptionHandlerMethod(final HandlerMethod handlerMethod,
final Exception exception) {
final Method method = new ExceptionHandlerMethodResolver(RestResponseEntityExceptionHandler.class)
.resolveMethod(exception);
final RestResponseEntityExceptionHandler handler = new RestResponseEntityExceptionHandler();
return new ServletInvocableHandlerMethod(handler, method);
}
};
exceptionResolver.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
exceptionResolver.afterPropertiesSet();
return exceptionResolver;
}
@Before
public void setup() {
JacksonTester.initFields(this, new ObjectMapper());
mvc = MockMvcBuilders.standaloneSetup(clientsController)
.setHandlerExceptionResolvers(createExceptionResolver())
.build();
}
// GET /api/clients/{id} 200
@Test
public void findById_ClientEntryFound_ShouldReturnFoundClientEntry() throws Exception {
final ObjectId id = new ObjectId();
final Client client = Client.builder()
.id(id)
.name("Microsoft")
.build();
final ClientDTO clientDTO = ClientDTO.builder()
.id(id)
.name("Microsoft")
.build();
when(clientsRepository.findById(id))
.thenReturn(Optional.of(client));
when(modelMapper.map(client, ClientDTO.class))
.thenReturn(clientDTO);
mvc.perform(get("/clients/" + id.toString())
.accept(TestUtils.APPLICATION_JSON_UTF8))
.andExpect(content().contentType(TestUtils.APPLICATION_JSON_UTF8))
.andExpect(status().isOk())
.andExpect(jsonPath("$.id", is(id)))
.andExpect(jsonPath("$.name", is("Microsoft")))
.andDo(MockMvcResultHandlers.print());
verify(modelMapper, times(1)).map(client, ClientDTO.class);
verify(clientsRepository, times(1)).findById(id);
verifyNoMoreInteractions(clientsRepository);
}
}
Run Code Online (Sandbox Code Playgroud)
我希望这能起作用,但我得到以下结果:
java.lang.AssertionError: JSON path "$.id"
Expected: is <5c9b9a0289d2b311b150b92c>
but: was <{timestamp=1553701378, machineIdentifier=9032371, processIdentifier=4529, counter=5290284, timeSecond=1553701378, time=1553701378000, date=1553701378000}>
Expected :is <5c9b9a0289d2b311b150b92c>
Actual :<{timestamp=1553701378, machineIdentifier=9032371, processIdentifier=4529, counter=5290284, timeSecond=1553701378, time=1553701378000, date=1553701378000}>
<Click to see difference>
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激(包括任何指示,如果您认为我的一般方法可以改进!)。
干杯!
杰克逊不知道您的ObjectId实例应该序列化为5c9b9a0289d2b311b150b92c而不是:
{
"timestamp": 1553701378,
"machineIdentifier": 9032371,
"processIdentifier": 4529,
"counter": 5290284,
"time": 1553701378000,
"date": 1553701378000,
"timeSecond": 1553701378
}
Run Code Online (Sandbox Code Playgroud)
幸运的是,它很容易修复。该ObjectId#toString()方法(将在内部调用ObjectId#toHexString())允许您将实例转换ObjectId为 24 字节的十六进制字符串表示形式。
因此,您可以使用@JsonSerializewithToStringSerializer将ObjectId实例表示为字符串:
{
"timestamp": 1553701378,
"machineIdentifier": 9032371,
"processIdentifier": 4529,
"counter": 5290284,
"time": 1553701378000,
"date": 1553701378000,
"timeSecond": 1553701378
}
Run Code Online (Sandbox Code Playgroud)
然后,在您的测试中,使用ObjectId#toString()方法(或ObjectId#toHexString())进行断言:
@JsonSerialize(using = ToStringSerializer.class)
private ObjectId id;
Run Code Online (Sandbox Code Playgroud)
或者,假设您使用的是 Spring Data for MongoDB,而不是ObjectId,您可以使用:
.andExpect(jsonPath("$.id", is(id.toString())))
Run Code Online (Sandbox Code Playgroud)
您还可以在映射器层中处理ObjectId到的转换。String
| 归档时间: |
|
| 查看次数: |
884 次 |
| 最近记录: |