Fix package tangle in bean override tests

This commit fixes a package tangle between the root bean override
package and its sub-packages. This was vastly improved with the
introduction of `@DummyBean` but we still had a few tests that were
at the wrong place.
This commit is contained in:
Stéphane Nicoll 2024-06-10 15:23:36 +02:00
parent 96302a7102
commit b9a6ba1242
4 changed files with 263 additions and 168 deletions

View File

@ -1,168 +0,0 @@
/*
* Copyright 2002-2024 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.test.context.bean.override;
import java.util.Collections;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.mockito.Answers;
import org.springframework.test.context.ContextCustomizer;
import org.springframework.test.context.bean.override.convention.TestBean;
import org.springframework.test.context.bean.override.mockito.MockitoBean;
import org.springframework.test.context.bean.override.mockito.MockitoSpyBean;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Bean override tests that validate the behavior with the TCF context cache.
*
* @author Stephane Nicoll
*/
@SuppressWarnings("unused")
class BeanOverrideContextCustomizerEqualityTests {
private final BeanOverrideContextCustomizerFactory factory = new BeanOverrideContextCustomizerFactory();
@Nested
class SameContextTests {
@Test
void testsWithOneIdenticalTestBean() {
assertThat(createContextCustomizer(Case1.class)).isEqualTo(createContextCustomizer(Case2.class));
}
@Test
void testsWithOneIdenticalMockitoMockBean() {
assertThat(createContextCustomizer(Case4.class)).isEqualTo(createContextCustomizer(Case5.class));
}
@Test
void testsWithOneIdenticalMockitoSpyBean() {
assertThat(createContextCustomizer(Case7.class)).isEqualTo(createContextCustomizer(Case8.class));
}
}
@Nested
class DifferentContextTests {
@Test
void testsWithSimilarTestBeanButDifferentMethod() {
assertThat(createContextCustomizer(Case1.class)).isNotEqualTo(createContextCustomizer(Case3.class));
}
@Test
void testsWithSimilarMockitoMockButDifferentAnswers() {
assertThat(createContextCustomizer(Case4.class)).isNotEqualTo(createContextCustomizer(Case6.class));
}
@Test
void testsWithSimilarMockitoSpyButDifferentProxyTargetClass() {
assertThat(createContextCustomizer(Case8.class)).isNotEqualTo(createContextCustomizer(Case9.class));
}
@Test
void testsWithSameConfigurationButOneIsMockitoBeanAndTheOtherMockitoSpy() {
assertThat(createContextCustomizer(Case4.class)).isNotEqualTo(createContextCustomizer(Case7.class));
}
}
private ContextCustomizer createContextCustomizer(Class<?> testClass) {
BeanOverrideContextCustomizer customizer = this.factory.createContextCustomizer(
testClass, Collections.emptyList());
assertThat(customizer).isNotNull();
return customizer;
}
interface DescriptionProvider {
static String createDescription() {
return "override";
}
}
static class Case1 implements DescriptionProvider {
@TestBean(methodName = "createDescription")
private String description;
}
static class Case2 implements DescriptionProvider {
@TestBean(methodName = "createDescription")
private String description;
}
static class Case3 implements DescriptionProvider {
@TestBean(methodName = "createDescription")
private String description;
static String createDescription() {
return "another value";
}
}
static class Case4 {
@MockitoBean
private String exampleService;
}
static class Case5 {
@MockitoBean
private String serviceToMock;
}
static class Case6 {
@MockitoBean(answers = Answers.RETURNS_MOCKS)
private String exampleService;
}
static class Case7 {
@MockitoSpyBean
private String exampleService;
}
static class Case8 {
@MockitoSpyBean
private String serviceToMock;
}
static class Case9 {
@MockitoSpyBean(proxyTargetAware = false)
private String serviceToMock;
}
}

View File

@ -0,0 +1,47 @@
/*
* Copyright 2002-2024 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.test.context.bean.override;
import java.util.Collections;
import org.springframework.lang.Nullable;
import org.springframework.test.context.ContextCustomizer;
/**
* Test utilities for {@link BeanOverrideContextCustomizer} that are public so
* that specific bean override implementations can use them.
*
* @author Stephane Nicoll
*/
public abstract class BeanOverrideContextCustomizerTestUtils {
private static final BeanOverrideContextCustomizerFactory factory = new BeanOverrideContextCustomizerFactory();
/**
* Create a {@link ContextCustomizer} for the given {@code testClass}. Return
* a customizer to handle any use of {@link BeanOverride} or {@code null} if
* the test class does not use them.
* @param testClass a test class to introspect
* @return a context customizer for bean override support, or null
*/
@Nullable
public static ContextCustomizer createContextCustomizer(Class<?> testClass) {
return factory.createContextCustomizer(testClass, Collections.emptyList());
}
}

