我有一個測試Servlet的問題。保鏢是一個簡單的方法doPost和init受我重寫的Servlet。但是,當我運行代碼,我得到異常Junit和EasyMock Servlet測試
@Before
public void Before() throws IOException, ServletException,
InstantiationException, IllegalAccessException,
IllegalArgumentException, InvocationTargetException,
NoSuchMethodException, SecurityException, ClassNotFoundException {
encoder = EasyMock.createMock(Encoder.class);
EasyMock.expect(encoder.encode("password")).andReturn("asdf");
EasyMock.expect(encoder.encode("nic")).andReturn("asss");
EasyMock.expect(encoder.encode("Password")).andReturn("ass");
EasyMock.replay(encoder);
db = EasyMock.createMock(UserDataBase.class);
db.connect();
EasyMock.expect(db.isConnected()).andReturn(true);
EasyMock.expect(db.getUserByLoginAndPassword("login", "asss"))
.andReturn(null);
EasyMock.expect(db.getUserByLoginAndPassword("login", "asdf"))
.andReturn(new User("Rafal", "Machnik"));
EasyMock.expect(db.getUserByLoginAndPassword("fake", "asdf"))
.andReturn(null);
EasyMock.expect(db.getUserByLoginAndPassword("login", "ass"))
.andReturn(null);
EasyMock.replay(db);
lsf = EasyMock.createMock(LoginServiceFactory.class);
EasyMock.expect(lsf.getEncoder()).andReturn(encoder).anyTimes();
EasyMock.expect(lsf.getUserDataBase()).andReturn(db).anyTimes();
EasyMock.replay(lsf);
config = EasyMock.createMock(ServletConfig.class);
EasyMock.expect(config.getInitParameter("LoginServiceFactory"))
.andReturn("pl.to.cw4.LoginServiceFactory");
EasyMock.replay(config);
request = EasyMock.createMock(HttpServletRequest.class);
EasyMock.expect(request.getParameter("login")).andReturn("login")
.anyTimes();
EasyMock.expect(request.getParameter("password")).andReturn("password")
.anyTimes();
EasyMock.replay(request);
pageSource = new StringWriter();
response = EasyMock.createMock(HttpServletResponse.class);
EasyMock.expect(response.getWriter())
.andReturn(new PrintWriter(pageSource)).anyTimes();
EasyMock.replay(response);
bouncer = new Bouncer(lsf);
bouncer.init(config);
}
@Test
public void bouncerTest() throws ServletException, IOException {
bouncer.service(request, response);
assertNotNull(pageSource.toString());
}
java.lang.AssertionError: 意外的方法調用getMethod(): 在org.easymock.internal.MockInvocationHandler.invoke(MockInvocationHandler.java:32)..
如果有人知道如何解決它,我會很感激。
好的;)謝謝我一定記得那個。 – RMachnik 2013-03-23 22:19:19