Adding Spock tests to Java project in Intellij
Asked Answered
B

3

7

I have an existing Java project set up in Intellij 12 and am attempting to add some Groovy classes to it. I've started with attempting to add a simple Spock class for testing purposes, but when I right-click on the class it does not give me an option to run it.

I've taken a look at my Intellij configuration and it pulls in groovy correctly. Further, I can write a Groovy script that uses a Groovy class and that runs without problems so it appears that Groovy is wired in. Is there something else I need to configure to specifically run Spock tests?

Bryner answered 21/11, 2013 at 11:47 Comment(3)
Do you have spock on your classpath? Running spock tests should be like running JUnit tests.Cauline
Thanks for commenting. Yep I have spock-core-0.7-groovy-2.0 on the classpath and the spock Specification import resolves so it appears to be in order.Bryner
There isn't anything else do to. Usually this works just fine (I use it all day long). If it doesn't, it's either a glitch in IntelliJ, or perhaps you have multiple Groovy (or Spock) versions on the IDE class path.Maltha
L
7

Easiest way to get spock and all dependencies is add library from maven.

Then, you should place your test in a folder, marked as test folder, if you want to allow batch processing of them.

If you place your script in folder, not marked as test, or source folder, you will be unable to run it.

If you can't see run button, it looks like Idea cannot recognise file as runnable, it isn't under source/test root, or it's extension is invalid.

Latrell answered 21/11, 2013 at 14:47 Comment(6)
An even easier way is to use gradle :-)Casablanca
@Alex In such case, only thing you should do to import spock is clicking New Library -> "from Maven" and type few words in search box. For gradle you need to create a build script and work with it from command line, or from Idea's awful JetGradle. I like gradle, but in such case it's an huge overkill.Latrell
Sorry that I can't use Gradle. Where can I find "New Library"? I'm not seeing it on the menu, or in the help.Consumedly
For other people looking at this answer: 1. I am assured by my team that you don't want to use IntelliJ add library, since that can get your dependencies out of sync with your maven build; 2. The search box shows a million libraries - I found elsewhere that you want 0.7-groovy-2.0Consumedly
@Consumedly Yep, adding a library wia idea is unsutable if you already have maven project. Add spock to maven instead. Latest version is here, and you can find import examples via link. In your case you should add a library in maven, as it's your 'model' build declaration, and idea project is generated from it. OP's question was about project not backed by any build system. If you still have issues, it's better to ask a new specific question. This one is not too general =)Latrell
It's funny, now I'm using Gradle at another company, and I'm getting the same issue.Consumedly
K
4

You can add Spock plugin by:

  1. Downloading the jar
  2. In IDEA File->Settings->Plugins->Install from disk. Choose the jar.
Karrykarst answered 20/1, 2014 at 20:3 Comment(0)
B
2

I was having the same issue and ended up here. I found that I had forgotten to extend spock.lang.Specification. As soon as I did, the Run option showed up.

Just posting in case it helps any other Spock novices like myself.

Blaisdell answered 27/4, 2014 at 22:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.