grails / grails-mail

The Grails Mail Plugin
https://grails.github.io/grails-mail/
Apache License 2.0
14 stars 25 forks source link

Did i miss something ? #52

Closed PhilippeRT closed 2 years ago

PhilippeRT commented 2 years ago

Hello,

It seems that, In grails 5.0.0 as in grails 4.0.12, the mailService is not injected in service. Did i miss something ? Example :

class TestMailService {

    MailService mailService

    def serviceMethod() {
        mailService.sendMail {
            from 'prt@localhost'
            to 'prt@localhost'
            subject 'TEST'
            text 'contenu'
        }

    }
}

and

class TestMailSpec extends Specification implements ServiceUnitTest<TestMailService> {

    def 'test mail'() {
        expect:
        service.serviceMethod()

    }
}

it gives

Condition failed with Exception:

service.serviceMethod()
|       |
|       java.lang.NullPointerException: Cannot invoke method sendMail() on null object
|           at fr.cda.TestMailService.serviceMethod(TestMailService.groovy:11)
|           at fr.cda.TestMailSpec.test mail(TestMailSpec.groovy:10)
<fr.cda.TestMailService@5d0529d1 mailService=null grails_plugins_mail_SendMail__applicationContext=org.springframework.web.context.support.GenericWebApplicationContext@863f764, started on Thu Oct 28 14:05:51 CEST 2021, parent: application>

Condition failed with Exception:

service.serviceMethod()
|       |
|       java.lang.NullPointerException: Cannot invoke method sendMail() on null object
|           at fr.cda.TestMailService.serviceMethod(TestMailService.groovy:11)
|           at fr.cda.TestMailSpec.test mail(TestMailSpec.groovy:10)
<fr.cda.TestMailService@5d0529d1 mailService=null grails_plugins_mail_SendMail__applicationContext=org.springframework.web.context.support.GenericWebApplicationContext@863f764, started on Thu Oct 28 14:05:51 CEST 2021, parent: application>

    at fr.cda.TestMailSpec.test mail(TestMailSpec.groovy:10)
Caused by: java.lang.NullPointerException: Cannot invoke method sendMail() on null object
    at fr.cda.TestMailService.serviceMethod(TestMailService.groovy:11)
    ... 1 more

I'm using the mailService in a grails 2.1.4 and did not have any issue.

What's wrong with it ?

Thanks

PhilippeRT commented 2 years ago

Sorry, It works in a controller and in a service (grails 5.0.0). So i missed something in test ? Thanks.

PhilippeRT commented 2 years ago

Hello, The thing I missed is that i have to make an Integration Spec and not a standard test. So too cool, it works perfectly. Thanks a lot.