Calculator.java

    1. package com.xzk;
    2. public class Calculator {
    3. public int evaluate(String expression) {
    4. int sum = 0;
    5. for (String summand: expression.split("\\+")) {
    6. sum += Integer.valueOf(summand);
    7. }
    8. return sum;
    9. }
    10. }

    CalculatorTest.java

    1. package com.xzk;
    2. import static org.junit.Assert.assertEquals;
    3. import org.junit.Test;
    4. public class CalculatorTest {
    5. @Test
    6. public void evaluatesExpression() {
    7. Calculator calculator = new Calculator();
    8. int sum = calculator.evaluate("1+2+3");
    9. assertEquals(6, sum);
    10. }
    11. }