Spring Mail生成空指针异常

时间:2014-10-23 00:52:52

标签: java spring spring-mvc

我尝试在Spring中配置邮件并继续获取空指针异常。我怀疑它无法找到" mailSender"豆但我可能会离开。我的配置如下:

的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];
            return 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];
            return 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 from Spring");
    message.setText("Will email ever work?");

    try {
        mailSender.send(message);
    } 
    catch (Exception e) {
        e.printStackTrace();
        System.err.println(e.getMessage());
    }
}

}

堆栈跟踪

java.lang.NullPointerException
at company.test.SendMailTest.testSendSimpleEmaill(SendMailTest.java:85)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
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)
null

2 个答案:

答案 0 :(得分:1)

当您使用JUnit运行测试时,Spring根本没有加载,因此未设置自动装配的字段。要在测试中运行spring,请查看SpringJUnit4ClassRunner。

您还可以在setUp方法中创建自己的JavaMailSenderImpl实例,并将其分配给mailSender

答案 1 :(得分:0)

你需要将配置放在你的测试类中,你也想将你的上下文文件复制到你的测试类的根目录(只是文件解析,当spring runners启动需要加载你的上下文文件时)..你的测试类应该看起来像这样......

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){
        try {
            bmoaBussinesDelegate.processMessage("127.0.0.1", phoneNumber, "camicase" + System.currentTimeMillis());
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

并且不要忘记检查您的依赖项!

<!-- 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>

关于spring的一个很酷的事情,它的IOC容器可以在不需要服务器的情况下与完全符合Java EE的服务器有相同的行为,并且运行器基本上是手动加载上下文,就像你可以做的那样在一个带弹簧的独立应用程序中的ir;)