Moved to BDDAssertJ

This commit is contained in:
Marcin Grzejszczak
2015-12-01 15:13:56 +01:00
parent cc36d6b8ad
commit 4a1743c984
5 changed files with 88 additions and 21 deletions

View File

@@ -9,10 +9,12 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.cloud.client.loadbalancer.LoadBalancerAutoConfiguration;
import org.springframework.cloud.netflix.archaius.ArchaiusAutoConfiguration;
import org.springframework.cloud.sleuth.instrument.integration.TraceSpringIntegrationAutoConfiguration;
import org.springframework.context.annotation.Configuration;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@EnableAutoConfiguration(exclude = { TraceSpringIntegrationAutoConfiguration.class,
ArchaiusAutoConfiguration.class, LoadBalancerAutoConfiguration.class })
@Configuration
public @interface DefaultTestAutoConfiguration {
}

View File

@@ -1,5 +1,8 @@
package org.springframework.cloud.sleuth.instrument.scheduling;
import static com.jayway.awaitility.Awaitility.await;
import static org.assertj.core.api.BDDAssertions.then;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
@@ -7,9 +10,6 @@ import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.cloud.sleuth.Span;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static com.jayway.awaitility.Awaitility.await;
import static org.assertj.core.api.Assertions.assertThat;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = {ScheduledTestConfiguration.class})
public class TracingOnScheduledITest {
@@ -32,8 +32,8 @@ public class TracingOnScheduledITest {
@Override
public void run() {
Span storedSpan = beanWithScheduledMethod.getSpan();
assertThat(storedSpan).isNotNull();
assertThat(storedSpan.getTraceId()).isNotNull();
then(storedSpan).isNotNull();
then(storedSpan.getTraceId()).isNotNull();
}
};
}
@@ -42,7 +42,7 @@ public class TracingOnScheduledITest {
return new Runnable() {
@Override
public void run() {
assertThat(beanWithScheduledMethod.getSpan()).isNotEqualTo(spanToCompare);
then(beanWithScheduledMethod.getSpan()).isNotEqualTo(spanToCompare);
}
};
}

View File

@@ -1,6 +1,7 @@
package org.springframework.cloud.sleuth.instrument.web;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.BDDAssertions.then;
import java.util.concurrent.atomic.AtomicReference;
@@ -56,9 +57,8 @@ public class TraceAsyncITest {
Awaitility.await().until(new Runnable() {
@Override
public void run() {
assertThat(span.getTraceId()).isNotNull().isEqualTo(TraceAsyncITest.this.asyncClass.getTraceId());
assertThat(span.getName()).isNotEqualTo(TraceAsyncITest.this.asyncClass.getSpanName());
}
then(span.getTraceId()).isNotNull().isEqualTo(asyncClass.getTraceId());
then(span.getName()).isNotEqualTo(asyncClass.getSpanName()); }
});
}

View File

