• TDD之断言验证System.out.print输出


    业务类:

    public class OutDemo {
    
        public void print(String content) {
            System.out.print(content);
        }
        
    }
    

    测试类:

    public class OutDemoTest {
    
        private StringBuilder systemOutput;
    
        @Before
        public void setUp() {
            systemOutput = injectSystemOutput();
        }
    
        @Test
        public void testOut() {
            OutDemo out = new  OutDemo();
            out.print("123");
            Assert.assertEquals(systemOutput.toString(), "123");
        }
    
        private StringBuilder injectSystemOutput() {
            StringBuilder stringBuilder = new StringBuilder();
            PrintStream outputPrintStream = new PrintStream(new OutputStream() {
                @Override
                public void write(int b) throws IOException {
                    stringBuilder.append((char) b);
                }
            });
            System.setOut(outputPrintStream);
            return stringBuilder;
        }
    
    }
    
  • 相关阅读:
    最小花费
    LOJ10090
    LOJ2436
    loj10087
    LOJ2632
    LOJ10021 Addition Chains
    LOJ10019生日蛋糕
    loj10018数的划分
    LOJ10015扩散
    loj10014数列分段二
  • 原文地址:https://www.cnblogs.com/markLogZhu/p/11955801.html
Copyright © 2020-2023  润新知