0

I have a springBootVersion = '2.3.2.RELEASE' app and I would like to use Spock for my unit and integration tests. I have this for Spock in my Gradle:

  testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'junit', module: 'junit' //by both name and group
    }

  testImplementation group: 'org.spockframework', name: 'spock-core', version: '1.2-groovy-2.4'
 testImplementation group: 'org.spockframework', name: 'spock-core', version: '1.2-groovy-2.4'
 implementation group: 'org.codehaus.gmavenplus', name: 'gmavenplus-plugin', version: '1.6.3'

And my first test is pretty simple:

@SpringBootTest(classes = PrototypeApplication.class)
class PrototypeApplicationSpec extends Specification {

    @Autowired
    ApplicationContext context

    def "test context loads"() {
        expect:
        context != null
    }
}

But when I run it, the ApplicationContext is null:

> Task :compileJava UP-TO-DATE
> Task :compileGroovy NO-SOURCE
> Task :processResources UP-TO-DATE
> Task :classes UP-TO-DATE
> Task :compileTestJava UP-TO-DATE
> Task :compileTestGroovy
> Task :processTestResources UP-TO-DATE
> Task :testClasses
> Task :test FAILED

Condition not satisfied:

> context != null |       | null    false
> 
> Condition not satisfied:
> 
> context != null |       | null    false

It's been awhile since I used Spock, so I am not 100% that my libraries are correct but it does try to run, just can't seem to load the context.

sonoerin
  • 5,015
  • 23
  • 75
  • 132

1 Answers1

2

You need to include spock-spring in your project.

Replace on of the spock-core duplicates with spock-spring inside your build.gradle:

testImplementation group: 'org.spockframework', name: 'spock-core', version: '1.2-groovy-2.4'
testImplementation group: 'org.spockframework', name: 'spock-spring', version: '1.2-groovy-2.4'
rieckpil
  • 10,470
  • 3
  • 32
  • 56