@@ -0,0 +1,65 @@
package org.springframework.cloud.sleuth.instrument.web;
import static org.assertj.core.api.BDDAssertions.then;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.cloud.sleuth.Trace;
import org.springframework.cloud.sleuth.TraceManager;
import org.springframework.cloud.sleuth.instrument.DefaultTestAutoConfiguration;
import org.springframework.cloud.sleuth.instrument.web.common.MvcITest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.DefaultMockMvcBuilder;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(TraceFilterITest.class)
@DefaultTestAutoConfiguration
@Ignore("Will fail cause no tracing data is set on the TraceFilter")
public class TraceFilterITest extends MvcITest {
@Autowired TraceManager traceManager;
@Test
public void should_create_and_return_trace_in_HTTP_header() throws Exception {
MvcResult mvcResult = whenSentPingWithoutTracingData();
then(tracingHeaderFrom(mvcResult)).isNotNull().isNotEmpty();
}
@Test
public void when_correlationId_is_sent_should_not_create_a_new_one_but_return_the_existing_one_instead() throws Exception {
String expectedTraceId = "passedCorId";
MvcResult mvcResult = whenSentPingWithTraceId(expectedTraceId);
then(tracingHeaderFrom(mvcResult)).isEqualTo(expectedTraceId);
}
@Override
protected void configureMockMvcBuilder(DefaultMockMvcBuilder mockMvcBuilder) {
mockMvcBuilder.addFilters(new TraceFilter(traceManager));
}
private MvcResult whenSentPingWithoutTracingData() throws Exception {
return mockMvc.perform(MockMvcRequestBuilders.get("/ping").accept(MediaType.TEXT_PLAIN)).andReturn();
}
private MvcResult whenSentPingWithTraceId(String passedCorrelationId) throws Exception {
return sendPingWithTraceId(Trace.TRACE_ID_NAME, passedCorrelationId);
}
private MvcResult sendPingWithTraceId(String headerName, String passedCorrelationId) throws Exception {
return mockMvc.perform(MockMvcRequestBuilders.get("/ping").accept(MediaType.TEXT_PLAIN)
.header(headerName, passedCorrelationId)).andReturn();
}
private String tracingHeaderFrom(MvcResult mvcResult) {
return mvcResult.getResponse().getHeader(Trace.TRACE_ID_NAME);
}
}

View File

@@ -1,6 +1,6 @@
package org.springframework.cloud.sleuth.instrument.web.client;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.BDDAssertions.then;
import java.util.ArrayList;
import java.util.Arrays;
@@ -69,8 +69,8 @@ public class FeignTraceTest {
ResponseEntity<String> response = this.testFeignInterface.getNoTrace();
// then
assertThat(getHeader(response, Trace.TRACE_ID_NAME)).isNull();
assertThat(this.listener.getEvents()).isEmpty();
then(getHeader(response, Trace.TRACE_ID_NAME)).isNull();
then(this.listener.getEvents()).isEmpty();
}
@Test
@@ -86,10 +86,10 @@ public class FeignTraceTest {
ResponseEntity<String> response = this.testFeignInterface.getTraceId();
// then
assertThat(getHeader(response, Trace.TRACE_ID_NAME)).isEqualTo(currentTraceId);
assertThat(getHeader(response, Trace.SPAN_ID_NAME)).isEqualTo(currentSpanId);
assertThat(getHeader(response, Trace.PARENT_ID_NAME)).isEqualTo(currentParentId);
assertThat(this.listener.getEvents().size()).isEqualTo(2);
then(getHeader(response, Trace.TRACE_ID_NAME)).isEqualTo(currentTraceId);
then(getHeader(response, Trace.SPAN_ID_NAME)).isEqualTo(currentSpanId);
then(getHeader(response, Trace.PARENT_ID_NAME)).isEqualTo(currentParentId);
then(this.listener.getEvents().size()).isEqualTo(2);
}
private String getHeader(ResponseEntity<String> response, String name) {
@@ -148,7 +148,7 @@ public class FeignTraceTest {
@RequestMapping(value = "/notrace", method = RequestMethod.GET)
public String notrace(
@RequestHeader(name = Trace.TRACE_ID_NAME, required = false) String traceId) {
assertThat(traceId).isNull();
then(traceId).isNull();
return "OK";
}
@@ -156,9 +156,9 @@ public class FeignTraceTest {
public String traceId(@RequestHeader(Trace.TRACE_ID_NAME) String traceId,
@RequestHeader(Trace.SPAN_ID_NAME) String spanId,
@RequestHeader(Trace.PARENT_ID_NAME) String parentId) {
assertThat(traceId).isNotEmpty();
assertThat(parentId).isNotEmpty();
assertThat(spanId).isNotEmpty();
then(traceId).isNotEmpty();
then(parentId).isNotEmpty();
then(spanId).isNotEmpty();
return traceId;
}
}