简体   繁体   English

在Groovy中模拟扩展Java类

[英]Mocking extended java classes in groovy

How can I mock java classes extended by groovy (Input/Output Stream) in groovy test? 如何在groovy测试中模拟由groovy(输入/输出流)扩展的Java类? I tried mockito and groovy mocks but with no luck. 我尝试了模仿和时髦的模拟,但是没有运气。 Is there any way it can work? 有什么办法可以工作吗? Any java/groovy library that can handle this case? 任何可以处理这种情况的java / groovy库?

There is a groovy servlet below that uses groovy getText method on InputStream and setBytes on OutputStream. 下面有一个groovy servlet,它在InputStream上使用groovy getText方法,在OutputStream上使用setBytes。 How to mock (1) getText and verify (2) setBytes in groovy test? 如何在Groovy测试中模拟(1)getText和验证(2)setBytes?

Thanks for help, 感谢帮助,

Michal 米哈尔

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

@groovy.transform.TypeChecked
class SomeServlet extends javax.servlet.http.HttpServlet {

    @Override
    protected void doPost(final HttpServletRequest request, final HttpServletResponse response) {
        final String charset = request.getCharacterEncoding();
        final String requestPayload = request.getInputStream().getText(charset); // GROOVY getText
        response.setContentType(request.getContentType());
        response.setStatus(200);
        response.setCharacterEncoding(charset);
        response.getOutputStream().setBytes(requestPayload.getBytes(charset)) // GROOVY setBytes
    }
}


class SomeServletTest {
    @org.testng.annotations.Test
    void aTest() {
        // given
        HttpServletRequest request = mock(HttpServletRequest)
        when(request.getCharacterEncoding()).thenReturn("UTF-8")
        when(request.getInputStream().getText("UTF-8")).thenReturn("some text")  // (1)

        HttpServletResponse response = mock(HttpServletResponse)

        def ss = new SomeServlet()

        // when
        ss.doPost(request, request)

        // then
        verify(response.getOutputStream()).setBytes("some text") // (2)
    }
}

I suggest you use ready made mocks of HttpServletRequest and HttpServletResponse instead of mocking the classes yourself. 我建议您使用HttpServletRequest和HttpServletResponse的现成的模拟程序,而不要自己模拟类。 Mocking Servlet API classes is difficult and would prove too costly in real world scenarios. 模拟Servlet API类非常困难,并且在现实世界中会证明成本太高。

Spring Framework has it all: http://docs.spring.io/spring-framework/docs/3.2.x/javadoc-api/org/springframework/mock/web/package-summary.html Spring Framework具备以下全部功能: http : //docs.spring.io/spring-framework/docs/3.2.x/javadoc-api/org/springframework/mock/web/package-summary.html

Using MockHttpServletRequest and MockHttpServletResponse classes, the unit test would be very simple and readable: 使用MockHttpServletRequest和MockHttpServletResponse类,单元测试将非常简单且可读:

import org.springframework.mock.web.MockHttpServletRequest
import org.springframework.mock.web.MockHttpServletResponse
import org.testng.Assert
import org.testng.annotations.Test

class SomeServletTest {

    @Test
    void aTest() {
        def request = new MockHttpServletRequest(
                characterEncoding: "UTF-8",
                content: "some text".bytes)

        def response = new MockHttpServletResponse()

        def ss = new SomeServlet()
        ss.doPost(request, response)

        Assert.assertEquals(response.getContentAsString(), "some text")
    }
}

Even if you don't want to build your whole application using Spring Framework, you can still benefit from its excellent unit test support. 即使您不想使用Spring Framework构建整个应用程序,仍然可以从其出色的单元测试支持中受益。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM