Grails单元测试用例无法初始化我的Grails服务。
public class ProfileDataServiceTests extends GrailsUnitTestCase{
ProfileDataService profileDataService;
void testprocessRawData() {
profileDataService.processRawData(getCustomerData())
}
}
Intellij Idea显示profileDataService是自动连接的,当我在其上调用方法时,由于profileDataService为null,因此会收到NullPointerException。
有什么想法吗 ?
请您参考如下方法:
如果您看到本教程,则将从grails 2.0.x开始不推荐使用grailsUnitTestCase。
Grails 1.3.x and below used the grails.test.GrailsUnitTestCase class hierarchy for testing in a JUnit 3 style. Grails 2.0.x and above deprecates these test harnesses in favour of mixins that can be applied to a range of different kinds of tests (JUnit 3, JUnit 4, Spock etc.) without subclassing
因此,您可以使用此链接提供更好的grails单元测试方法。
http://grails.github.io/grails-doc/3.0.x/guide/testing.html#unitTestingControllers
这种方式
@Integration
class NameSpec extends Specification {
@Autowired
ExampleService exampleService
}
希望这可以帮助!谢谢




