我正在为servlet编写测试用例。在测试中,我无法提供Servlet Config变量。我也提供我的代码和测试
代码是:
public void service (HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
ServletConfig config = getServletConfig();
msSmtpServer = getServletConfig().getInitParameter("smptserver");
msSmtpPort = getServletConfig().getInitParameter("smtpport");
msUserName = getServletConfig().getInitParameter("username");
msPassword = getServletConfig().getInitParameter("password");
String buf = getRequestBuffer(request).toString();
并且测试是:
public class AddUserServletTest {
@Mock
HttpServletRequest request;
@Mock
HttpServletResponse response;
@Mock
HttpSession session;
@Mock
RequestDispatcher rd;
@Mock
ServletOutputStream servletOut;
@Mock
ServletConfig sg;
public final static String Seperator = new Character((char)1).toString();
public final static String ContentDelimeter = new Character((char)2).toString();
@BeforeClass
public void setUp() throws Exception
{
MockitoAnnotations.initMocks(this);
}
@Test
public void test() throws Exception {
DataSet ds=new DataSet();
String buffer=ds.getUserId()+Seperator+ds.getTableID()+Seperator+ds.getMemberID()+Seperator+ds.getNHID();
ByteArrayOutputStream bos = new ByteArrayOutputStream ();
ZipOutputStream out = new ZipOutputStream(bos);
out.putNextEntry(new ZipEntry("request.txt"));
out.write(buffer.getBytes("UTF-8"));
out.closeEntry();
out.close ();
bos.close();
String b64String = Base64.encodeBase64String(bos.toByteArray());
Reader inputString = new StringReader(b64String);
BufferedReader reqbuffer = new BufferedReader(inputString);
when(request.getReader()).thenReturn(reqbuffer);
when(sg.getInitParameter("smptserver")).thenReturn("abc.xyz.com");
when(sg.getInitParameter("smtpport")).thenReturn("80");
when(sg.getInitParameter("username")).thenReturn("password@xyz.com");
when(sg.getInitParameter("password")).thenReturn("abcd");
when(response.getOutputStream()).thenReturn(servletOut);
new AddUsers().service(request, response);
ArgumentCaptor<String> bufferCaptor = ArgumentCaptor.forClass(String.class);
verify(servletOut).print(bufferCaptor.capture());
String responseBody = bufferCaptor.getValue();
ZipInputStream zipIn = new ZipInputStream(new ByteArrayInputStream(Base64.decodeBase64(responseBody.toString().getBytes())));
zipIn.getNextEntry();
StringBuffer sb = new StringBuffer();
ByteArrayOutputStream out1 = new ByteArrayOutputStream();
IOUtils.copy(zipIn, out1);
sb = new StringBuffer();
sb.append(out1.toString("UTF-8"));
System.out.println("--------------- :"+sb.toString());
String[] res=sb.toString().split(Seperator);
AssertJUnit.assertEquals("Success",res[0]);
}
}
我试图初始化但是在servlet中,值为null而没有获取值。
如何使用mockito初始化测试中的ServletConfig变量?
答案 0 :(得分:2)
当前代码:
new AddUsers().service(request, response);
创建servlet的新实例并尝试立即使用它,以便实例不知道sg
。
尝试这样可以在使用之前注入模拟:
AddUsers servlet = new AddUsers();
servlet.init(sg); // use the servlet life-cycle method to inject the mock
servlet.service(request, response);
答案 1 :(得分:0)
主观上servlet是一个可怕的概念,至少是因为它们的初始化打破了需要默认构造函数的封装概念,并将初始化委托给init
方法而不是使用构造函数参数。然而,可以通过提供用于测试目的的包可见构造器来解决。基本上你需要能够注入一个ServletConfig
的实例,你可以按照@Andres S描述的解决方案,或者你可以遵循以下,一般,不是特定于servlet,模式:
public class AddUsers implements Servlet... {
private final Supplier<ServletConfig> configSupplier = () -> some default call;
public AddUsers() {}
@VisibleForTesting // guava
AddUsers(Supplier<ServletConfig> configSupplier) {
this.configSupplier = configSupplier;
}
public void service (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletConfig config = configSupplier.get();
...
在测试中,您只需使用左侧打开的构造函数进行测试:
new AddUsers(() -> sg).service(request, response);