whitenexx
whitenexx

Reputation: 1360

How to unit test a service in grails 2.0.3?

I created a unit test for a service in grails 2.0.3 and i want to run this test with junit runner in eclipse. When i right click on the test-class => debug as => junit test i get the following error message in console:

Class not found myProject.ReportServiceTests
java.lang.ClassNotFoundException: myProject.ReportServiceTests
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.loadClass(RemoteTestRunner.java:693)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.loadClasses(RemoteTestRunner.java:429)
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)

My ReportServiceTest.groovy in test/unit/myProject:

package myProject

import grails.test.mixin.*
import grails.test.mixin.support.*
import org.junit.*

/**
* See the API for {@link grails.test.mixin.support.GrailsUnitTestMixin} for usage     instructions
*/

@TestFor(myProject.ReportService)
@Mock(Sale)
class ReportServiceTests {

void setUp() {
    // Setup logic here
}

void tearDown() {
    // Tear down logic here
}

void testSomething() {
    fail "Implement me"
}

void testGetSalesCompleted() {
    def salesCompleted = service.getSalesCompleted()
    if(salesCompleted.count > 0) {
        assertTrue()
    }
    fail "no sales completed"
}
}

Where is the mistake? The pregenerated *ControllerTests.groovy are working fine with junit runner in eclipse! I'm using STS: SpringSource Tool Suite

Version: 2.9.2.RELEASE Build Id: 201205071000

Upvotes: 1

Views: 5211

Answers (1)

doj
doj

Reputation: 31

A tip to enable Grails unit tests working with Eclipse junit runner : just add @Test annotation to one of the tests;

=> it's enough for the runner to identify the class has a suite of tests, and the @TestFor magic will do the rest : dynamically, all methods "become" tests for eclipse junit runner.

Upvotes: 3

Related Questions