GH-77 - Avoid exposing TestUtils to user applications.

Moved TestUtils from spring-modulith-test to spring-modulith-integration-test as it's only used there and doesn't need to be exposed to user applications.
This commit is contained in:
Oliver Drotbohm
2022-11-24 22:35:32 +01:00
parent b402f00c77
commit 02881539b6
3 changed files with 2 additions and 2 deletions

View File

@@ -24,8 +24,8 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigurationPackages;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.ApplicationContext;
import org.springframework.modulith.test.ApplicationModuleTest.BootstrapMode;
import org.springframework.modulith.test.TestUtils;
import org.springframework.modulith.test.ApplicationModuleTest.BootstrapMode;
import com.acme.myproject.NonVerifyingModuleTest;
import com.acme.myproject.moduleA.ServiceComponentA;

View File

@@ -21,8 +21,8 @@ import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.modulith.test.ApplicationModuleTest.BootstrapMode;
import org.springframework.modulith.test.TestUtils;
import org.springframework.modulith.test.ApplicationModuleTest.BootstrapMode;
import com.acme.myproject.NonVerifyingModuleTest;
import com.acme.myproject.moduleA.ServiceComponentA;

View File

@@ -0,0 +1,75 @@
/*
* Copyright 2018-2022 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
*
* https://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.modulith.test;
import static org.assertj.core.api.Assertions.*;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.UnsatisfiedDependencyException;
import org.springframework.boot.test.context.SpringBootContextLoader;
import org.springframework.boot.test.context.SpringBootTestContextBootstrapper;
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.test.context.BootstrapContext;
import org.springframework.test.context.CacheAwareContextLoaderDelegate;
import org.springframework.test.context.ContextLoadException;
import org.springframework.test.context.MergedContextConfiguration;
import org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate;
import org.springframework.test.context.support.DefaultBootstrapContext;
/**
* @author Oliver Drotbohm
*/
public class TestUtils {
public static void assertDependencyMissing(Class<?> testClass, Class<?> expectedMissingDependency) {
CacheAwareContextLoaderDelegate delegate = new DefaultCacheAwareContextLoaderDelegate();
BootstrapContext bootstrapContext = new DefaultBootstrapContext(testClass, delegate);
SpringBootTestContextBootstrapper bootstrapper = new SpringBootTestContextBootstrapper();
bootstrapper.setBootstrapContext(bootstrapContext);
MergedContextConfiguration configuration = bootstrapper.buildMergedContextConfiguration();
AssertableApplicationContext context = AssertableApplicationContext.get(() -> {
SpringBootContextLoader loader = new SpringBootContextLoader();
try {
return (ConfigurableApplicationContext) loader.loadContext(configuration);
} catch (ContextLoadException o_O) {
throw asRuntimeException(o_O.getCause());
} catch (Exception o_O) {
throw asRuntimeException(o_O);
}
});
assertThat(context).hasFailed();
assertThat(context).getFailure().isInstanceOfSatisfying(UnsatisfiedDependencyException.class, it -> {
assertThat(it.getMostSpecificCause()).isInstanceOfSatisfying(NoSuchBeanDefinitionException.class, ex -> {
assertThat(ex.getBeanType()).isEqualTo(expectedMissingDependency);
});
});
}
private static RuntimeException asRuntimeException(Throwable o_O) {
return o_O instanceof RuntimeException ? (RuntimeException) o_O : new RuntimeException(o_O);
}
}