Introduce assertj assertions (#315)
* Introduce assertJ assertions in some tests
* Fix code formatting to be inline with Spring rules
This commit is contained in:
@@ -24,7 +24,6 @@ import java.util.Map;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.assertj.core.api.AbstractAssert;
|
||||
import org.assertj.core.api.Assertions;
|
||||
import org.springframework.cloud.sleuth.Span;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
@@ -32,6 +31,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import static java.util.stream.Collectors.joining;
|
||||
import static java.util.stream.Collectors.toList;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class ListOfSpansAssert extends AbstractAssert<ListOfSpansAssert, ListOfSpans> {
|
||||
|
||||
@@ -60,7 +60,7 @@ public class ListOfSpansAssert extends AbstractAssert<ListOfSpansAssert, ListOfS
|
||||
log.info("Difference between parent ids and span ids " +
|
||||
difference.stream().map(span -> "id as long [" + span + "] and as hex [" + Span.idToHex(span) + "]").collect(
|
||||
joining("\n")));
|
||||
Assertions.assertThat(spanIds).containsAll(parentSpanIds);
|
||||
assertThat(spanIds).containsAll(parentSpanIds);
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -70,12 +70,12 @@ public class ListOfSpansAssert extends AbstractAssert<ListOfSpansAssert, ListOfS
|
||||
List<Span> matchingSpans = this.actual.spans.stream()
|
||||
.filter(span -> span.getName().equals(name) && span.logs().stream().filter(entry ->
|
||||
entry.getEvent().equals(Span.CLIENT_SEND)).findAny().isPresent()).collect(toList());
|
||||
Assertions.assertThat(matchingSpans).isNotEmpty();
|
||||
assertThat(matchingSpans).isNotEmpty();
|
||||
List<Map<String, String>> matchingSpansTags = matchingSpans.stream().map(Span::tags).collect(
|
||||
toList());
|
||||
Map<String, String> spanTags = new HashMap<>();
|
||||
matchingSpansTags.forEach(spanTags::putAll);
|
||||
Assertions.assertThat(spanTags.entrySet()).containsAll(tags.entrySet());
|
||||
assertThat(spanTags.entrySet()).containsAll(tags.entrySet());
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@@ -19,8 +19,8 @@ package org.springframework.cloud.sleuth.instrument.web.client;
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
import org.assertj.core.api.Assertions;
|
||||
import org.junit.After;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
@@ -49,9 +49,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.CoreMatchers.nullValue;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringApplicationConfiguration(classes = {
|
||||
@@ -84,12 +82,12 @@ public class WebClientDiscoveryExceptionTests {
|
||||
|
||||
try {
|
||||
provider.get(this);
|
||||
Assert.fail("should throw an exception");
|
||||
Assertions.fail("should throw an exception");
|
||||
}
|
||||
catch (RuntimeException e) {
|
||||
}
|
||||
|
||||
assertThat(ExceptionUtils.getLastException(), is(nullValue()));
|
||||
assertThat(ExceptionUtils.getLastException()).isNull();
|
||||
|
||||
SleuthAssertions.then(this.tracer.getCurrentSpan()).isEqualTo(span);
|
||||
this.tracer.close(span);
|
||||
|
||||
@@ -30,14 +30,11 @@ import org.springframework.cloud.sleuth.Span;
|
||||
import org.springframework.cloud.sleuth.SpanNamer;
|
||||
import org.springframework.cloud.sleuth.SpanReporter;
|
||||
import org.springframework.cloud.sleuth.Tracer;
|
||||
import org.springframework.cloud.sleuth.assertions.SleuthAssertions;
|
||||
import org.springframework.cloud.sleuth.log.SpanLogger;
|
||||
import org.springframework.cloud.sleuth.sampler.AlwaysSampler;
|
||||
import org.springframework.cloud.sleuth.sampler.NeverSampler;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.atLeast;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
@@ -93,14 +90,14 @@ public class DefaultTracerTests {
|
||||
|
||||
List<Span> spans = new ArrayList<>(captor.getAllValues());
|
||||
|
||||
assertThat("spans was wrong size", spans.size(), is(NUM_SPANS));
|
||||
assertThat(spans).hasSize(NUM_SPANS);
|
||||
|
||||
Span root = assertSpan(spans, null, CREATE_SIMPLE_TRACE);
|
||||
Span child = assertSpan(spans, root.getSpanId(), IMPORTANT_WORK_1);
|
||||
Span grandChild = assertSpan(spans, child.getSpanId(), IMPORTANT_WORK_2);
|
||||
|
||||
List<Span> gen4 = findSpans(spans, grandChild.getSpanId());
|
||||
assertThat("gen4 was non-empty", gen4.isEmpty(), is(true));
|
||||
assertThat(gen4).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -108,7 +105,7 @@ public class DefaultTracerTests {
|
||||
DefaultTracer tracer = new DefaultTracer(NeverSampler.INSTANCE, new Random(),
|
||||
this.spanNamer, this.spanLogger, this.spanReporter);
|
||||
Span span = tracer.createSpan(CREATE_SIMPLE_TRACE);
|
||||
assertThat(span.isExportable(), is(false));
|
||||
assertThat(span.isExportable()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -116,7 +113,7 @@ public class DefaultTracerTests {
|
||||
DefaultTracer tracer = new DefaultTracer(new AlwaysSampler(), new Random(),
|
||||
this.spanNamer, this.spanLogger, this.spanReporter);
|
||||
Span span = tracer.createSpan(CREATE_SIMPLE_TRACE);
|
||||
assertThat(span.isExportable(), is(true));
|
||||
assertThat(span.isExportable()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -124,9 +121,9 @@ public class DefaultTracerTests {
|
||||
DefaultTracer tracer = new DefaultTracer(new AlwaysSampler(), new Random(),
|
||||
this.spanNamer, this.spanLogger, this.spanReporter);
|
||||
Span span = tracer.createSpan(CREATE_SIMPLE_TRACE, NeverSampler.INSTANCE);
|
||||
assertThat(span.isExportable(), is(false));
|
||||
assertThat(span.isExportable()).isFalse();
|
||||
Span child = tracer.createSpan(CREATE_SIMPLE_TRACE_SPAN_NAME + "/child", span);
|
||||
assertThat(child.isExportable(), is(false));
|
||||
assertThat(child.isExportable()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -136,7 +133,7 @@ public class DefaultTracerTests {
|
||||
Span parent = tracer.createSpan(CREATE_SIMPLE_TRACE);
|
||||
Span span = tracer.createSpan(IMPORTANT_WORK_1, parent);
|
||||
tracer.close(span);
|
||||
assertThat(tracer.getCurrentSpan(), is(equalTo(parent)));
|
||||
assertThat(tracer.getCurrentSpan()).isEqualTo(parent);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -147,7 +144,7 @@ public class DefaultTracerTests {
|
||||
.build();
|
||||
Span span = tracer.createSpan(IMPORTANT_WORK_1, parent);
|
||||
tracer.close(span);
|
||||
assertThat(tracer.getCurrentSpan(), is(equalTo(null)));
|
||||
assertThat(tracer.getCurrentSpan()).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -159,7 +156,7 @@ public class DefaultTracerTests {
|
||||
.build();
|
||||
Span span = tracer.createSpan(IMPORTANT_WORK_2, parent);
|
||||
tracer.close(span);
|
||||
assertThat(tracer.getCurrentSpan(), is(equalTo(grandParent)));
|
||||
assertThat(tracer.getCurrentSpan()).isEqualTo(grandParent);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -169,7 +166,7 @@ public class DefaultTracerTests {
|
||||
|
||||
Span span = tracer.createChild(null, "childName");
|
||||
|
||||
SleuthAssertions.assertThat(span.isExportable()).isFalse();
|
||||
assertThat(span.isExportable()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -190,10 +187,9 @@ public class DefaultTracerTests {
|
||||
|
||||
private Span assertSpan(List<Span> spans, Long parentId, String name) {
|
||||
List<Span> found = findSpans(spans, parentId);
|
||||
assertThat("more than one span with parentId " + parentId, found.size(), is(1));
|
||||
assertThat(found).as("More than one span with parentId %s", parentId).hasSize(1);
|
||||
Span span = found.get(0);
|
||||
assertThat("name is wrong for span with parentId " + parentId, span.getName(),
|
||||
is(name));
|
||||
assertThat(span.getName()).as("Name should be %s", name).isEqualTo(name);
|
||||
return span;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user