Fix ordering problem with tests

EnableDiscoveryClientMissingImplTests did not close its application
context, so it caused other tests to fail on the command line
(sometimes).
This commit is contained in:
Dave Syer
2016-01-18 18:02:52 +00:00
parent 47c77cb165
commit df6203a204
3 changed files with 75 additions and 25 deletions

View File

@@ -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 {
}

View File

@@ -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 <code>@EnableDiscoveryClient</code> 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 {
}
}

View File

@@ -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