<legend id='VYdUQ'><style id='VYdUQ'><dir id='VYdUQ'><q id='VYdUQ'></q></dir></style></legend>
      <bdo id='VYdUQ'></bdo><ul id='VYdUQ'></ul>
  1. <i id='VYdUQ'><tr id='VYdUQ'><dt id='VYdUQ'><q id='VYdUQ'><span id='VYdUQ'><b id='VYdUQ'><form id='VYdUQ'><ins id='VYdUQ'></ins><ul id='VYdUQ'></ul><sub id='VYdUQ'></sub></form><legend id='VYdUQ'></legend><bdo id='VYdUQ'><pre id='VYdUQ'><center id='VYdUQ'></center></pre></bdo></b><th id='VYdUQ'></th></span></q></dt></tr></i><div id='VYdUQ'><tfoot id='VYdUQ'></tfoot><dl id='VYdUQ'><fieldset id='VYdUQ'></fieldset></dl></div>

    <tfoot id='VYdUQ'></tfoot>
  2. <small id='VYdUQ'></small><noframes id='VYdUQ'>

    1. 如何模拟 FileInputStream 和其他 *Streams

      时间:2023-09-25

            <bdo id='86Dci'></bdo><ul id='86Dci'></ul>
                <tbody id='86Dci'></tbody>
            1. <tfoot id='86Dci'></tfoot>

            2. <i id='86Dci'><tr id='86Dci'><dt id='86Dci'><q id='86Dci'><span id='86Dci'><b id='86Dci'><form id='86Dci'><ins id='86Dci'></ins><ul id='86Dci'></ul><sub id='86Dci'></sub></form><legend id='86Dci'></legend><bdo id='86Dci'><pre id='86Dci'><center id='86Dci'></center></pre></bdo></b><th id='86Dci'></th></span></q></dt></tr></i><div id='86Dci'><tfoot id='86Dci'></tfoot><dl id='86Dci'><fieldset id='86Dci'></fieldset></dl></div>

              <small id='86Dci'></small><noframes id='86Dci'>

              <legend id='86Dci'><style id='86Dci'><dir id='86Dci'><q id='86Dci'></q></dir></style></legend>

              • 本文介绍了如何模拟 FileInputStream 和其他 *Streams的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我有一个获取 GenericFile 作为输入参数的类,它读取数据并进行一些额外的处理.我需要测试一下:

                I have class that gets GenericFile as input argument reads data and does some additional processing. I need to test it:

                public class RealCardParser {
                
                    public static final Logger l = LoggerFactory.getLogger(RealCardParser.class);
                
                    @Handler
                    public ArrayList<String> handle(GenericFile genericFile) throws IOException {
                        ArrayList<String> strings = new ArrayList<String>();
                        FileInputStream fstream = new FileInputStream((File) genericFile.getFile());
                        DataInputStream in = new DataInputStream(fstream);
                        BufferedReader br =  new BufferedReader(new InputStreamReader(in));
                        String strLine = br.readLine();//skip header
                        while ((strLine = br.readLine()) != null) {
                            l.info("handling in parser: {}", strLine);
                            strings.add(strLine);
                        }
                        br.close();
                        return strings;
                    }
                }
                

                问题在于新的 FileInputStream.我可以模拟 GenericFile 但它没有用,因为 FileInputStream 检查文件是否存在.我改变了我的班级:

                The issue is with new FileInputStream. I can mock GenericFile but it is useless cause FileInputStream checks if file exists. I changed my class so:

                public class RealCardParser {
                
                    public static final Logger l = LoggerFactory.getLogger(RealCardParser.class);
                
                    protected BufferedReader getBufferedReader(GenericFile genericFile) throws FileNotFoundException {
                        FileInputStream fstream = new FileInputStream((File) genericFile.getFile());
                        DataInputStream in = new DataInputStream(fstream);
                        return new BufferedReader(new InputStreamReader(in));
                    }
                
                    @Handler
                    public ArrayList<String> handle(GenericFile genericFile) throws IOException {
                        ArrayList<String> strings = new ArrayList<String>();
                        BufferedReader br = getBufferedReader(genericFile);
                        String strLine = br.readLine();//skip header
                        while ((strLine = br.readLine()) != null) {
                            l.info("handling in parser: {}", strLine);
                            strings.add(strLine);
                        }
                        br.close();
                        return strings;
                    }
                }
                

                所以现在我可以重写方法 getBufferedReader 和测试方法处理程序:

                So now I can override method getBufferedReader and test method handler:

                @RunWith(MockitoJUnitRunner.class)
                public class RealCardParserTest {
                
                    RealCardParser parser;
                
                    @Mock
                    GenericFile genericFile;
                
                    @Mock
                    BufferedReader bufferedReader;
                
                    @Mock
                    File file;
                
                    @Before
                    public void setUp() throws Exception {
                        parser = new RealCardParser() {
                            @Override
                            public BufferedReader getBufferedReader(GenericFile genericFile) throws FileNotFoundException {
                                return bufferedReader;
                            }
                        };
                
                        when(genericFile.getFile()).thenReturn(file);
                        when(bufferedReader.readLine()).thenReturn("header").thenReturn("1,2,3").thenReturn(null);
                    }
                
                    @Test
                    public void testParser() throws Exception {
                        parser.handle(genericFile);
                        //do some asserts
                    }
                }
                

                Handler 方法现在已经被测试覆盖了,但我仍然发现了导致 cobertura 问题的方法 getBufferedReader.如何测试方法 getBufferedReader 或者是否有其他解决方案?

                Handler method now is covered with tests, but I still have uncovered method getBufferedReader that leads to cobertura problems. How to test method getBufferedReader or maybe there is another solution of the problem?

                推荐答案

                您可以使用 PowerMockRunner 和 PowerMockito 模拟 FileInputStream.请参阅下面的模拟代码-

                You can mock FileInputStream by using PowerMockRunner and PowerMockito. See the below code for mocking-

                @RunWith(PowerMockRunner.class)
                @PrepareForTest({
                        FileInputStream.class
                })
                public class A{
                
                 @Test
                        public void testFileInputStream ()
                                    throws Exception
                       {
                           final FileInputStream fileInputStreamMock = PowerMockito.mock(FileInputStream.class);
                           PowerMockito.whenNew(FileInputStream.class).withArguments(Matchers.anyString())
                                               .thenReturn(fileInputStreamMock);
                    //Call the actual method containing the new constructor of FileInputStream 
                
                        }
                }
                

                这篇关于如何模拟 FileInputStream 和其他 *Streams的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:带有局部变量的 Mockito 下一篇:如何在 Java 中模拟静态方法?

                相关文章

                最新文章

                <small id='8ZAkq'></small><noframes id='8ZAkq'>

                <tfoot id='8ZAkq'></tfoot>
                  <legend id='8ZAkq'><style id='8ZAkq'><dir id='8ZAkq'><q id='8ZAkq'></q></dir></style></legend>
                  <i id='8ZAkq'><tr id='8ZAkq'><dt id='8ZAkq'><q id='8ZAkq'><span id='8ZAkq'><b id='8ZAkq'><form id='8ZAkq'><ins id='8ZAkq'></ins><ul id='8ZAkq'></ul><sub id='8ZAkq'></sub></form><legend id='8ZAkq'></legend><bdo id='8ZAkq'><pre id='8ZAkq'><center id='8ZAkq'></center></pre></bdo></b><th id='8ZAkq'></th></span></q></dt></tr></i><div id='8ZAkq'><tfoot id='8ZAkq'></tfoot><dl id='8ZAkq'><fieldset id='8ZAkq'></fieldset></dl></div>
                  • <bdo id='8ZAkq'></bdo><ul id='8ZAkq'></ul>