• junit case


    package test;
    
    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;
        }
    }
    package test;
    
    import static org.junit.Assert.*;
    
    import org.junit.Before;
    import org.junit.Test;
    
    public class TestCal {
    
        private static Calculator calculator = new Calculator();
        public void setUp() throws Exception {
            calculator.clear();
        }
    
        @Test
        public void testAdd() {
            calculator.add(2);
            calculator.add(3);
            assertEquals(5, calculator.getResult());
        }
    
        @Test
        public void testSubstract() {
            calculator.add(10);
            calculator.substract(2);
            assertEquals(8, calculator.getResult());
        }
    
        @Test
        public void testMultiply() {
            fail("Not yet implemented");
        }
    
        @Test
        public void testDivide() {
            calculator.add(8);
            calculator.divide(2);
            assertEquals(4, calculator.getResult());
        }
    
    }
  • 相关阅读:
    PAT乙级1002. 写出这个数 (20)
    PAT乙级1001. 害死人不偿命的(3n+1)猜想 (15)
    HDU 4520 小Q系列故事——最佳裁判
    POJ 3414 Pots(BFS)
    POJ 1118 Lining Up
    POJ 2484 A Funny Game
    POJ 2231 Moo Volume
    HDU 5718 Oracle(高精度)
    FZU 1502 Letter Deletion(DP)
    HDU 1564 Play a game
  • 原文地址:https://www.cnblogs.com/yufenghou/p/4001075.html
Copyright © 2020-2023  润新知