View File

@ -0,0 +1,107 @@
/*
* Copyright 2002-2024 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.test.context.bean.override.convention;
import org.junit.jupiter.api.Test;
import org.springframework.test.context.ContextCustomizer;
import org.springframework.test.context.bean.override.BeanOverrideContextCustomizerTestUtils;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests that validate the behavior of {@link TestBean} with the TCF context cache.
*
* @author Stephane Nicoll
*/
class TestBeanContextCustomizerEqualityTests {
@Test
void contextCustomizerWithSameOverrideInDifferentTestClassesIsEqual() {
assertThat(createContextCustomizer(Case1.class)).isEqualTo(createContextCustomizer(Case2.class));
}
@Test
void contextCustomizerWithDifferentMethodsIsNotEqual() {
assertThat(createContextCustomizer(Case1.class)).isNotEqualTo(createContextCustomizer(Case3.class));
}
@Test
void contextCustomizerWithByNameVsByTypeLookupIsNotEqual() {
assertThat(createContextCustomizer(Case4.class)).isNotEqualTo(createContextCustomizer(Case5.class));
}
private ContextCustomizer createContextCustomizer(Class<?> testClass) {
ContextCustomizer customizer = BeanOverrideContextCustomizerTestUtils.createContextCustomizer(testClass);
assertThat(customizer).isNotNull();
return customizer;
}
interface DescriptionProvider {
static String createDescription() {
return "override";
}
}
static class Case1 implements DescriptionProvider {
@TestBean(methodName = "createDescription")
private String description;
}
static class Case2 implements DescriptionProvider {
@TestBean(methodName = "createDescription")
private String description;
}
static class Case3 implements DescriptionProvider {
@TestBean(methodName = "createDescription")
private String description;
static String createDescription() {
return "another value";
}
}
static class Case4 {
@TestBean
private String description;
private static String description() {
return "overridden";
}
}
static class Case5 {
@TestBean(name = "descriptionBean")
private String description;
private static String description() {
return "overridden";
}
}
}

View File

@ -0,0 +1,109 @@
/*
* Copyright 2002-2024 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.test.context.bean.override.mockito;
import org.junit.jupiter.api.Test;
import org.mockito.Answers;
import org.springframework.test.context.ContextCustomizer;
import org.springframework.test.context.bean.override.BeanOverrideContextCustomizerTestUtils;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests that validate the behavior of {@link MockitoBean} and
* {@link MockitoSpyBean} with the TCF context cache.
*
* @author Stephane Nicoll
*/
class MockitoBeanContextCustomizerEqualityTests {
@Test
void contextCustomizerWithSameMockInDifferentClassIsEqual() {
assertThat(createContextCustomizer(Case1.class)).isEqualTo(createContextCustomizer(Case2.class));
}
@Test
void contextCustomizerWithSameSpyInDifferentClassIsEqual() {
assertThat(createContextCustomizer(Case4.class)).isEqualTo(createContextCustomizer(Case5.class));
}
@Test
void contextCustomizerWithSimilarMockButDifferentAnswersIsNotEqual() {
assertThat(createContextCustomizer(Case1.class)).isNotEqualTo(createContextCustomizer(Case3.class));
}
@Test
void contextCustomizerWithSimilarSpyButDifferentProxyTargetClassFlagIsNotEqual() {
assertThat(createContextCustomizer(Case5.class)).isNotEqualTo(createContextCustomizer(Case6.class));
}
@Test
void contextCustomizerWithMockAndSpyAreNotEqual() {
assertThat(createContextCustomizer(Case1.class)).isNotEqualTo(createContextCustomizer(Case4.class));
}
private ContextCustomizer createContextCustomizer(Class<?> testClass) {
ContextCustomizer customizer = BeanOverrideContextCustomizerTestUtils.createContextCustomizer(testClass);
assertThat(customizer).isNotNull();
return customizer;
}
static class Case1 {
@MockitoBean
private String exampleService;
}
static class Case2 {
@MockitoBean
private String serviceToMock;
}
static class Case3 {
@MockitoBean(answers = Answers.RETURNS_MOCKS)
private String exampleService;
}
static class Case4 {
@MockitoSpyBean
private String exampleService;
}
static class Case5 {
@MockitoSpyBean
private String serviceToMock;
}
static class Case6 {
@MockitoSpyBean(proxyTargetAware = false)
private String serviceToMock;
}
}