From df6203a2049c53da0d7842597093f79c2cf13960 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Mon, 18 Jan 2016 18:02:52 +0000 Subject: [PATCH] Fix ordering problem with tests EnableDiscoveryClientMissingImplTests did not close its application context, so it caused other tests to fail on the command line (sometimes). --- .../springframework/cloud/AdhocTestSuite.java | 37 +++++++++++++++++ ...EnableDiscoveryClientMissingImplTests.java | 22 +++++----- ...ediaAutoConfigurationIntegrationTests.java | 41 ++++++++++++------- 3 files changed, 75 insertions(+), 25 deletions(-) create mode 100644 spring-cloud-commons/src/test/java/org/springframework/cloud/AdhocTestSuite.java diff --git a/spring-cloud-commons/src/test/java/org/springframework/cloud/AdhocTestSuite.java b/spring-cloud-commons/src/test/java/org/springframework/cloud/AdhocTestSuite.java new file mode 100644 index 00000000..95ead569 --- /dev/null +++ b/spring-cloud-commons/src/test/java/org/springframework/cloud/AdhocTestSuite.java @@ -0,0 +1,37 @@ +/* + * Copyright 2012-2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud; + +import org.junit.Ignore; +import org.junit.runner.RunWith; +import org.junit.runners.Suite; +import org.junit.runners.Suite.SuiteClasses; +import org.springframework.cloud.client.discovery.EnableDiscoveryClientMissingImplTests; +import org.springframework.cloud.client.hypermedia.CloudHypermediaAutoConfigurationIntegrationTests; + +/** + * A test suite for probing weird ordering problems in the tests. + * + * @author Dave Syer + */ +@RunWith(Suite.class) +@SuiteClasses({ EnableDiscoveryClientMissingImplTests.class, + CloudHypermediaAutoConfigurationIntegrationTests.class }) +@Ignore +public class AdhocTestSuite { + +} diff --git a/spring-cloud-commons/src/test/java/org/springframework/cloud/client/discovery/EnableDiscoveryClientMissingImplTests.java b/spring-cloud-commons/src/test/java/org/springframework/cloud/client/discovery/EnableDiscoveryClientMissingImplTests.java index ee023a81..b1a0c461 100644 --- a/spring-cloud-commons/src/test/java/org/springframework/cloud/client/discovery/EnableDiscoveryClientMissingImplTests.java +++ b/spring-cloud-commons/src/test/java/org/springframework/cloud/client/discovery/EnableDiscoveryClientMissingImplTests.java @@ -1,28 +1,27 @@ package org.springframework.cloud.client.discovery; -import static org.junit.Assert.assertTrue; - import org.junit.Test; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.Configuration; import org.springframework.core.NestedRuntimeException; +import static org.junit.Assert.assertTrue; + /** - * Tests that if @EnableDiscoveryClient is used, but there is no implementation - * on the classpath, then fail + * Tests that if @EnableDiscoveryClient is used, but there is no + * implementation on the classpath, then fail * @author Spencer Gibb */ public class EnableDiscoveryClientMissingImplTests { @Test public void testContextFails() { - try { - new SpringApplicationBuilder() - .sources(App.class) - .web(false) - .run(new String[0]); - } catch (NestedRuntimeException e) { + try (ConfigurableApplicationContext context = new SpringApplicationBuilder() + .sources(App.class).web(false).run(new String[0]);) { + } + catch (NestedRuntimeException e) { Throwable rootCause = e.getRootCause(); assertTrue(rootCause instanceof IllegalStateException); assertTrue(rootCause.getMessage().contains("no implementations")); @@ -32,7 +31,8 @@ public class EnableDiscoveryClientMissingImplTests { @EnableAutoConfiguration @Configuration @EnableDiscoveryClient - //this will fail with @EnableDiscoveryClient and no implementation (nothing in spring.factories) + // this will fail with @EnableDiscoveryClient and no implementation (nothing in + // spring.factories) public static class App { } } diff --git a/spring-cloud-commons/src/test/java/org/springframework/cloud/client/hypermedia/CloudHypermediaAutoConfigurationIntegrationTests.java b/spring-cloud-commons/src/test/java/org/springframework/cloud/client/hypermedia/CloudHypermediaAutoConfigurationIntegrationTests.java index 90c59b31..005d9ca0 100644 --- a/spring-cloud-commons/src/test/java/org/springframework/cloud/client/hypermedia/CloudHypermediaAutoConfigurationIntegrationTests.java +++ b/spring-cloud-commons/src/test/java/org/springframework/cloud/client/hypermedia/CloudHypermediaAutoConfigurationIntegrationTests.java @@ -15,9 +15,6 @@ */ package org.springframework.cloud.client.hypermedia; -import static org.hamcrest.Matchers.*; -import static org.junit.Assert.*; - import org.junit.Test; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; @@ -29,9 +26,15 @@ import org.springframework.context.annotation.Configuration; import org.springframework.hateoas.client.Traverson; import org.springframework.hateoas.client.Traverson.TraversalBuilder; +import static org.hamcrest.Matchers.arrayWithSize; +import static org.hamcrest.Matchers.hasSize; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.notNullValue; +import static org.junit.Assert.assertThat; + /** * Integration tests for {@link CloudHypermediaAutoConfiguration}. - * + * * @author Oliver Gierke */ public class CloudHypermediaAutoConfigurationIntegrationTests { @@ -39,9 +42,11 @@ public class CloudHypermediaAutoConfigurationIntegrationTests { @Test public void picksUpHypermediaProperties() { - try (ConfigurableApplicationContext context = getApplicationContext(ConfigWithRemoteResource.class)) { + try (ConfigurableApplicationContext context = getApplicationContext( + ConfigWithRemoteResource.class)) { - CloudHypermediaProperties properties = context.getBean(CloudHypermediaProperties.class); + CloudHypermediaProperties properties = context + .getBean(CloudHypermediaProperties.class); assertThat(properties.getRefresh().getInitialDelay(), is(50000)); assertThat(properties.getRefresh().getFixedDelay(), is(10000)); @@ -51,38 +56,46 @@ public class CloudHypermediaAutoConfigurationIntegrationTests { @Test public void doesNotCreateCloudHypermediaPropertiesifNotActive() { - try (ConfigurableApplicationContext context = getApplicationContext(Config.class)) { - assertThat(context.getBeanNamesForType(CloudHypermediaProperties.class), is(arrayWithSize(0))); + try (ConfigurableApplicationContext context = getApplicationContext( + Config.class)) { + assertThat(context.getBeanNamesForType(CloudHypermediaProperties.class), + is(arrayWithSize(0))); } } @Test public void doesNotRegisterResourceRefresherIfNoDiscoveredResourceIsDefined() { - try (ConfigurableApplicationContext context = getApplicationContext(Config.class)) { + try (ConfigurableApplicationContext context = getApplicationContext( + Config.class)) { assertThat(context.getBeansOfType(RemoteResource.class).values(), hasSize(0)); - assertThat(context.getBeanNamesForType(RemoteResourceRefresher.class), is(arrayWithSize(0))); + assertThat(context.getBeanNamesForType(RemoteResourceRefresher.class), + is(arrayWithSize(0))); } } @Test public void registersResourceRefresherIfDiscoverredResourceIsDefined() { - try (ConfigurableApplicationContext context = getApplicationContext(ConfigWithRemoteResource.class)) { + try (ConfigurableApplicationContext context = getApplicationContext( + ConfigWithRemoteResource.class)) { assertThat(context.getBeansOfType(RemoteResource.class).values(), hasSize(1)); - assertThat(context.getBean(RemoteResourceRefresher.class), is(notNullValue())); + assertThat(context.getBean(RemoteResourceRefresher.class), + is(notNullValue())); } } - private static ConfigurableApplicationContext getApplicationContext(Class configuration) { + private static ConfigurableApplicationContext getApplicationContext( + Class configuration) { return SpringApplication.run(configuration, new String[0]); } @Configuration @EnableAutoConfiguration - static class Config {} + static class Config { + } @Configuration @EnableAutoConfiguration