1

サーブレットのテストに問題があります。Bouncerは、単純なメソッド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)で..

誰かがそれを修正する方法を知っていたら、私は感謝するでしょう。

4

2 に答える 2

1

このservice()メソッドはgetMethod()、リクエストを呼び出して、、または他のサーブレットメソッドを呼び出す必要があるかどうかを判断しdoGet()ますdoPost()。モックリクエストでこの呼び出しをスタブしなかったためgetMethod()、EasyMockはこの例外をスローします。

テストしたいメソッドなので、doPost()呼び出すのではなく、直接呼び出してみませんか?service()

于 2013-03-23T22:08:22.640 に答える
1

このエラーは、easymockがモックされたオブジェクトでgetMethod()を呼び出すメソッドに遭遇したことを示しています。プログラムを1行ずつデバッグし、モックオブジェクトのexpect呼び出しを追加します。

モックオブジェクトでない場合は、expectにメソッド呼び出しを追加する必要はありませんが、モックオブジェクトのすべての呼び出しをテストメソッドに追加する必要があります。

getMethod()はサービスで呼び出され、HttpServletRequestをモックしているため、HttpServletRequestで呼び出されるすべてのメソッドもモックする必要があります。

于 2013-03-23T22:08:49.003 に答える