【问题标题】:Junit and EasyMock Servlet testingJunit 和 EasyMock Servlet 测试
【发布时间】:2016-03-20 21:08:23
【问题描述】:

我在测试 Servlet 时遇到问题。 Bouncer 是一个 Servlet,它具有简单的方法 doPost 和 init 被我覆盖。但是当我运行该代码时,我得到了异常

@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)...

如果有人知道如何解决它,我将不胜感激。

【问题讨论】:

    标签: java servlets junit easymock


    【解决方案1】:

    该错误表明 easymock 在模拟对象中遇到了方法调用 getMethod()。逐行调试程序并为模拟对象添加期望调用。

    如果方法调用不是模拟对象,则不必在 expect 中添加方法调用,但应将模拟对象中的所有调用添加到您的测试方法中。

    getMethod() 在服务中被调用,因为你在模拟 HttpServletRequest ,你还需要模拟在 HttpServletRequest 上调用的所有方法

    【讨论】:

      【解决方案2】:

      service() 方法在请求上调用 getMethod() 以确定它是否必须调用 doGet()doPost() 或其他 servlet 方法。由于您没有在模拟请求中将此调用存根到 getMethod(),因此 EasyMock 会引发此异常。

      你为什么不直接调用doPost(),而不是调用service(),因为那是你要测试的方法?

      【讨论】:

      • 我没有直接调用 doPost() 因为它默认受保护并且我的测试在其他包中;)
      • 好的,现在可以了,我把它改成公开的,所以我可以直接做,谢谢你的帮助;)
      • 将测试放在与测试类相同的包中是一种传统做法。您也可以公开该方法。如果您不想这样做,则必须对服务方法内部调用的所有方法进行存根。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-14
      相关资源
      最近更新 更多