java8 elasticsearch_java – Elasticsearch – Junit测试(模拟或填...

我使用Elastic 6.2,SpringBoot,Java 8.

@RestController

@Log4j2

@AllArgsConstructor

@RequestMapping("/api/logs")

public class ElasticRestController {

@PostMapping("/search")

public GenericResponse> findLogs(@RequestBody ESLogRequestDTO esLogRequest,

Pageable pageable) throws NoConnectionException {

SearchResponse searchResponse = elasticUIService.

findLogsByParameters(esLogRequest, pageable);

return GenericResponse.

success(convertToStandardResponse(searchResponse.getHits(), pageable));

}

}

这里是JUnit控制器测试,在json(searchRequest)中有一些填充请求:

@WebMvcTest(

value = ElasticRestController.class,

secure = false

)

public class ElasticRestControllerTest extends AbstractControllerTest {

private static final String CONTENT_TYPE = "application/json;charset=UTF-8";

@MockBean

private ElasticUIService elasticUIService;

@MockBean

private ElasticsearchService elasticsearchService;

@Autowired

private ElasticRestController elasticRestController;

@Autowired

private MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter;

@Autowired

private MockMvc mockMvc;

@Rule

public JUnitRestDocumentation restDocumentation = new JUnitRestDocumentation();

@Before

public void before() {

mockMvc = MockMvcBuilders.standaloneSetup(elasticRestController)

.setCustomArgumentResolvers(new PageableHandlerMethodArgumentResolver())

.setMessageConverters(mappingJackson2HttpMessageConverter)

.apply(MockMvcRestDocumentation.documentationConfiguration(this.restDocumentation))

.build();

}

@Test

public void findLogsByParametersTest() throws Exception {

String searchRequest = "{\n" +

"\t \"levels\": [\"INFO\"],\n" +

" \"module\": \"test module\",\n" +

" \"version\": \"version 1\",\n" +

" \"thread\": \"test thread\",\n" +

" \"requestId\": \"1\",\n" +

" \"message\": \"test message 3\",\n" +

" \"rangeFrom\": \"2018-02-26T07:02:50.000Z\",\n" +

" \"rangeTo\": \"2018-03-05T07:02:50.000Z\",\n" +

" \"node\": \"first node\",\n" +

" \"system\": \"super system 1\",\n" +

" \"header\": \"test\",\n" +

" \"submodule\": \"test submodule\",\n" +

" \"operation\": \"some operation\",\n" +

" \"service\": \"some service\",\n" +

" \"type\": \"some type\",\n" +

" \"metricType\": \"duration\",\n" +

" \"valueFrom\":400,\n" +

" \"valueTo\":600\n" +

"}";

SearchResponse searchResponse = getSearchResponse();

when(elasticUIService.findLogsByParameters(any(ESLogRequestDTO.class),

any(Pageable.class)))

.thenReturn(searchResponse);

mockMvc.perform(post("/api/logs/search")

.contentType(CONTENT_TYPE)

.content(searchRequest)

.accept(CONTENT_TYPE)

)

.andDo(document(CLASS_NAME_METHOD_NAME))

.andExpect(status().isOk())

.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE));

}

public SearchResponse getSearchResponse() {

SearchResponse searchResponse = new SearchResponse();

return searchResponse;

}

}

我不明白我如何用一些数据来模拟填充SearchResponse.有人有经验吗?也许有一些方法可以通过像searchRequest这样的json数据填充它?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值