### Question:
Mackerel { public void send( final List<Metric> metrics ) { Objects.requireNonNull(metrics); if (CollectionUtils.isEmpty(metrics)) { return; } final var headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); headers.set(API_KEY_HEADER, apiKey); final HttpEntity<?> request = new HttpEntity<>(metrics, headers); final Status status = restTemplate.postForObject(ENDPOINT, request, Status.class); if (status == null) { throw new MackerelException("Failed to call Mackerel API. An empty response received."); } if (!status.isSuccess()) { throw new MackerelException( String.format( "Failed to call Mackerel API :: success=%s, metrics=%s", String.valueOf(status.isSuccess()), metrics.toString())); } } Mackerel(
        final RestTemplateBuilder restTemplateBuilder,
        final String apiKey
    ); void send(
        final List<Metric> metrics
    );  }### Answer:
@Test public void sendTest() { final List<Metric> metrics = List.of( Metric.builder() .name("metric-1") .time(1L) .value(BigDecimal.ONE) .build(), Metric.builder() .name("metric-2") .time(2L) .value(BigDecimal.valueOf(2L)) .build()); final Status status = Status.builder().success(true).build(); when(restTemplate.postForObject(eq(ENDPOINT), entityCaptor.capture(), eq(Status.class))) .thenReturn(status); mackerel.send(metrics); verify(restTemplate).postForObject(eq(ENDPOINT), any(), eq(Status.class)); final HttpEntity<?> entity = entityCaptor.getValue(); final HttpHeaders headers = entity.getHeaders(); assertEquals(MediaType.APPLICATION_JSON, headers.getContentType()); assertEquals(API_KEY, headers.getFirst(API_KEY_HEADER)); assertEquals(metrics, entity.getBody()); }
@Test @SuppressWarnings("ConstantConditions") public void sendTest_nullMetrics() { expectedException.expect(NullPointerException.class); mackerel.send(null); }
@Test public void sendTest_emptyMetrics() { mackerel.send(Collections.emptyList()); verify(restTemplate, never()).postForObject(anyString(), any(), any()); }
@Test public void sendTest_nullResponse() { expectedException.expect(MackerelException.class); expectedException.expectMessage("Failed to call Mackerel API. An empty response received."); final List<Metric> metrics = List.of( Metric.builder() .name("metric-1") .time(1L) .value(BigDecimal.ONE) .build()); when(restTemplate.postForObject(eq(ENDPOINT), any(), eq(Status.class))) .thenReturn(null); mackerel.send(metrics); verify(restTemplate).postForObject(eq(ENDPOINT), any(), eq(Status.class)); }
@Test public void sendTest_failedResponse() { expectedException.expect(MackerelException.class); expectedException.expectMessage("Failed to call Mackerel API :: success=false, metrics="); final List<Metric> metrics = List.of( Metric.builder() .name("metric-1") .time(1L) .value(BigDecimal.ONE) .build()); final Status status = Status.builder().success(false).build(); when(restTemplate.postForObject(eq(ENDPOINT), any(), eq(Status.class))) .thenReturn(status); mackerel.send(metrics); verify(restTemplate).postForObject(eq(ENDPOINT), any(), eq(Status.class)); }