Wednesday, November 26, 2014

JMockit


    final static String HTTP_RESP = "Test HTTP response\nHello World!\n";
   
    @Test
    public void test(@Mocked final URLConnection conn, @Mocked final URL url) throws IOException {
       
        new NonStrictExpectations() {
            {
                url.openConnection(); returns(conn);
                conn.getInputStream(); returns(new ByteArrayInputStream(HTTP_RESP.getBytes()));
            }
        };

        Assert.assertEquals(HTTP_RESP, getResponseStr());
    }
   
    public String getResponseStr() throws IOException {
        StringBuilder sb = new StringBuilder();
        URL url = new URL("http://www.google.com");
        URLConnection conn = url.openConnection();
        BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line = null;
        while((line = reader.readLine()) != null) {
            sb.append(line).append("\n");
        }
        return sb.toString();
    }

http://abhinandanmk.blogspot.com/2012/06/jmockit-tutoriallearn-it-today-with.html

No comments:

Post a Comment