com.ajaxjs.web.mock.MockResponse Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ajaxjs-web Show documentation
Show all versions of ajaxjs-web Show documentation
AJAXJS Web aims to full-stack, not only the server-side framework, but also integrates the front-end library. It's written in HTML5 + Java, a successor to the JVM platform, efficient, secure, stable, cross-platform and many other advantages, but it abandoned the traditional enterprise architecture brought about by the large and bloated, emphasizing the lightweight, and fast, very suitable for the Internet fast application.
package com.ajaxjs.web.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import javax.servlet.ServletOutputStream;
import javax.servlet.WriteListener;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.mockito.ArgumentCaptor;
public class MockResponse {
/**
* 除了字符串使用 StringWriter,Response 输出的还可以是流
*
* @param response 响应对象
* @return 流对象以便获取信息
*/
public static ServletOutputStream streamFactory(HttpServletResponse response) {
ServletOutputStream out = new ServletOutputStream() {
private OutputStream os = new ByteArrayOutputStream();
@Override
public void write(int i) throws IOException {
os.write(i);
}
@Override
public String toString() {
return os.toString();
}
public boolean isReady() {
return false;
}
public void setWriteListener(WriteListener arg0) {
}
};
try {
when(response.getOutputStream()).thenReturn(out);
} catch (IOException e) {
e.printStackTrace();
}
return out;
}
/**
* Writer:形象的比喻:当我们调用 response.getWriter()
* 这个对象同时获得了网页的画笔,这时你就可以通过这个画笔在网页上画任何你想要显示的东西。Writer 就是向页面输出信息,负责让客户端显示内容
*
* @param response 响应对象
* @return writer 以便获取输出信息
*/
public static StringWriter writerFactory(HttpServletResponse response) {
StringWriter writer = new StringWriter();
try {
when(response.getWriter()).thenReturn(new PrintWriter(writer));
} catch (IOException e) {
e.printStackTrace();
}
return writer;
}
/**
* 获取 MVC 跳转模版的那个路径
*
* @param request 请求对象
* @return 模版路径
*/
public static String getRequestDispatcheResult(HttpServletRequest request) {
ArgumentCaptor dispatcherArgument = ArgumentCaptor.forClass(String.class);
verify(request).getRequestDispatcher(dispatcherArgument.capture());
return dispatcherArgument.getValue();
}
}