'how to run GroovyTestCase in Eclipse?

I'm new to both Eclipse and Groovy.

In Eclipse, I set up a new project. I added a new Groovy script that looks like this

println "Hello jerkface!

I can hit the "play" button, it asks me if I want to run as a Groovy Console, Groovy Script, or Java Application. I choose "Groovy Script" and it works. Hooray!

Now I want to create a new test case, so I create a new Groovy test, and I edit it so that it looks like this:

import groovy.util.GroovyTestCase

class DegenerateTestCase extends GroovyTestCase {
    void testAlwaysTrue() {
        assert true
    }
}

If I try to run that file, Eclipse says "Did not find runnable type for Groovy Shell in selection". I'm not quite sure what that means. Is it because there is no main() method?

The funny thing is, if I try to run that test file from the command line using groovy or groovysh, it works just fine.

Any ideas what I have to do to run Groovy test scripts within Eclipse?

P.S. I have installed the Groovy Development Tools 4.5.0 into Eclipse.



Solution 1:[1]

So it seems that Eclipse is trying (and failing!) to be helpful, and complaining that this file won't actually do anything. So either of these solutions will get rid of the problem

  1. Add a static void main(args) {} to the test class. Then you can run the class (it won't do anything...but at least it will not give you that error.
  2. Add a command outside the class definition (e.g. println "hello world"). That's avoiding the pop-up error for me, but leading to another complaint about JUnit not being found...but that's a separate issue.

UPDATE: I think I'm getting closer.

As noted in the comments, adding either the static void main() definition or adding something outside the class definition will get rid of the "Did not find runnable type" message, but will not cause the tests to run.

I think I see two things going on. (But I'm still new at this and might be mistaken.)

  1. When using the "new file" wizard in Eclipse, Eclipse remembers the type of file that you picked (Groovy Type or Groovy Test). That influences how Eclipse will run the file.
  2. It seems like using the run buttons in the toolbar is a better choice than right-clicking the file name in the package explorer and choosing "run as". Those buttons seem to use the information from the file creation step to run things correctly. (That seems to be called a Launch Configuration, and I'll have to figure out what that does.)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1