1

I'm writing Spock tests for a Spring Boot application and the tests have a YAML config file as a dependency. I'm able to successfully wire in the config file using @RunWith(SpringJUnit4ClassRunner.class), but these are Spock tests, not JUnit tests and things do not behave properly when this annotation is used.

So I skip it. Spock tests run, but the YAML config is not wired up at all. I tried lots of different options but nothing appears to be working. Does anyone else have experience with this?

Here's my test:

package com.company.producer

import com.amazonaws.services.kinesis.AmazonKinesis
import com.comcast.cpp.broker.rasurakinesisproducer.config.YAMLConfig
import org.junit.runner.RunWith
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.ConfigFileApplicationContextInitializer
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.test.context.ActiveProfiles
import org.springframework.test.context.ContextConfiguration
import org.springframework.test.context.TestPropertySource
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner
import org.springframework.test.context.junit4.SpringRunner
//import org.springframework.test.context.junit4.SpringJUnit4ClassRunner
import spock.lang.Specification

//@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = [YAMLConfig.class],
        initializers = ConfigFileApplicationContextInitializer.class)

class ProducerSpec extends Specification {

    @Autowired
    YAMLConfig yamlConfig;


//    @Test
    def "just a config test" () {

        when:
        true

        then:
        assert yamlConfig != null
        assert yamlConfig.sourcePath != null
    }

The Spock test results show that yamlConfig is null. It was never wired up.

Condition not satisfied:

yamlConfig != null
|          |
null       false

    at com.comcast.cpp.broker.rasurakinesisproducer.ProducerSpec.just a config test(ProducerSpec.groovy:128)

Does anyone have experience with this?

Melvin
  • 149
  • 2
  • 11

0 Answers0