Last active
March 25, 2019 08:28
-
-
Save alainsahli/a3d8fd26adc527aa422c to your computer and use it in GitHub Desktop.
spring-test issue when using multiple @ContextConfiguration defined on a base class and on a composed annotation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.junit.runner.RunWith; | |
import org.springframework.test.context.ContextConfiguration; | |
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | |
@ContextConfiguration(classes = AnotherAppContext.class) | |
@RunWith(SpringJUnit4ClassRunner.class) | |
public abstract class AbstractBaseTest { | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
@Configuration | |
public class AnotherAppContext { | |
@Bean | |
public AnotherBean anotherBean() { | |
return new AnotherBean("Bar"); | |
} | |
static class AnotherBean { | |
private final String value; | |
AnotherBean(String value) { | |
this.value = value; | |
} | |
public String value() { | |
return this.value; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.lang.annotation.Retention; | |
import java.lang.annotation.Target; | |
import org.springframework.test.context.ContextConfiguration; | |
import static java.lang.annotation.ElementType.TYPE; | |
import static java.lang.annotation.RetentionPolicy.RUNTIME; | |
@Target(TYPE) | |
@Retention(RUNTIME) | |
@ContextConfiguration(classes = TestAppContext.class) | |
public @interface ComposedAnnotation { | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.junit.Test; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import static org.junit.Assert.assertEquals; | |
import static org.junit.Assert.assertNotNull; | |
@ComposedAnnotation | |
public class ComposedAnnotationTest extends AbstractBaseTest { | |
@Autowired | |
private TestAppContext.MyBean myBean; | |
/** | |
* This test fails | |
*/ | |
@Test | |
public void myBeanMustNotBeNull() throws Exception { | |
assertNotNull(this.myBean); | |
assertEquals("Foo", this.myBean.value()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
@Configuration | |
public class TestAppContext { | |
@Bean | |
public MyBean myBean() { | |
return new MyBean("Foo"); | |
} | |
static class MyBean { | |
private final String value; | |
MyBean(String value) { | |
this.value = value; | |
} | |
public String value() { | |
return this.value; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.junit.Test; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import static org.junit.Assert.assertEquals; | |
import static org.junit.Assert.assertNotNull; | |
@ContextConfiguration(classes = TestAppContext.class) | |
public class WorkingTest extends AbstractBaseTest { | |
@Autowired | |
private TestAppContext.MyBean myBean; | |
/** | |
* This test passes | |
*/ | |
@Test | |
public void myBeanMustNotBeNull() throws Exception { | |
assertNotNull(this.myBean); | |
assertEquals("Foo", this.myBean.value()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment