Refactor some tests to work with Spring Boot 1.4

This commit is contained in:
Dave Syer
2016-05-06 15:16:14 +01:00
parent 2e2c8b3e17
commit 9ce77ad22a
5 changed files with 93 additions and 92 deletions

View File

@@ -16,12 +16,11 @@
package org.springframework.cloud.sleuth;
import org.junit.Ignore;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
import org.springframework.cloud.sleuth.instrument.web.TraceFilterCustomExtractorTests;
import org.springframework.cloud.sleuth.instrument.web.client.WebClientExceptionTests;
import org.springframework.cloud.sleuth.instrument.web.RestTemplateTraceAspectIntegrationTests;
import org.springframework.cloud.sleuth.instrument.web.client.WebClientDiscoveryExceptionTests;
/**
* A test suite for probing weird ordering problems in the tests.
@@ -29,8 +28,9 @@ import org.springframework.cloud.sleuth.instrument.web.client.WebClientException
* @author Dave Syer
*/
@RunWith(Suite.class)
@SuiteClasses({ TraceFilterCustomExtractorTests.class, WebClientExceptionTests.class })
@Ignore
@SuiteClasses({ WebClientDiscoveryExceptionTests.class,
RestTemplateTraceAspectIntegrationTests.class })
// @Ignore
public class AdhocTestSuite {
}

View File

