Reputation: 3684
I have a problem with testing Servlet. Bouncer is a Servlet with simple method doPost and init overrited by me. But when i run that code i get exception
@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: Unexpected method call getMethod(): at org.easymock.internal.MockInvocationHandler.invoke(MockInvocationHandler.java:32)...
If someone had idea how to fix it I will be thankful.
Upvotes: 1
Views: 2087
Reputation: 3777
The error indicates that the easymock has come across a method call getMethod() in a mocked object . Debug the program line by line and add a expect call for the mocked object.
You don't have to add the method call in expect if it not a mocked object but all the calls in a mocked object should be added to your test method.
getMethod() is called in the service and since you are mocking HttpServletRequest , you also need to mock all the methods call on HttpServletRequest
Upvotes: 1
Reputation: 691655
The service()
method calls getMethod()
on the request to determine if it must call doGet()
, doPost()
or the other servlet methods. Since you didn't stub this call to getMethod()
on your mock request, EasyMock throws this exception.
Why don't you call doPost()
directly, rather than calling service()
, since that's the method you want to test?
Upvotes: 1