如何在grails 2.0.3中对服务进行单元测试?

时间:2012-06-15 17:35:59

标签: java grails groovy junit junit4

我在grails 2.0.3中为服务创建了一个单元测试,我想在eclipse中使用junit runner运行此测试。当我右键点击test-class => debug as => junit test我在控制台中收到以下错误消息:

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)

我在Report / unit / myProject中的ReportServiceTest.groovy:

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"
}
}

错误在哪里?预生成的* ControllerTests.groovy在eclipse中与junit runner合作正常! 我正在使用STS: SpringSource工具套件

版本:2.9.2.RELEASE Build Id:201205071000

1 个答案:

答案 0 :(得分:3)

使用Eclipse junit runner启用Grails单元测试的技巧:只需将@Test注释添加到其中一个测试中;

=>它足以让跑步者识别出该类有一套测试,而@TestFor魔术将完成剩下的工作:动态地,所有方法都“成为”eclipse junit runner的测试。