0

我有两个文件:

计算器

public class Calc {
 public int add(int a, int b) {
  return a + b;
 }
 public int subtract(int a, int b) {
  return a - b;
 }
}

和 TestCalc.java

import org.junit.* ;
import static org.junit.Assert.* ;

public class TestCalc {

  Calc c = new Calc();

  @Test
  public void testSum() {
    // 6 + 9 = 15
    int expected = 15;
    int actual = c.add(6, 9);
    assertEquals("adding 6 and 9", expected, actual);
  }

  @Test
  public void testSubstr() {
    // 18 - 3 = 15
    int expected = 15;
    int actual = c.subtract(18, 3);
    assertEquals("subtracting 3 from 18", expected, actual);
    }
  }

他们编译没有错误:

javac -cp .:junit-4.11.jar TestCalc.java
javac -cp .:junit-4.11.jar Calc.java

但是当我尝试运行时,我收到了一个错误:

java -cp .:junit-4.11.jar TestCalc

Exception in thread "main" java.lang.NoSuchMethodError: main

有人可以解释一下为什么吗?以及如何解决这个问题?

4

1 回答 1

1

您应该使用JUnit 的运行器来运行测试用例类。

java -cp .:junit-4.11.jar org.junit.runner.JUnitCore TestCalc.java

JUnitCore 将在您的测试类中运行测试。

于 2012-11-30T04:42:29.243 回答