@@ -22,7 +22,6 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.core.env.Environment;
import org.springframework.http.MediaType;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.web.servlet.MockMvc;
@@ -39,7 +38,7 @@ import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.request.async.WebAsyncTask;
@SpringApplicationConfiguration(classes = {
RestTemplateTraceAspectIntegrationTests.CorrelationIdAspectTestConfiguration.class })
RestTemplateTraceAspectIntegrationTests.Config.class })
@RunWith(SpringJUnit4ClassRunner.class)
@WebIntegrationTest(randomPort = true)
@DirtiesContext
@@ -113,10 +112,9 @@ public class RestTemplateTraceAspectIntegrationTests {
.andExpect(status().isOk());
}
@EnableAsync
@DefaultTestAutoConfiguration
@Import(AspectTestingController.class)
public static class CorrelationIdAspectTestConfiguration {
public static class Config {
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();

View File

@@ -14,7 +14,6 @@ import org.springframework.cloud.sleuth.NoOpSpanReporter;
import org.springframework.cloud.sleuth.Span;
import org.springframework.cloud.sleuth.Tracer;
import org.springframework.cloud.sleuth.instrument.DefaultTestAutoConfiguration;
import org.springframework.cloud.sleuth.TraceKeys;
import org.springframework.cloud.sleuth.instrument.web.common.AbstractMvcIntegrationTest;
import org.springframework.cloud.sleuth.sampler.AlwaysSampler;
import org.springframework.context.annotation.Configuration;
@@ -29,30 +28,14 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(TraceFilterAlwaysSamplerIntegrationTests.class)
@DefaultTestAutoConfiguration
@RestController
@Configuration
@Import(AlwaysSampler.class)
@SpringApplicationConfiguration(TraceFilterAlwaysSamplerIntegrationTests.Config.class)
public class TraceFilterAlwaysSamplerIntegrationTests extends AbstractMvcIntegrationTest {
private static Log logger = LogFactory
.getLog(TraceFilterAlwaysSamplerIntegrationTests.class);
@Autowired
Tracer tracer;
@Autowired
TraceKeys traceKeys;
static Span span;
@RequestMapping("/ping")
public String ping() {
logger.info("ping");
span = this.tracer.getCurrentSpan();
return "ping";
}
@Test
public void when_always_sampler_is_used_span_is_exportable() throws Exception {
Long expectedTraceId = new Random().nextLong();
@@ -106,4 +89,23 @@ public class TraceFilterAlwaysSamplerIntegrationTests extends AbstractMvcIntegra
private Long tracingHeaderFrom(MvcResult mvcResult) {
return Span.hexToId(mvcResult.getResponse().getHeader(Span.TRACE_ID_NAME));
}
@DefaultTestAutoConfiguration
@RestController
@Configuration
@Import(AlwaysSampler.class)
static class Config {
@Autowired
private Tracer tracer;
@RequestMapping("/ping")
public String ping() {
logger.info("ping");
span = this.tracer.getCurrentSpan();
return "ping";
}
}
}

View File

@@ -1,5 +1,10 @@
package org.springframework.cloud.sleuth.instrument.web;
import static org.assertj.core.api.BDDAssertions.then;
import static org.springframework.cloud.sleuth.assertions.SleuthAssertions.then;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.asyncDispatch;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import java.util.Random;
import java.util.concurrent.CompletableFuture;
@@ -12,7 +17,6 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.autoconfigure.ManagementServerProperties;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.cloud.sleuth.Span;
import org.springframework.cloud.sleuth.TraceKeys;
import org.springframework.cloud.sleuth.Tracer;
import org.springframework.cloud.sleuth.instrument.DefaultTestAutoConfiguration;
import org.springframework.cloud.sleuth.instrument.web.common.AbstractMvcIntegrationTest;
@@ -26,47 +30,29 @@ import org.springframework.test.web.servlet.setup.DefaultMockMvcBuilder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import static org.springframework.cloud.sleuth.assertions.SleuthAssertions.then;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.asyncDispatch;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(TraceFilterIntegrationTests.class)
@DefaultTestAutoConfiguration
@RestController
@SpringApplicationConfiguration(TraceFilterIntegrationTests.Config.class)
public class TraceFilterIntegrationTests extends AbstractMvcIntegrationTest {
private static Log logger = LogFactory.getLog(TraceFilterIntegrationTests.class);
@Autowired Tracer tracer;
@Autowired TraceKeys traceKeys;
@Autowired TraceFilter traceFilter;
@Autowired
private TraceFilter traceFilter;
static Span span;
@RequestMapping("/ping")
public String ping() {
logger.info("ping");
span = this.tracer.getCurrentSpan();
return "ping";
}
@RequestMapping("/future")
public CompletableFuture<String> future() {
logger.info("future");
return CompletableFuture.completedFuture("ping");
}
private static Span span;
@Test
public void should_create_and_return_trace_in_HTTP_header() throws Exception {
MvcResult mvcResult = whenSentPingWithoutTracingData();
then(tracingHeaderFrom(mvcResult)).isNotNull();
then(TraceFilterIntegrationTests.span).hasLoggedAnEvent(Span.SERVER_RECV).hasLoggedAnEvent(Span.SERVER_SEND);
then(TraceFilterIntegrationTests.span).hasLoggedAnEvent(Span.SERVER_RECV)
.hasLoggedAnEvent(Span.SERVER_SEND);
}
@Test
public void should_ignore_sampling_the_span_if_uri_matches_management_properties_context_path() throws Exception {
public void should_ignore_sampling_the_span_if_uri_matches_management_properties_context_path()
throws Exception {
MvcResult mvcResult = whenSentInfoWithTraceId(new Random().nextLong());
then(notSampledHeaderIsPresent(mvcResult)).isEqualTo(true);
@@ -83,8 +69,7 @@ public class TraceFilterIntegrationTests extends AbstractMvcIntegrationTest {
}
@Test
public void when_message_is_sent_should_eventually_clear_mdc()
throws Exception {
public void when_message_is_sent_should_eventually_clear_mdc() throws Exception {
Long expectedTraceId = new Random().nextLong();
whenSentPingWithTraceId(expectedTraceId);
@@ -93,13 +78,12 @@ public class TraceFilterIntegrationTests extends AbstractMvcIntegrationTest {
}
@Test
public void when_traceId_is_sent_to_async_endpoint_span_is_joined()
throws Exception {
public void when_traceId_is_sent_to_async_endpoint_span_is_joined() throws Exception {
Long expectedTraceId = new Random().nextLong();
MvcResult mvcResult = whenSentFutureWithTraceId(expectedTraceId);
mvcResult = this.mockMvc.perform(asyncDispatch(mvcResult)).andExpect(status().isOk())
.andReturn();
mvcResult = this.mockMvc.perform(asyncDispatch(mvcResult))
.andExpect(status().isOk()).andReturn();
then(tracingHeaderFrom(mvcResult)).isEqualTo(expectedTraceId);
}
@@ -120,7 +104,8 @@ public class TraceFilterIntegrationTests extends AbstractMvcIntegrationTest {
}
private MvcResult whenSentInfoWithTraceId(Long passedTraceId) throws Exception {
return sendPingWithTraceId("/additionalContextPath/info", Span.TRACE_ID_NAME, passedTraceId);
return sendPingWithTraceId("/additionalContextPath/info", Span.TRACE_ID_NAME,
passedTraceId);
}
private MvcResult whenSentFutureWithTraceId(Long passedTraceId) throws Exception {
@@ -132,8 +117,8 @@ public class TraceFilterIntegrationTests extends AbstractMvcIntegrationTest {
return sendPingWithTraceId("/ping", headerName, traceId);
}
private MvcResult sendPingWithTraceId(String path, String headerName,
Long traceId) throws Exception {
private MvcResult sendPingWithTraceId(String path, String headerName, Long traceId)
throws Exception {
return this.mockMvc
.perform(MockMvcRequestBuilders.get(path).accept(MediaType.TEXT_PLAIN)
.header(headerName, Span.idToHex(traceId))
@@ -146,16 +131,39 @@ public class TraceFilterIntegrationTests extends AbstractMvcIntegrationTest {
}
private boolean notSampledHeaderIsPresent(MvcResult mvcResult) {
return Span.SPAN_NOT_SAMPLED.equals(mvcResult.getResponse().getHeader(Span.SAMPLED_NAME));
return Span.SPAN_NOT_SAMPLED
.equals(mvcResult.getResponse().getHeader(Span.SAMPLED_NAME));
}
@DefaultTestAutoConfiguration
@RestController
@Configuration
static class Config {
@Bean
ManagementServerProperties managementServerProperties() {
ManagementServerProperties managementServerProperties = new ManagementServerProperties();
managementServerProperties.setContextPath("/additionalContextPath");
return managementServerProperties;
protected static class Config {
@Autowired
private Tracer tracer;
@RequestMapping("/ping")
public String ping() {
logger.info("ping");
span = this.tracer.getCurrentSpan();
return "ping";
}
@RequestMapping("/future")
public CompletableFuture<String> future() {
logger.info("future");
return CompletableFuture.completedFuture("ping");
}
@Configuration
static class ManagementServer {
@Bean
ManagementServerProperties managementServerProperties() {
ManagementServerProperties managementServerProperties = new ManagementServerProperties();
managementServerProperties.setContextPath("/additionalContextPath");
return managementServerProperties;
}
}
}
}

View File

@@ -16,7 +16,6 @@
package org.springframework.cloud.sleuth.instrument.web.client;
import static junitparams.JUnitParamsRunner.$;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.junit.Assert.assertThat;
@@ -27,8 +26,6 @@ import java.util.Map;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
@@ -48,27 +45,20 @@ import org.springframework.cloud.sleuth.util.ExceptionUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.rules.SpringClassRule;
import org.springframework.test.context.junit4.rules.SpringMethodRule;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.client.RestTemplate;
import junitparams.JUnitParamsRunner;
import junitparams.Parameters;
@RunWith(JUnitParamsRunner.class)
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = {
WebClientDiscoveryExceptionTests.TestConfiguration.class })
@WebIntegrationTest(value = {
"spring.application.name=exceptionservice" }, randomPort = true)
@DirtiesContext
public class WebClientDiscoveryExceptionTests {
@ClassRule
public static final SpringClassRule SCR = new SpringClassRule();
@Rule
public final SpringMethodRule springMethodRule = new SpringMethodRule();
@Autowired
TestFeignInterfaceWithException testFeignInterfaceWithException;
@Autowired
@@ -90,9 +80,7 @@ public class WebClientDiscoveryExceptionTests {
}
// issue #240
@Test
@Parameters
public void shouldCloseSpanUponException(ResponseEntityProvider provider)
private void shouldCloseSpanUponException(ResponseEntityProvider provider)
throws IOException {
Span span = this.tracer.createSpan("new trace");
@@ -109,12 +97,17 @@ public class WebClientDiscoveryExceptionTests {
this.tracer.close(span);
}
Object[] parametersForShouldCloseSpanUponException() {
return $(
@Test
public void testFeignInterfaceWithException() throws Exception {
shouldCloseSpanUponException(
(ResponseEntityProvider) (tests) -> tests.testFeignInterfaceWithException
.shouldFailToConnect(),
(ResponseEntityProvider) (tests) -> tests.template
.getForEntity("http://exceptionservice/", Map.class));
.shouldFailToConnect());
}
@Test
public void testTemplate() throws Exception {
shouldCloseSpanUponException((ResponseEntityProvider) (tests) -> tests.template
.getForEntity("http://exceptionservice/", Map.class));
}
@FeignClient("exceptionservice")