Skip to content

Commit

Permalink
Fix package tangle in bean override tests
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
snicoll committed Jun 10, 2024
1 parent 96302a7 commit b9a6ba1
Show file tree
Hide file tree
Showing 4 changed files with 263 additions and 168 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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());
}

}
Original file line number Diff line number Diff line change
@@ -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";
}
}

}
Loading

0 comments on commit b9a6ba1

Please sign in to comment.