GH-28 - Consolidate sample and integration tests module.

This commit is contained in:
Oliver Drotbohm
2022-07-27 12:03:47 +02:00
parent 9512c36c32
commit 13b83ef947
41 changed files with 23 additions and 208 deletions

View File

@@ -0,0 +1,73 @@
/*
* 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 com.acme.myproject;
import static org.assertj.core.api.Assertions.*;
import org.junit.jupiter.api.Test;
import org.springframework.modulith.model.ApplicationModules;
import org.springframework.modulith.model.ApplicationModules.Filters;
import org.springframework.modulith.model.Violations;
import com.acme.myproject.invalid.InvalidComponent;
import com.acme.myproject.moduleB.internal.InternalComponentB;
import com.tngtech.archunit.base.DescribedPredicate;
import com.tngtech.archunit.core.domain.JavaClass;
/**
* Test cases to verify the validity of the overall modulith rules
*
* @author Oliver Drotbohm
* @author Peter Gafert
*/
class ModulithTest {
static final DescribedPredicate<JavaClass> DEFAULT_EXCLUSIONS = Filters.withoutModules("cycleA", "cycleB", "invalid2",
"fieldinjected");
@Test
void verifyModules() {
String componentName = InternalComponentB.class.getSimpleName();
assertThatExceptionOfType(Violations.class) //
.isThrownBy(() -> ApplicationModules.of(Application.class, DEFAULT_EXCLUSIONS).verify()) //
.withMessageContaining(String.format("Module '%s' depends on non-exposed type %s within module 'moduleB'",
"invalid", InternalComponentB.class.getName()))
.withMessageContaining(String.format("%s declares constructor %s(%s)", InvalidComponent.class.getSimpleName(),
InvalidComponent.class.getSimpleName(), componentName));
}
@Test
void verifyModulesWithoutInvalid() {
ApplicationModules.of(Application.class, DEFAULT_EXCLUSIONS.or(Filters.withoutModule("invalid"))).verify();
}
@Test
void detectsCycleBetweenModules() {
assertThatExceptionOfType(Violations.class) //
.isThrownBy(() -> ApplicationModules.of(Application.class, Filters.withoutModules("invalid", "invalid2")).verify()) //
// mentions modules
.withMessageContaining("cycleA") //
.withMessageContaining("cycleB") //
// mentions offending types
.withMessageContaining("CycleA") //
.withMessageContaining("CycleB");
}
}

View File

@@ -0,0 +1,37 @@
/*
* 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 com.acme.myproject;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.core.annotation.AliasFor;
import org.springframework.modulith.test.ApplicationModuleTest;
import org.springframework.modulith.test.ApplicationModuleTest.BootstrapMode;
/**
* @author Oliver Drotbohm
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@ApplicationModuleTest(verifyAutomatically = false)
public @interface NonVerifyingModuleTest {
@AliasFor(annotation = ApplicationModuleTest.class, attribute = "mode")
BootstrapMode value() default BootstrapMode.STANDALONE;
}

View File

@@ -0,0 +1,44 @@
/*
* 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 com.acme.myproject.complex;
import static org.assertj.core.api.Assertions.*;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.modulith.model.NamedInterface;
import org.springframework.modulith.model.NamedInterfaces;
import org.springframework.modulith.test.ModuleTestExecution;
import com.acme.myproject.NonVerifyingModuleTest;
/**
* @author Oliver Drotbohm
*/
@NonVerifyingModuleTest
class ComplexTest {
@Autowired ModuleTestExecution moduleTest;
@Test
void exposesNamedInterfaces() {
NamedInterfaces interfaces = moduleTest.getModule().getNamedInterfaces();
assertThat(interfaces.stream().map(NamedInterface::getName)) //
.containsExactlyInAnyOrder("API", "SPI", "Port 1", "Port 2", "Port 3");
}
}

View File

