简体   繁体   中英

Grails Unit Test Case doesn't initialize Grails Service

Grails Unit Test Case doesn't initialize my Grails Service.

public class ProfileDataServiceTests extends GrailsUnitTestCase{

    ProfileDataService profileDataService;
    void testprocessRawData() {
        profileDataService.processRawData(getCustomerData())
    }

}

Intellij Idea shows profileDataService is auto wired, when i invoke a method on it, i get NullPointerException as profileDataService is null.

Any thoughts ?

If you see this tutorial you will see grailsUnitTestCase are deprecated since grails 2.0.x

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

So you can use this link which provides better way of grails unit testing.

http://grails.github.io/grails-doc/3.0.x/guide/testing.html#unitTestingControllers

kind of this way

@Integration
class NameSpec extends Specification {

  @Autowired
  ExampleService exampleService
}

Hope this helps! Thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM