|
||||||||||
|
||||||||||
GroovyUsing Groovy
Language GuideGroovy FeaturesModulesExamplesUseful LinksIDE SupportSupportCommunityDevelopersTools we use
Feeds
|
The Groovy AdvantageGroovy simplifies JUnit testing, making it more Groovy, in 3 ways.
See the following article Unit test your java code with groovy ExampleTo write unit tests in groovy, you have to create a class extending groovy.util.GroovyTestCase. import groovy.util.GroovyTestCase class MyTest extends GroovyTestCase { void testSomething() { assert 1 == 1 } } Groovy Test AssertionsApart from the default assertions already available from the JUnit framework, GroovyTestCase also offers additionnal test assertions:
DetailsBy default Groovy unit test cases generate java bytecode and so are just the same as any other Java unit test cases. One thing to watch is often Ant / Maven look for *.java files to find unit tests with pattern matching, rather than *.class files.
maven.test.search.classdir = true Once you've got this enabled you can use Maven goals to run individual test cases like this
maven test:single -Dtestcase=foo.MyGroovyTest Running GroovyTestCases on the command-lineSince beta-6, you can also run your groovy tests (extending GroovyTestCase) on the command-line. It has simple as launching any other Groovy script or class:
groovy MyTest.groovy Running GroovyTestCases in IDEsMost IDEs support JUnit but maybe don't yet handle Groovy shame! Firstly if you compile the groovy code to bytecode, then it'll just work in any JUnit IDE just fine. Sometimes though you want to just hack the unit test script and run from in your IDE without doing a build. The GroovyTestSuite class is a JUnit TestSuite which will compile and run a GroovyUnit test case from a command line argument (when run as an application) or from the test system property when run as a JUnit test suite. To run the GroovyUnitTest as an application, just do the equivalent of this in your IDE
java groovy.util.GroovyTestSuite src/test/Foo.groovy Or to run the test suite inside your IDE, just run the GroovyTestSuite test with this system property defined
-Dtest=src/test/Foo.groovy Either of the above can really help improve the development experience of writing Groovy unit test cases in IDEs that don't yet support Groovy natively. Using normal scripts as unit test casesYou can write scripts like this
x = [1, 2, 3]
assert x.size() == 3 and use these scripts as unit test cases if you use the GroovyTestSuite class to run them as described above. When the above script is compiled, it doesn't actually implement JUnit's TestCase and so needs a special runner so that it can be used inside a JUnit test framework. This is what GroovyTestSuite does, it detects scripts like the above and wraps them in a JUnit Test adapter so you can run scripts like the above as a unit test case inside your IDE |
|||||||||
|