@@ -0,0 +1,50 @@
/*
* Copyright 2019-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 com.acme.myproject.fieldinjected;
import static org.assertj.core.api.Assertions.*;
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.model.ApplicationModules;
import org.springframework.modulith.test.ModuleTestExecution;
import com.acme.myproject.NonVerifyingModuleTest;
import com.acme.myproject.moduleA.ServiceComponentA;
/**
* Integration tests to verify field injection is rejected.
*
* @author Oliver Drotbohm
*/
@NonVerifyingModuleTest
class FieldInjectedIntegrationTest {
@Autowired ModuleTestExecution execution;
@MockBean ServiceComponentA dependency;
@Test
void rejectsFieldInjection() {
ApplicationModules modules = execution.getModules();
assertThat(execution.getModule().detectDependencies(modules)) //
.hasMessageContaining("field injection") //
.hasMessageContaining("WithFieldInjection.a"); // offending field
}
}

View File

@@ -0,0 +1,63 @@
/*
* 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 com.acme.myproject.moduleA;
import static org.assertj.core.api.Assertions.*;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.modulith.test.PublishedEvents;
import org.springframework.modulith.test.PublishedEvents.TypedPublishedEvents;
import com.acme.myproject.NonVerifyingModuleTest;
import com.acme.myproject.moduleB.ServiceComponentB;
/**
* @author Oliver Drotbohm
*/
@NonVerifyingModuleTest
class ModuleATest {
@Autowired ApplicationContext context;
@Autowired PublishedEvents events;
@Test
void bootstrapsModuleAOnly() {
context.getBean(ServiceComponentA.class);
assertThatExceptionOfType(NoSuchBeanDefinitionException.class)
.isThrownBy(() -> context.getBean(ServiceComponentB.class));
}
@Test
void assertEventsFired() {
context.getBean(ServiceComponentA.class).fireEvent();
TypedPublishedEvents<SomeEventA> matching = events.ofType(SomeEventA.class) //
.matching(it -> it.getMessage().equals("Message"));
assertThat(matching).hasSize(1);
}
@Test
void injectsPublishedEventsIntoMethod(PublishedEvents events) {
assertThat(events).isNotNull();
}
}

View File

@@ -0,0 +1,91 @@
/*
* 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 com.acme.myproject.moduleB;
import static org.assertj.core.api.Assertions.*;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.mockito.internal.creation.bytebuddy.MockAccess;
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 com.acme.myproject.NonVerifyingModuleTest;
import com.acme.myproject.moduleA.ServiceComponentA;
import com.acme.myproject.moduleB.internal.InternalComponentB;
/**
* @author Oliver Drotbohm
*/
class ModuleBTest {
@Nested
static class WithoutMocksTest {
@Autowired ServiceComponentB serviceComponentB;
@NonVerifyingModuleTest
static class Config {}
@Test
void failsToStartBecauseServiceComponentAIsMissing() throws Exception {
TestUtils.assertDependencyMissing(WithoutMocksTest.Config.class, ServiceComponentA.class);
}
}
@Nested
@NonVerifyingModuleTest
static class WithMocksTest {
@Autowired ApplicationContext context;
@MockBean ServiceComponentA serviceComponentA;
@Test
void bootstrapsModuleB() {
context.getBean(ServiceComponentB.class);
assertThat(context.getBean(ServiceComponentA.class)).isInstanceOf(MockAccess.class);
}
@Test
void considersNestedPackagePartOfTheModuleByDefault() {
context.getBean(InternalComponentB.class);
}
@Test
void tweaksAutoConfigurationPackageToModulePackage() {
assertThat(AutoConfigurationPackages.get(context)) //
.containsExactly(getClass().getPackage().getName());
}
}
@Nested
@NonVerifyingModuleTest(BootstrapMode.DIRECT_DEPENDENCIES)
static class WithUpstreamModuleTest {
@Autowired ServiceComponentA componentA;
@Autowired ServiceComponentB componentB;
@Test
void bootstrapsContext() {}
}
}

