美文网首页
Junit简单示例

Junit简单示例

作者: DCMeng | 来源:发表于2019-06-13 17:07 被阅读0次

    编写类文件Calculator

    package com.strong.logisticscloud.limarchetype.service;
    
    public class Calculator {
        private static int result; // 静态变量,用于存储运行结果
        public void add(int n) {
            result = result + n;
        }
        public void substract(int n) {
            result = result - 1;  //Bug: 正确的应该是 result =result-n
        }
        public void multiply(int n) {
        }         // 此方法尚未写好
        public void divide(int n) {
            result = result / n;
        }
        public void square(int n) {
            result = n * n;
        }
        public void squareRoot(int n) {
            for (; ;) ;            //Bug : 死循环
        }
        public void clear() {     // 将结果清零
            result = 0;
        }
        public int getResult(){
            return result;
        }
    }
    

    测试类文件CalculatorTest

    package com.strong.logisticscloud.limarchetype.service;
    
    import static org.junit.Assert.assertEquals;
    import static org.junit.Assert.fail;
    
    import java.util.Arrays;
    import java.util.Collection;
    
    import org.junit.Before;
    import org.junit.Ignore;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.junit.runners.Parameterized;
    import org.junit.runners.Parameterized.Parameters;
    
    public class CalculatorTest {
        private static Calculator calculator = new Calculator();
    
        @Before
        public void setUp() throws Exception {
            calculator.clear();
        }
    
        @Test(timeout = 1000)
        public void testAdd() {
            calculator.add(3);
            calculator.add(4);
            assertEquals(7, calculator.getResult());
        }
    
        @Test(timeout = 1000)
        public void testSubstract() {
            calculator.add(8);
            calculator.substract(3);
            assertEquals(5, calculator.getResult());
    
        }
        @Ignore
        @Test(timeout = 1000)
        public void testMultiply() {
            fail("Not yet implemented");
        }
    
        @Test(timeout = 1000)
        public void testDivide() {
            calculator.add(8);
            calculator.divide(2);
            assertEquals(4, calculator.getResult());
        }
    
        @Test(timeout = 1000)
        public void squareRoot() {
            calculator.squareRoot(4);
            assertEquals(2, calculator.getResult());
        }
    
        @Test(expected = Exception.class)
        public void divideByZero() {
            calculator.divide(0);
        }
    
        @RunWith(Parameterized.class)
        public static class SquareTest {
            private static Calculator calculator = new Calculator();
            private int param;
            private int result;
    
            @Parameters
            public static Collection<Object[]> data() {
                return Arrays.asList(new Object[][] { { 2, 5 }, { 0, 0 }, { -3, 9 }, });
            }
    
            // 构造函数,对变量进行初始化
            public SquareTest(int param, int result) {
                this.param = param;
                this.result = result;
            }
    
            @Test(timeout = 1000)
            public void square() {
                calculator.square(param);
                assertEquals(result, calculator.getResult());
            }
        }
    }
    
    

    相关文章

      网友评论

          本文标题:Junit简单示例

          本文链接:https://www.haomeiwen.com/subject/pyasfctx.html