【问题标题】:mocking url.openStream() should return InputStream模拟 url.openStream() 应该返回 InputStream
【发布时间】:2019-05-07 11:47:57
【问题描述】:

我的实际类接受 URL 作为输入并调用 url.openStream(),这应该返回 InputStream。

public static Map<String, Object> parseA(URL url) throws Exception {
   byte[] readData = new byte[25*1024*1024];
    // Here url.openStream() returning null
            InputStream is = url.openStream();

            while((readLength = is.read(readData, 0, 25*1024*1024)) != -1){
                br = new BufferedReader(new InputStreamReader(new 
    ByteArrayInputStream(readData)));

                // All CW_* strings are collected first
}

我的测试课是

 @Rule
    public TemporaryFolder folder= new TemporaryFolder();
 @Test(enabled = true)
    public void parseATest() {

            File file=null;
           try {
             file =folder.newFile("testingData.txt");
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

            final URLConnection mockConnection = EasyMock.createMock(URLConnection.class);
            final URLStreamHandler handler = new URLStreamHandler() {

                @Override
                protected URLConnection openConnection(final URL arg0)
                        throws IOException {
                    return mockConnection;
                }
            };
            URL url=null;
            try {
                url = new URL("http://foo.bar", "foo.bar", 80, "", handler);
            } catch (MalformedURLException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            InputStream is=null;
            try {
                is = new FileInputStream(file);
            } catch (FileNotFoundException e2) {
                // TODO Auto-generated catch block
                e2.printStackTrace();
            } 
            try {
                EasyMock.expect(url.openStream()).andReturn(is).anyTimes();
            } catch (FileNotFoundException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        try {
             // imageHeaderParser is object of actual class
            imageHeaderParser.parseA(url);
        } catch (IfmSwimParserException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

这里 TemporaryFolder 用于创建临时文件。我不希望 URL 进入网络。它可以只是虚拟 URL,当我调用 url.openStream() 时,它应该返回临时文件流我提到了。

【问题讨论】:

    标签: java junit mocking java-io


    【解决方案1】:

    一个文件可以转换为一个 URL,所以就这样做吧:

    Url testUrl = Paths.get("folder",("testingData.txt").toUri().toURL();
    Map<String, Object> map = parseA(testUrl);
    // assert map content
    

    此外,如果您想测试文件处理行为,则不需要任何模拟。

    【讨论】:

    • 谢谢@davidxxx,你能告诉我在哪里可以找到这种信息
    • 不客气。您指的是哪种信息?
    • 与文件和新更新有关。执行测试用例后,这里的文件没有被删除。根据 TEMPFOLDER,它应该被删除吧?
    • 应该按照java doc删除:junit.org/junit4/javadoc/4.12/org/junit/rules/…,但这不是强制的:“删除成功与否不受这条规则的检查,不会抛出异常万一删除失败。”也许该文件不可删除,因为它仍处于打开状态。你可以检查一下。
    • 它没有打开。有什么方法可以删除它?
    猜你喜欢
    • 1970-01-01
    • 2012-03-12
    • 2011-09-16
    • 2013-03-09
    • 2021-01-07
    • 2023-04-06
    • 2020-07-01
    • 2012-12-29
    • 1970-01-01
    相关资源
    最近更新 更多