我试图使用Spring对一个简单的portlet进行单元测试。以下是我到目前为止所做的......
import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.assertThat;
import java.io.IOException;
import javax.portlet.GenericPortlet;
import javax.portlet.PortletException;
import org.junit.Test;
import org.springframework.mock.web.portlet.MockPortletConfig;
import org.springframework.mock.web.portlet.MockRenderRequest;
import org.springframework.mock.web.portlet.MockRenderResponse;
public class HelloWorldPortletTest {
@Test
public void testDoView() throws PortletException, IOException {
MockRenderResponse renderResponse = new MockRenderResponse();
GenericPortlet portlet = new HelloWorldPortlet();
portlet.init(new MockPortletConfig());
portlet.render(new MockRenderRequest(), renderResponse);
assertThat(renderResponse.getContentType(), equalTo("text/html"));
assertThat(renderResponse.getContentAsString(), equalTo("Hello Portlet" + System.getProperty("line.separator")));
}
}
不幸的是,当我运行它时,我得到以下NullPointerException:
java.lang.NullPointerException
at javax.portlet,GenericPortlet.getTitle(GenericPortlet.java:281)
at stuff.HelloWorldPortlet.getTitle(HelloWorldPortlet.java:126)
at javax.portlet.GenericPortlet.render(GenericPortlet.java:252)
at stuff.HelloWorldPortlet.render(HelloWorldPortlet.java:94)
at stuff.HelloWorldPortletTest.testDoView(HelloWorldPortletTest.java:24)
如何让Spring读入portlet.xml文件,以便GenericPortlet的getTitle()方法不返回null?