Skip to content

SpringBootTest does not work with Java 10/modules #13581

Closed

Description

@keirlawson

I attempted to port the Spring web testing example at https://github.com/spring-guides/gs-testing-web to Java 10 and modules, however I find when running my tests via Maven that the @SpringBootTest annotation is no longer able to locate my application class despite the code working previously before modularisation.I receive the following error when running mvn test

java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test

Adding the application class explicitly as this message suggests then results in

java.lang.reflect.InaccessibleObjectException: Unable to make field private static java.lang.Object hello.Application$$EnhancerBySpringCGLIB$$d7dfca41.CGLIB$CALLBACK_FILTER accessible: module gs.testing.web does not "opens hello" to unnamed module @2fc6f97f

The changes I made to the initial repo can be viewed here: spring-guides/gs-testing-web@master...keirlawson:j10_modules

Activity

  1. added
    status: waiting-for-triage
    on Jun 26, 2018
  2. wilkinsona commented on Jun 26, 2018

    @wilkinsona
    Member

    Thanks for the report.

    Being unable to find @SpringBootConfiguration is indicative of classpath scanning not working correctly. Specifying the application class explicitly means that scanning isn't used.

    The second problem is trying to point out that your module-info is incorrect and that you need to add opens hello to it:

    module gs.testing.web {
        requires spring.web;
        requires spring.boot;
        requires spring.boot.autoconfigure;
        requires spring.context;
        opens hello;
    }
    

    With that change in place, SmokeTest fails as the controller cannot be injected:

    org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'hello.SmokeTest': Unsatisfied dependency expressed through field 'controller'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'hello.HomeController' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:587) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    	at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:91) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:373) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1350) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:401) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    	at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:118) ~[spring-test-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    	at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83) ~[spring-test-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    	at org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener.prepareTestInstance(SpringBootDependencyInjectionTestExecutionListener.java:44) ~[spring-boot-test-autoconfigure-2.0.3.RELEASE.jar:2.0.3.RELEASE]
    	at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:246) ~[spring-test-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:227) [spring-test-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:289) [spring-test-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) [junit-4.12.jar:4.12]
    	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291) [spring-test-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:246) [spring-test-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97) [spring-test-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) [junit-4.12.jar:4.12]
    	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) [junit-4.12.jar:4.12]
    	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) [junit-4.12.jar:4.12]
    	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) [junit-4.12.jar:4.12]
    	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) [junit-4.12.jar:4.12]
    	at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) [spring-test-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    	at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) [spring-test-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    	at org.junit.runners.ParentRunner.run(ParentRunner.java:363) [junit-4.12.jar:4.12]
    	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190) [spring-test-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    	at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:365) [surefire-junit4-2.22.0.jar:2.22.0]
    	at org.apache.maven.surefire.junit4.JUnit4Provider.executeWithRerun(JUnit4Provider.java:273) [surefire-junit4-2.22.0.jar:2.22.0]
    	at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:238) [surefire-junit4-2.22.0.jar:2.22.0]
    	at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:159) [surefire-junit4-2.22.0.jar:2.22.0]
    	at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:383) [surefire-booter-2.22.0.jar:2.22.0]
    	at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:344) [surefire-booter-2.22.0.jar:2.22.0]
    	at org.apache.maven.surefire.booter.ForkedBooter.execute(ForkedBooter.java:125) [surefire-booter-2.22.0.jar:2.22.0]
    	at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:417) [surefire-booter-2.22.0.jar:2.22.0]
    Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'hello.HomeController' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1509) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1065) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:584) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    	... 31 common frames omitted
    

    As with the first problem, this is indicative of classpath scanning not working correctly. This can be confirmed by adding another test class:

    package hello;
    
    import java.util.Arrays;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
    
    public class ScanningTest {
    
        @Test
        public void scanningTest() throws Exception {
            PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
            System.out.println(Arrays.toString(resolver.getResources("classpath*:hello/**/*.class")));
        }
    
    }

    It produces the following output:

    [file [/Users/awilkinson/dev/temp/gs-testing-web/complete/target/test-classes/hello/ApplicationTest.class], file [/Users/awilkinson/dev/temp/gs-testing-web/complete/target/test-classes/hello/HttpRequestTest.class], file [/Users/awilkinson/dev/temp/gs-testing-web/complete/target/test-classes/hello/ScanningTest.class], file [/Users/awilkinson/dev/temp/gs-testing-web/complete/target/test-classes/hello/SmokeTest.class], file [/Users/awilkinson/dev/temp/gs-testing-web/complete/target/test-classes/hello/WebLayerTest.class], file [/Users/awilkinson/dev/temp/gs-testing-web/complete/target/test-classes/hello/WebMockTest.class]]
    

    Note that the only classes that have been found are in target/test-classes and those in target/classes have been missed.

    The args that Surefire uses to launch the forked JVM are written to target/surefire. This file is usually deleted on exit but it preserved when running with -X. It has contents similar to the following:

    --module-path
    /Users/awilkinson/dev/temp/gs-testing-web/complete/target/classes:/Users/awilkinson/.m2/repository/org/springframework/boot/spring-boot/2.0.3.RELEASE/spring-boot-2.0.3.RELEASE.jar:/Users/awilkinson/.m2/repository/org/springframework/boot/spring-boot-autoconfigure/2.0.3.RELEASE/spring-boot-autoconfigure-2.0.3.RELEASE.jar:/Users/awilkinson/.m2/repository/org/springframework/spring-web/5.0.7.RELEASE/spring-web-5.0.7.RELEASE.jar:/Users/awilkinson/.m2/repository/org/springframework/spring-context/5.0.7.RELEASE/spring-context-5.0.7.RELEASE.jar
    --class-path
    /Users/awilkinson/.m2/repository/org/apache/maven/surefire/surefire-booter/2.22.0/surefire-booter-2.22.0.jar:/Users/awilkinson/.m2/repository/org/apache/maven/surefire/surefire-api/2.22.0/surefire-api-2.22.0.jar:/Users/awilkinson/.m2/repository/org/apache/maven/surefire/surefire-logger-api/2.22.0/surefire-logger-api-2.22.0.jar:/Users/awilkinson/dev/temp/gs-testing-web/complete/target/test-classes:/Users/awilkinson/.m2/repository/org/springframework/boot/spring-boot-starter-web/2.0.3.RELEASE/spring-boot-starter-web-2.0.3.RELEASE.jar:/Users/awilkinson/.m2/repository/org/springframework/boot/spring-boot-starter/2.0.3.RELEASE/spring-boot-starter-2.0.3.RELEASE.jar:/Users/awilkinson/.m2/repository/org/springframework/boot/spring-boot-starter-logging/2.0.3.RELEASE/spring-boot-starter-logging-2.0.3.RELEASE.jar:/Users/awilkinson/.m2/repository/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar:/Users/awilkinson/.m2/repository/ch/qos/logback/logback-core/1.2.3/logback-core-1.2.3.jar:/Users/awilkinson/.m2/repository/org/apache/logging/log4j/log4j-to-slf4j/2.10.0/log4j-to-slf4j-2.10.0.jar:/Users/awilkinson/.m2/repository/org/apache/logging/log4j/log4j-api/2.10.0/log4j-api-2.10.0.jar:/Users/awilkinson/.m2/repository/org/slf4j/jul-to-slf4j/1.7.25/jul-to-slf4j-1.7.25.jar:/Users/awilkinson/.m2/repository/javax/annotation/javax.annotation-api/1.3.2/javax.annotation-api-1.3.2.jar:/Users/awilkinson/.m2/repository/org/yaml/snakeyaml/1.19/snakeyaml-1.19.jar:/Users/awilkinson/.m2/repository/org/springframework/boot/spring-boot-starter-json/2.0.3.RELEASE/spring-boot-starter-json-2.0.3.RELEASE.jar:/Users/awilkinson/.m2/repository/com/fasterxml/jackson/core/jackson-databind/2.9.6/jackson-databind-2.9.6.jar:/Users/awilkinson/.m2/repository/com/fasterxml/jackson/core/jackson-annotations/2.9.0/jackson-annotations-2.9.0.jar:/Users/awilkinson/.m2/repository/com/fasterxml/jackson/core/jackson-core/2.9.6/jackson-core-2.9.6.jar:/Users/awilkinson/.m2/repository/com/fasterxml/jackson/datatype/jackson-datatype-jdk8/2.9.6/jackson-datatype-jdk8-2.9.6.jar:/Users/awilkinson/.m2/repository/com/fasterxml/jackson/datatype/jackson-datatype-jsr310/2.9.6/jackson-datatype-jsr310-2.9.6.jar:/Users/awilkinson/.m2/repository/com/fasterxml/jackson/module/jackson-module-parameter-names/2.9.6/jackson-module-parameter-names-2.9.6.jar:/Users/awilkinson/.m2/repository/org/springframework/boot/spring-boot-starter-tomcat/2.0.3.RELEASE/spring-boot-starter-tomcat-2.0.3.RELEASE.jar:/Users/awilkinson/.m2/repository/org/apache/tomcat/embed/tomcat-embed-core/8.5.31/tomcat-embed-core-8.5.31.jar:/Users/awilkinson/.m2/repository/org/apache/tomcat/embed/tomcat-embed-el/8.5.31/tomcat-embed-el-8.5.31.jar:/Users/awilkinson/.m2/repository/org/apache/tomcat/embed/tomcat-embed-websocket/8.5.31/tomcat-embed-websocket-8.5.31.jar:/Users/awilkinson/.m2/repository/org/hibernate/validator/hibernate-validator/6.0.10.Final/hibernate-validator-6.0.10.Final.jar:/Users/awilkinson/.m2/repository/javax/validation/validation-api/2.0.1.Final/validation-api-2.0.1.Final.jar:/Users/awilkinson/.m2/repository/org/jboss/logging/jboss-logging/3.3.2.Final/jboss-logging-3.3.2.Final.jar:/Users/awilkinson/.m2/repository/com/fasterxml/classmate/1.3.4/classmate-1.3.4.jar:/Users/awilkinson/.m2/repository/org/springframework/spring-beans/5.0.7.RELEASE/spring-beans-5.0.7.RELEASE.jar:/Users/awilkinson/.m2/repository/org/springframework/spring-webmvc/5.0.7.RELEASE/spring-webmvc-5.0.7.RELEASE.jar:/Users/awilkinson/.m2/repository/org/springframework/spring-aop/5.0.7.RELEASE/spring-aop-5.0.7.RELEASE.jar:/Users/awilkinson/.m2/repository/org/springframework/spring-expression/5.0.7.RELEASE/spring-expression-5.0.7.RELEASE.jar:/Users/awilkinson/.m2/repository/org/springframework/boot/spring-boot-starter-test/2.0.3.RELEASE/spring-boot-starter-test-2.0.3.RELEASE.jar:/Users/awilkinson/.m2/repository/org/springframework/boot/spring-boot-test/2.0.3.RELEASE/spring-boot-test-2.0.3.RELEASE.jar:/Users/awilkinson/.m2/repository/org/springframework/boot/spring-boot-test-autoconfigure/2.0.3.RELEASE/spring-boot-test-autoconfigure-2.0.3.RELEASE.jar:/Users/awilkinson/.m2/repository/com/jayway/jsonpath/json-path/2.4.0/json-path-2.4.0.jar:/Users/awilkinson/.m2/repository/net/minidev/json-smart/2.3/json-smart-2.3.jar:/Users/awilkinson/.m2/repository/net/minidev/accessors-smart/1.2/accessors-smart-1.2.jar:/Users/awilkinson/.m2/repository/org/ow2/asm/asm/5.0.4/asm-5.0.4.jar:/Users/awilkinson/.m2/repository/org/slf4j/slf4j-api/1.7.25/slf4j-api-1.7.25.jar:/Users/awilkinson/.m2/repository/junit/junit/4.12/junit-4.12.jar:/Users/awilkinson/.m2/repository/org/assertj/assertj-core/3.9.1/assertj-core-3.9.1.jar:/Users/awilkinson/.m2/repository/org/mockito/mockito-core/2.15.0/mockito-core-2.15.0.jar:/Users/awilkinson/.m2/repository/net/bytebuddy/byte-buddy/1.7.11/byte-buddy-1.7.11.jar:/Users/awilkinson/.m2/repository/net/bytebuddy/byte-buddy-agent/1.7.11/byte-buddy-agent-1.7.11.jar:/Users/awilkinson/.m2/repository/org/objenesis/objenesis/2.6/objenesis-2.6.jar:/Users/awilkinson/.m2/repository/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar:/Users/awilkinson/.m2/repository/org/hamcrest/hamcrest-library/1.3/hamcrest-library-1.3.jar:/Users/awilkinson/.m2/repository/org/skyscreamer/jsonassert/1.5.0/jsonassert-1.5.0.jar:/Users/awilkinson/.m2/repository/com/vaadin/external/google/android-json/0.0.20131108.vaadin1/android-json-0.0.20131108.vaadin1.jar:/Users/awilkinson/.m2/repository/org/springframework/spring-core/5.0.7.RELEASE/spring-core-5.0.7.RELEASE.jar:/Users/awilkinson/.m2/repository/org/springframework/spring-jcl/5.0.7.RELEASE/spring-jcl-5.0.7.RELEASE.jar:/Users/awilkinson/.m2/repository/org/springframework/spring-test/5.0.7.RELEASE/spring-test-5.0.7.RELEASE.jar:/Users/awilkinson/.m2/repository/org/xmlunit/xmlunit-core/2.5.1/xmlunit-core-2.5.1.jar:/Users/awilkinson/.m2/repository/org/apache/maven/surefire/surefire-junit4/2.22.0/surefire-junit4-2.22.0.jar
    --patch-module
    gs.testing.web=/Users/awilkinson/dev/temp/gs-testing-web/complete/target/test-classes
    --add-exports
    gs.testing.web/hello=ALL-UNNAMED
    --add-modules
    gs.testing.web
    --add-reads
    gs.testing.web=ALL-UNNAMED
    org.apache.maven.surefire.booter.ForkedBooter
    

    Note that target/classes is on the module path and thattarget/test-classes has been patched into gs.testing.web. As far as I can tell, Surefire has set things up properly which would suggest that this is a Spring Framework problem. I've opened https://jira.spring.io/browse/SPR-16977.

  3. added
    status: invalid
    for: external-project
    and removed
    status: waiting-for-triage
    on Jun 26, 2018
  4. keirlawson commented on Jun 26, 2018

    @keirlawson
    Author

    Thanks for getting back. I'm not clear why I should need to add opens hello for this test though, as the tests are in the same package they should not be crossing a module boundary?

  5. wilkinsona commented on Jun 26, 2018

    @wilkinsona
    Member

    The tests may be in the same module but Spring Framework is not. From Spring Framework's reference documentation:

    On JDK 9’s module path (Jigsaw), Spring’s classpath scanning generally works as expected. However, please make sure that your component classes are exported in your module-info descriptors; if you expect Spring to invoke non-public members of your classes, make sure that they are 'opened' (i.e. using an opens declaration instead of an exports declaration in your module-info descriptor).

  6. keirlawson commented on Jun 27, 2018

    @keirlawson
    Author

    Ok, that bit of documentation seems to imply that I need to export but not necessarily open said package then as so far as I can see there are no non-public members in use, yet when I just export it I get the same InaccessibleObjectException? Am I missing something here?

  7. wilkinsona commented on Jun 28, 2018

    @wilkinsona
    Member

    Am I missing something here?

    Yes, Spring Framework's use of CGLib to proxy your Application @Configuration class. CGLib adds non-public members which need to be accessible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    for: external-projectstatus: invalid

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions