Re: Cannot get Spock to load applicationContext using @ContextConfiguration

2,985 views
Skip to first unread message

Peter Niederwieser

unread,
Dec 27, 2012, 1:59:19 AM12/27/12
to spockfr...@googlegroups.com
Can you double-check that you have spock-spring on the class path?

Cheers,
Peter

On Thu, Dec 27, 2012 at 5:36 AM, springster <wol...@gmail.com> wrote:
I am new to Spock and attempting to load my applicationContext and other dependency's when firing off my test using eclipse/STS. I have done this before using @ContextConfiguration with plan java and junit but when I try what I believe should be working with Spock nothing loads. 

Below is my configuration, when I uncomment the @RunWith(SpringJUnit4ClassRunner.class) line and create a method that has a @Test annotoation the context loads and everything "works"... but at this point I am not using Spock. 

//@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations = [ "file:/Users/me/project-new/www/WEB-INF/applicationContext-hibernate.xml", "classpath:/reportsContext.xml", "classpath:/dataSourceContext-local.xml"])
class HelloSpock extends spock.lang.Specification {
    @Autowired
WidgetManager widgetManager; 
def "length of Spock's and his friends' names"() {
        expect:
        name.size() == length

        where:
        name     | length
        "Spock"  | 5
        "Kirk"   | 4
        "Scotty" | 6
    }

(I didn't see any [CODE] tag to use for the above)

The test completes immediately and nothing from my context loads. 

Thanks in advanced. 

Keith

--
You received this message because you are subscribed to the Google Groups "Spock Framework - User" group.
To view this discussion on the web visit https://groups.google.com/d/msg/spockframework/-/j-57xXQB0EoJ.
To post to this group, send email to spockfr...@googlegroups.com.
To unsubscribe from this group, send email to spockframewor...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/spockframework?hl=en.

K Grebnellow

unread,
Dec 27, 2012, 9:52:18 AM12/27/12
to spockfr...@googlegroups.com
I have spock-core-0.7-groovy-1.8.jar on my build path in eclipse/sts and I am able to run the  "def "length of Spock's and his friends' names"()" test however everything that should be loaded by my applicationContext comes back null. 

Keith 

Peter Niederwieser

unread,
Dec 27, 2012, 5:28:22 PM12/27/12
to spockfr...@googlegroups.com
In order for the Spring TestContext framework integration to work, you additionally have to put spock-spring on the class path. Otherwise, @ContextConfiguration will have no effect whatsoever.

Cheers,
Peter

K Grebnellow

unread,
Jan 7, 2013, 2:31:02 PM1/7/13
to spockfr...@googlegroups.com
Thanks for the reply. 

I am running these tests through eclipse/sts therefore I went to Run>Run Configurations>JUnit (selected my test)>Class Path Tab> and added the spock-core-0.7-groovy-1.8 and groovy-all-1.8.4 jars ( I was getting an error without having the groovy all jar). I added these to the Bootstrap Entities. After doing so and hitting run I get the following error: 

java.lang.Exception: No runnable methods
at org.junit.runners.BlockJUnit4ClassRunner.validateInstanceMethods(BlockJUnit4ClassRunner.java:166)
at org.junit.runners.BlockJUnit4ClassRunner.collectInitializationErrors(BlockJUnit4ClassRunner.java:102)
at org.junit.runners.ParentRunner.validate(ParentRunner.java:344)
at org.junit.runners.ParentRunner.<init>(ParentRunner.java:74)
.....

Perhaps I am missing the boat on this one. Let me know you thoughts and thanks for your patience. 


Keith 

Peter Niederwieser

unread,
Jan 7, 2013, 3:24:59 PM1/7/13
to spockfr...@googlegroups.com
Make sure to REMOVE `@RunWith(SpringJUnit4ClassRunner.class)` and any `@Test` annotation, and make sure to ADD the spock-spring Jar. Without more information, that's all I can say.

Cheers,
Peter

springster

unread,
Mar 5, 2013, 12:11:34 PM3/5/13
to spockfr...@googlegroups.com
I have removed any reference to @RunWith... and @Test and I am still getting the same error (see below). I have added (as stated before) the suggested jars via Run>Run Configurations>JUnit (selected my test)>Class Path Tab> and added the spock-core-0.7-groovy-1.8 and groovy-all-1.8.4 jars. What other information could I provide you with?

Here is my updated Spec. 
@ContextConfiguration(locations = "appcontext.xml")
class TestSpec extends Specification {
@Autowired
private Manager manager;
  
def "length of Spock's and his friends' names"() {
println(manager)
}
}


java.lang.Exception: No runnable methods
at org.junit.runners.BlockJUnit4ClassRunner.validateInstanceMethods(BlockJUnit4ClassRunner.java:166)
at org.junit.runners.BlockJUnit4ClassRunner.collectInitializationErrors(BlockJUnit4ClassRunner.java:102)
at org.junit.runners.ParentRunner.validate(ParentRunner.java:344)
at org.junit.runners.ParentRunner.<init>(ParentRunner.java:74)
at org.junit.runners.BlockJUnit4ClassRunner.<init>(BlockJUnit4ClassRunner.java:55)
at org.junit.internal.builders.JUnit4Builder.runnerForClass(JUnit4Builder.java:13)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:29)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:24)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.<init>(JUnit4TestReference.java:33)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestClassReference.<init>(JUnit4TestClassReference.java:25)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:48)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:452)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

Peter Niederwieser

unread,
Mar 5, 2013, 12:18:13 PM3/5/13
to spockfr...@googlegroups.com
This stack trace indicates a general problem of Eclipse not recognizing Spock specifications. More precisely, Eclipse doesn't find the @RunWith annotation on the spock.lang.Specification base class (which it should). There is a known bug in Eclipse 3.5 and earlier that requires you to add the sources Jar for the Spock Jar in order to be able to run Spock specs. Maybe that's what you are running into. 

Cheers,
Peter

To unsubscribe from this group and stop receiving emails from it, send an email to spockframewor...@googlegroups.com.

To post to this group, send email to spockfr...@googlegroups.com.

Tom Litton

unread,
Mar 5, 2013, 12:27:44 PM3/5/13
to spockfr...@googlegroups.com
Don't you need at least one block in the feature method?


def "length of Spock's and his friends' names"() {
println(manager)
                expect:
                true
}


To unsubscribe from this group and stop receiving emails from it, send an email to spockframewor...@googlegroups.com.
To post to this group, send email to spockfr...@googlegroups.com.

Peter Niederwieser

unread,
Mar 5, 2013, 12:30:48 PM3/5/13
to spockfr...@googlegroups.com
Good point. Although I'm not sure if it will solve the "wrong runner" problem.

Cheers,
Peter

Justin Calleja

unread,
Oct 21, 2013, 4:08:54 PM10/21/13
to spockfr...@googlegroups.com
Thanks for mentioning the spock-spring dependency Peter... I know the documentation mentions this but I think I was adding dependencies as I go along and hadn't added that since everything was working for what I was doing at the time... completely forgot by the time i got to spring injection in my specs... just spent 2 hours trying to find out what the problem was....

btw, haven't searched the spock docs for this, but ended up needing it. If ure reading this, and u know whether it's missing in your doc or not, u can decide whether to put it or not:

To run Specifications with name e.g. SomethingSpec in eclipse add:

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.16</version>
                <configuration>
                    <includes>
                        <include>%regex[.*Spec.*]</include>
                        <include>%regex[.*Test.*]</include>
                        <include>%regex[.*TestCase.*]</include>
                    </includes>
                </configuration>
 </plugin>

in build -> plugins. (that also took some of my time to find... maybe somebody else can benefit).

cheers,
Justin
Reply all
Reply to author
Forward
0 new messages