View File

@@ -0,0 +1,85 @@
/*
* 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 com.acme.myproject.moduleC;
import static org.assertj.core.api.Assertions.*;
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 com.acme.myproject.NonVerifyingModuleTest;
import com.acme.myproject.moduleA.ServiceComponentA;
import com.acme.myproject.moduleB.ServiceComponentB;
/**
* @author Oliver Drotbohm
*/
class ModuleCTest {
@Nested
public static class FailsStandaloneTest {
@NonVerifyingModuleTest
static class Config {}
@Test
void failsStandalone() {
TestUtils.assertDependencyMissing(FailsStandaloneTest.Config.class, ServiceComponentB.class);
}
}
@Nested
static class FailsWithDirectDependencyTest {
@NonVerifyingModuleTest(BootstrapMode.DIRECT_DEPENDENCIES)
static class Config {}
@Test
void failsWithDirectDependency() {
TestUtils.assertDependencyMissing(FailsWithDirectDependencyTest.Config.class, ServiceComponentA.class);
}
}
@Nested
@NonVerifyingModuleTest(BootstrapMode.DIRECT_DEPENDENCIES)
static class SucceedsWithDirectDependencyPlusItsDependenciesMocksTest {
@MockBean ServiceComponentA serviceComponentA;
@Test
void bootstrapsContext() {
assertThat(serviceComponentA).isNotNull();
}
}
@Nested
@NonVerifyingModuleTest(BootstrapMode.ALL_DEPENDENCIES)
static class SucceedsWithAllDependenciesTest {
@Autowired ServiceComponentA serviceComponentA;
@Autowired ServiceComponentB serviceComponentB;
@Test
void bootstrapsContext() {
assertThat(serviceComponentA).isNotNull();
assertThat(serviceComponentB).isNotNull();
}
}
}

View File

@@ -0,0 +1,94 @@
/*
* 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.docs;
import static org.assertj.core.api.Assertions.*;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Comparator;
import java.util.Optional;
import org.junit.jupiter.api.Test;
import org.springframework.modulith.docs.Documenter.Options;
import org.springframework.modulith.model.ApplicationModule;
import org.springframework.modulith.model.ApplicationModule.DependencyType;
import com.acme.myproject.Application;
/**
* Unit tests for {@link Documenter}.
*
* @author Oliver Drotbohm
*/
class DocumenterTest {
Documenter documenter = new Documenter(Application.class);
@Test
void writesComponentStructureAsPlantUml() throws IOException {
documenter.toPlantUml();
}
@Test
void writesSingleModuleDocumentation() throws IOException {
ApplicationModule module = documenter.getModules().getModuleByName("moduleB") //
.orElseThrow(() -> new IllegalArgumentException());
documenter.writeModuleAsPlantUml(module, Options.defaults() //
.withColorSelector(it -> Optional.of("#ff0000")) //
.withDefaultDisplayName(it -> it.getDisplayName().toUpperCase()));
Options options = Options.defaults() //
.withComponentFilter(component -> component.getRelationships().stream()
.anyMatch(it -> it.getTagsAsSet().contains(DependencyType.EVENT_LISTENER.toString())));
documenter.writeModulesAsPlantUml(options);
}
@Test
void testName() {
documenter.getModules().stream() //
.map(it -> documenter.toModuleCanvas(it));
}
@Test
void customizesOutputLocation() throws IOException {
String customOutputFolder = "build/spring-modulith";
Path path = Paths.get(customOutputFolder);
try {
documenter.withOutputFolder(customOutputFolder).writeModuleCanvases();
assertThat(Files.list(path)).isNotEmpty();
assertThat(path).exists();
} finally {
Files.walk(path)
.sorted(Comparator.reverseOrder())
.map(Path::toFile)
.forEach(File::delete);
}
}
}