Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SpringMockTestExecutionListener initializes @Lazy beans #823

Closed
jiallombardo opened this issue Feb 15, 2018 · 1 comment
Closed

SpringMockTestExecutionListener initializes @Lazy beans #823

jiallombardo opened this issue Feb 15, 2018 · 1 comment

Comments

@jiallombardo
Copy link

Issue description

Beans marked as @lazy are inadvertedly initialized when running spock-spring tests. I suspect the culprit is SpringMockTestExecutionListener:54. @lazy autowiring works fine, however, classes and @beans, annotated with @lazy, are loaded due to the invocation of applicationContext.getBean(...)

How to reproduce

I've uploaded a small project that has this issue reproduced in tests:
https://github.com/jiallombardo/test-spock-spring

When running LazyBeanInitializedSpec, you will notice an "I AM INITIALIZED" message in console log.
When running LazyBeanNotInitializedSpec (which is a regular, non spring spock test), this message won't appear.

Additional Environment information

Java/JDK

java -version 8

Groovy version

groovy -version 2.4.4

Gradle

gradle -version 3.1

Operating System

Windows

IDE

IntelliJ

Build-tool dependencies used

Gradle/Grails

compile("org.springframework.boot:spring-boot-starter-actuator:1.5.10.RELEASE")
compile("org.springframework.boot:spring-boot-starter-aop:1.5.10.RELEASE")
compile("org.springframework.boot:spring-boot-starter-jetty:1.5.10.RELEASE")
compile("org.springframework.boot:spring-boot-starter-web:1.5.10.RELEASE") {
    exclude(group: "org.springframework.boot", module: "spring-boot-starter-tomcat")
}

testCompile("cglib:cglib-nodep:3.2.4")
testCompile("org.codehaus.groovy:groovy-all:2.4.4")
testCompile("org.springframework.boot:spring-boot-starter-test:1.5.10.RELEASE")
testCompile("org.spockframework:spock-core:1.1-groovy-2.4")
testCompile("org.spockframework:spock-spring:1.1-groovy-2.4")
@dkhamitov
Copy link
Contributor

Just a test which is expected to pass as per Spring semantics

@ContextConfiguration(classes = SpringConfig)
class SpringSpockSpec extends Specification {
    @Autowired
    Foo foo

    def "foo#bar shouldn't be initialized"() {
        expect:
        foo.bar == null
    }
}

@Configuration
class SpringConfig {
    @Bean
    Foo foo() {
        new Foo()
    }

    @Lazy
    @Bean
    Object bar(Foo foo) {
        foo.bar = new Object()
        foo.bar
    }
}

class Foo {
    Object bar
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants