4

I’m somewhat new to testing (scary, huh), so please forgive me if this is ignorant.

Is spock-spring 0.7-groovy-2.0 compatible with new Spring 3.2 release in light of the changes made to the testing framework?

I’ve looked over both the Spring 3.2 docs under Testing:

As well as Spock docs under News:

But nothing helps me tell whether the new Spring 3.2 Testing framework will still allow a testing context for a Spock Specification to be configured in the manner described for Spring 3.2 Testing ( Spring 3.2 docs section 11.3.4) such that my annotated beans are injectable.

I tried anyway, but have been unsuccessful in loading a testing context, though tests not dependent upon injected beans pass fine.

I can provide details of my @ContextConfiguration attempts (tried both locations= and classes= patterns in Spring 3.2 docs section 11.3.4 referenced above) , if it ought to work, but for now my question is just this: Can a Spock Specification testing context still be configured to work with Spring 3.2?.

If so, any successful examples would be great (not seeing any Spring 3.2 with Spock around).

Thanks.

slava
  • 1,901
  • 6
  • 28
  • 32
Bill LaPrise
  • 808
  • 17
  • 27
  • This answer is pretty old. Hopefully everyone is on newer versions of Spring and Spock. If you need justifications, this might help http://stackoverflow.com/a/43480494/378151 – Snekse Apr 18 '17 at 19:27

2 Answers2

3

As far as I can tell, Spock's Spring integration should work fine with the new testing features in Spring 3.2. Compared to testing Spring-based applications with JUnit, the only required changes are (as always):

  • Remove the @RunWith annotation
  • Put spock-spring on the test class path

Note that you can't use the old approach of extending Spring test base classes. Instead you'll have to use the annotation-based Spring testing approach.

If you find a case where the strategy outlined above doesn't work (and you have the same test working with JUnit), please submit an issue at http://issues.spockframework.org.

Peter Niederwieser
  • 121,412
  • 21
  • 324
  • 259
1

On the inspiration of Peter's answer, I found the testing context does set up fine. Here is what I used and it ran properly:

PersonServiceSpec.groovy:

@ContextConfiguration(locations="classpath*:/PersonServiceSpec-context.xml")
class PersonServiceSpec extends Specification {

    @Autowired
    PersonService personService;

    def username

    def setup() {
        this.username = "tester"
    }

    def "Does search for username pull tester" () {

        expect: "tester" == username;
    }

    def "PersonService exists" () {
        expect: personService != null;
    }

}

with this PersonServiceSpec-context.xml placed on the classpath (src/main/resource for my maven proj):

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<bean id="personSevice" class="<qualifiedname>.services.TPersonServiceImpl" >
</bean>

</beans>

and a stubbed TPersonServiceImpl class that implemented my PersonService interface methods.

Tests passed.

cdeszaq
  • 30,869
  • 25
  • 117
  • 173
Bill LaPrise
  • 808
  • 17
  • 27