earlyadopter
earlyadopter

Reputation: 1547

JUnit help wanted: Exception in thread "main" java.lang.NoSuchMethodError: main

I have two files:

Calc.java

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

and 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);
    }
  }

They compile without errors:

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

But when I'm trying to run, I'm getting an error:

java -cp .:junit-4.11.jar TestCalc

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

Could someone, please, explain why? And how to fix that?

Upvotes: 0

Views: 1068

Answers (1)

Vikdor
Vikdor

Reputation: 24124

You should use the JUnit's runners to run the test case classes.

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

JUnitCore will run the tests in your test class.

Upvotes: 1

Related Questions