java - Spring Mail Generating Null Pointer exception -
i'm trying configure mail service in spring , maintain getting null pointer exception. suspicion can't find "mailsender" bean way off. configuration follows:
web.xml
<!-- processes application requests --> <servlet> <servlet-name>appservlet</servlet-name> <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class> <init-param> <param-name>logverbositylevel</param-name> <param-value>error</param-value> </init-param> <init-param> <param-name>contextconfiglocation</param-name> <param-value>/web-inf/spring/appservlet/servlet-context.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>
servlet-context.xml
<?xml version="1.0" encoding="utf-8"?> <beans:beans xmlns="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xsi:schemalocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <beans:bean id="mailsender" class="org.springframework.mail.javamail.javamailsenderimpl"> <beans:property name="host" value="mail.company.com"/> </beans:bean> </beans:beans>
sendmailtest.java
package company.test; import static org.mockito.mockito.when; import static org.mockito.mockitoannotations.initmocks; import java.util.hashmap; import java.util.map; import java.util.properties; import javax.activation.commandmap; import javax.activation.mailcapcommandmap; import javax.mail.message; import javax.mail.messagingexception; import javax.mail.passwordauthentication; import javax.mail.session; import javax.mail.transport; import javax.mail.internet.internetaddress; import javax.mail.internet.mimemessage; import javax.servlet.servletcontext; import javax.servlet.servletcontextevent; import javax.servlet.servletcontextlistener; import org.junit.before; import org.junit.test; import org.mockito.mock; import org.mockito.mockito; import org.mockito.invocation.invocationonmock; import org.mockito.stubbing.answer; import org.slf4j.logger; import org.springframework.beans.factory.annotation.autowired; import org.springframework.mail.mailsender; import org.springframework.mail.simplemailmessage; import org.springframework.mock.web.mockservletcontext; import company.applistener; public class sendmailtest { private static final logger logger = applistener.getlogger(); @mock private servletcontextevent mockservletcontextevent; private final map<string, object> map = new hashmap<string, object>(); @mock private servletcontext mockservletcontext; @before public void setup() { system.setproperty("java.net.preferipv4stack" , "true"); initmocks(this); map.clear(); when(mockservletcontextevent.getservletcontext()).thenreturn(mockservletcontext); mockito.doanswer(new answer<object>() { public object answer(final invocationonmock invocation) throws throwable { final string key = (string) invocation.getarguments()[0]; final object value = invocation.getarguments()[1]; homecoming map.put(key, value); } }).when(mockservletcontext).setattribute(mockito.anystring(), mockito.anyobject()); mockito.doanswer(new answer<object>() { public object answer(final invocationonmock invocation) throws throwable { final object value = invocation.getarguments()[0]; homecoming map.remove(value); } }).when(mockservletcontext).removeattribute(mockito.anystring()); } @autowired private mailsender mailsender; @test public void testsendsimpleemaill() { simplemailmessage message = new simplemailmessage(); string name = "john doe"; message.setfrom("john.doe@company.com"); message.setto("jane.doe@company2.com"); message.setsubject("test email spring"); message.settext("will email ever work?"); seek { mailsender.send(message); } grab (exception e) { e.printstacktrace(); system.err.println(e.getmessage()); } }
}
stack trace
java.lang.nullpointerexception @ company.test.sendmailtest.testsendsimpleemaill(sendmailtest.java:85) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:57) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) @ java.lang.reflect.method.invoke(method.java:606) @ org.junit.runners.model.frameworkmethod$1.runreflectivecall(frameworkmethod.java:44) @ org.junit.internal.runners.model.reflectivecallable.run(reflectivecallable.java:15) @ org.junit.runners.model.frameworkmethod.invokeexplosively(frameworkmethod.java:41) @ org.junit.internal.runners.statements.invokemethod.evaluate(invokemethod.java:20) @ org.junit.internal.runners.statements.runbefores.evaluate(runbefores.java:28) @ org.junit.runners.blockjunit4classrunner.runchild(blockjunit4classrunner.java:76) @ org.junit.runners.blockjunit4classrunner.runchild(blockjunit4classrunner.java:50) @ org.junit.runners.parentrunner$3.run(parentrunner.java:193) @ org.junit.runners.parentrunner$1.schedule(parentrunner.java:52) @ org.junit.runners.parentrunner.runchildren(parentrunner.java:191) @ org.junit.runners.parentrunner.access$000(parentrunner.java:42) @ org.junit.runners.parentrunner$2.evaluate(parentrunner.java:184) @ org.junit.runners.parentrunner.run(parentrunner.java:236) @ org.eclipse.jdt.internal.junit4.runner.junit4testreference.run(junit4testreference.java:50) @ org.eclipse.jdt.internal.junit.runner.testexecution.run(testexecution.java:38) @ org.eclipse.jdt.internal.junit.runner.remotetestrunner.runtests(remotetestrunner.java:467) @ org.eclipse.jdt.internal.junit.runner.remotetestrunner.runtests(remotetestrunner.java:683) @ org.eclipse.jdt.internal.junit.runner.remotetestrunner.run(remotetestrunner.java:390) @ org.eclipse.jdt.internal.junit.runner.remotetestrunner.main(remotetestrunner.java:197) null
yu need set configurations in test class, whould re-create context file root of test class (just file resolution, when spring runners starts needs load context file)..your test class should this...
import org.junit.test; import org.junit.runner.runwith; import org.springframework.beans.factory.annotation.autowired; import org.springframework.test.context.contextconfiguration; import org.springframework.test.context.junit4.springjunit4classrunner; import org.springframework.test.context.web.webappconfiguration; @runwith(springjunit4classrunner.class) @contextconfiguration(locations = { "applicationcontext.xml" }) @webappconfiguration public class facadetest { private string phonenumber = "111111115"; @autowired private bussinesdelegate bussinesdelegate; @test public void runtest(){ this.fulltest(phonenumber, "aaaaa"); } public void fulltest(string phonenumber, string answer){ seek { bmoabussinesdelegate.processmessage("127.0.0.1", phonenumber, "camicase" + system.currenttimemillis()); } grab (exception e) { // todo auto-generated grab block e.printstacktrace(); } }
and dont forget check dependencies!
<!-- junit --> <dependency> <groupid>junit</groupid> <artifactid>junit</artifactid> <version>4.11</version> <scope>test</scope> </dependency> <!-- testng --> <dependency> <groupid>org.testng</groupid> <artifactid>testng</artifactid> <version>6.8.5</version> <scope>test</scope> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-test</artifactid> <version>${spring.version}</version> </dependency>
the cool thing spring, its ioc container can probide same behavior total java ee compliant server without needing server, , runner load context manually, same way ir in standole app spring ;)
java spring spring-mvc
No comments:
Post a Comment