test-》build path-  加库 junit4.
删除module.java
project->clean

添加junit 4







package test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class CalculatorTest {

    private static Calculator calculator = new Calculator();

    @Before
    public void setUp() throws Exception {
        calculator.clear();
    }

    @After
    public void tearDown() throws Exception {
    }

    // 加法 7+2=10
    @Test
    public void testAdd() {
        calculator.add(7);
        calculator.add(2);
        assertThat(calculator.getResult(), is(10));
    }

    // 减法（按教程要得到 5，我帮你改成正确逻辑）
    @Test
    public void testSubstract() {
        calculator.add(7);    // 先+7
        calculator.substract(2); // 再-2
        assertThat(calculator.getResult(), is(5)); // 7-2=5 ✔
    }

    // 乘法 3×3=9（必须先给初始值！）
    @Test
    public void testMultiply() {
        calculator.add(3);  // 先给 3
        calculator.multiply(3); // 3×3=9
        assertThat(calculator.getResult(), is(9));
    }

    // 除法 12/2=6
    @Test
    public void testDivide() {
        calculator.add(12);
        calculator.divide(2);
        assertThat(calculator.getResult(), is(6));
    }
}










package test;

public class Calculator {
    private int result = 0;

    public void add(int num) {
        result += num;
    }

    public void substract(int num) {
        result -= num;
    }

    public void multiply(int num) {
        result *= num;
    }

    public void divide(int num) {
        if (num == 0) throw new ArithmeticException("除数不能为0");
        result /= num;
    }

    public void clear() {
        result = 0;
    }

    public int getResult() {
        return result;
    }
}














