📄 loginservletmocktest.java
字号:
package login;
import javax.servlet.http.HttpServletRequest;
import org.jmock.Mock;
import org.jmock.MockObjectTestCase;
public class LoginServletMockTest extends MockObjectTestCase {
public void testMockRequest1() {
//构造一个Mock对象,参数必须是接口
Mock mock = new Mock(HttpServletRequest.class);
mock.expects(atLeastOnce()) //表示此方法至少执行一次
.method("getParameter") //方法名为getParameter
.with(eq("username")) //方法参数为"username"
.will(returnValue("tomclus")); //方法返回值是"tomclus"
mock.expects(atLeastOnce())
.method("getParameter")
.with(eq("password"))
.will(returnValue("abcd"));
//根据Mock对象获取一个HttpServletRequest对象的替身
HttpServletRequest request = (HttpServletRequest) mock.proxy();
//断言调用结果
assertEquals(true,new LoginServlet().isValidUser(request));
}
public static void main(String[] args) {
junit.textui.TestRunner.run(LoginServletMockTest.class);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -