8

I've been playing with JUnit 5 and spring-test-junit5. I then tried to use the nested tests feature, but my tests fail. This also happens when I run directly from the command line using gradle.

@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = TestConfig.class)
@DisplayName("In cat club")
public class NestedCatTests {
    @Autowired
    Cat cat;

    @Autowired
    List<Cat> cats;

    @Test
    @DisplayName("Catbert is the cat")
    void catWasAutowired() {
        assertEquals(
                "Catbert",
                cat.getName()
        );
    }

    @Nested
    @DisplayName("Catbert")
    class IsMemberOfClub {
        @Test
        @DisplayName("is a member")
        void isMemberOf() {
            assertTrue(cats.contains(cat));
        }
    }
}

I get the following exception:

java.lang.IllegalStateException: Failed to load ApplicationContext

at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124)
at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:117)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:230)
at org.springframework.test.context.junit.jupiter.SpringExtension.postProcessTestInstance(SpringExtension.java:91)
at org.junit.jupiter.engine.descriptor.ClassTestDescriptor.lambda$null$1(ClassTestDescriptor.java:196)
at org.junit.jupiter.engine.descriptor.JupiterTestDescriptor.executeAndMaskThrowable(JupiterTestDescriptor.java:102)
...
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

Caused by: java.lang.IllegalStateException: Neither GenericXmlContextLoader nor AnnotationConfigContextLoader was able to load an ApplicationContext from [MergedContextConfiguration@4816278d testClass = NestedCatTests.IsMemberOfClub, locations = '{}', classes = '{}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{}', contextCustomizers = set[[empty]], contextLoader = 'org.springframework.test.context.support.DelegatingSmartContextLoader', parent = [null]].
at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:263)
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:98)
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:116)
... 44 more
Christo
  • 1,802
  • 4
  • 20
  • 31
  • 1
    I've managed to get it to work if I add "@ContextConfiguration(classes = TestConfig.class)" to the nested class. I imagine that this behaviour will be improved? – Christo Feb 24 '17 at 08:16
  • Please open a GitHub issue on https://github.com/sbrannen/spring-test-junit5. At some point, this will become an official Spring project. – Marc Philipp Feb 24 '17 at 14:31
  • @Marc I'll create an issue for this use case. – Christo Feb 25 '17 at 16:56
  • Issue https://github.com/sbrannen/spring-test-junit5/issues/8 created. – Christo Mar 01 '17 at 08:15
  • 1
    Please look at this sample [NestedTestsWithSpringAndJUnitJupiterTestCase](https://github.com/spring-projects/spring-framework/blob/bc14c5ba83e1f211628456bbccce7b2531aac58c/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/nested/NestedTestsWithSpringAndJUnitJupiterTestCase.java) – jumb0jet Mar 06 '17 at 15:50
  • Thanks @ahrystiuk your example is even more succinct given the SpringJunitConfig. I guess I expected the enclosing class configuration to be available to the nested test class unless otherwise provided. – Christo Mar 06 '17 at 19:42

1 Answers1

0

I have a working @Nested testing example using Spring Boot.

@SpringBootTest(webEnvironment = RANDOM_PORT)
@Slf4j
@DisplayName("API endpoints integration tests")
class IntegrationTests {

    @LocalServerPort
    private int port;

    private WebTestClient client;

    @Autowired
    private JwtTokenProvider jwtTokenProvider;

    @BeforeEach
    private void setup() {
        this.client = WebTestClient.bindToServer()
                .baseUrl("http://localhost:" + port)
                .build();
    }

    @Nested
    @DisplayName("if user is not logged in")
    class NotLoggedIn { ... }

    @Nested
    @DisplayName("if user logged as user")
    class LoggedAsUser{ ... }

//...

}
Hantsy
  • 8,006
  • 7
  • 64
  • 109