1

I'm trying to run an example of junit test using NetBeans, but I'm alwaus getting No runnable methods error although I actually have runnable methods as far as I understand. I found this example here: https://www.tutorialspoint.com/junit/junit_basic_usage.htm Could you please advise me how to solve this problem? the screenshot of the error package TestPackage;

public class ClassToTest {

   private String message;

   //Constructor
   //@param message to be printed

   public ClassToTest(String message){
      this.message = message;
   }

   // prints the message

   public String printMessage(){
      System.out.println(message);
      return message;
   }
}

//---------------------------------------------------------------------

package TestPackage;
import org.junit.Test;
import static org.junit.Assert.assertEquals;

public class TestJuniit {

   String message = "Hello World";
   ClassToTest ClassToTest = new ClassToTest(message);

   @Test
   public void testPrintMessage() {
      assertEquals(message,ClassToTest.printMessage());
   }
}

//---------------------------------------------------------------------

package TestPackage;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
public class TestRunner {

    public static void main(String[] args) {
      Result result = JUnitCore.runClasses(TestJuniit.class);

      for (Failure failure : result.getFailures()) {
         System.out.println(failure.toString());
      }

      System.out.println(result.wasSuccessful());
    }

}
Cute Shark
  • 87
  • 6
  • Your code style is inconsistent, confusing, and hard to read. I'd advise that you learn Java coding standards. I know I can run this easily in IntelliJ without writing a TestRunner class. I'd advise that you try that with IntelliJ. – duffymo Mar 24 '18 at 13:28
  • @duffymo Aside from a single instance of a field beginning with a capital letter, no it's not. Also, "switch IDE" is not useful solution. – Michael Mar 24 '18 at 13:41
  • May be you can refer to [java.lang.Exception: No runnable methods exception in running JUnits](https://stackoverflow.com/questions/24319697/java-lang-exception-no-runnable-methods-exception-in-running-junits/24319836) – thundear Mar 24 '18 at 13:56

2 Answers2

1

I recently encountered the same with IntelliJ, where the reason was that my test class didn't follow the naming convention, ending with "Test" or "TestCase" as far as I remember. In your example you are not following this convention, maybe that is the cause?

Tobb
  • 11,850
  • 6
  • 52
  • 77
  • I've just tried to rename it, but unfortunately it didn't help so it seems that there is something else here. – Cute Shark Mar 24 '18 at 13:43
  • From your screenshot it looks like you are running the tests through a class that you wrote yourself. I have not seen that way of running tests before. Usually, tests can be run directly in the IDE, typically by right-clicking the test method. I would assume Netbeans has this capability. – Tobb Mar 24 '18 at 13:50
  • Yes, that screenshot might have been a bit misleading. I've uploaded another one. In fact I get this error all the time no matter how I run the TestRunner class – Cute Shark Mar 24 '18 at 13:56
0

Runs and passes for me running in IntelliJ.

I put the class and the test in the same package - one under /src/java and the other under test/java.

package cruft;

/**
 * Created by Michael
 * Creation date 3/24/2018.
 * @link https://stackoverflow.com/questions/49465133/junit-no-runnable-methods?noredirect=1#49465133
 */
public class ClassToTest {

    private String message;
    public ClassToTest(String message){
        this.message = message;
    }

    public String printMessage(){
        System.out.println(message);
        return message;
    }
}

Test class with corrections:

package cruft;

import org.junit.Assert;
import org.junit.Test;

/**
 * Created by Michael
 * Creation date 3/24/2018.
 * @link https://stackoverflow.com/questions/49465133/junit-no-runnable-methods?noredirect=1#49465133
 */
public class ClassToTestTest {

    @Test
    public void testPrintMessage() {
        // setup
        String message = "Hello World";
        ClassToTest classToTest = new ClassToTest(message);
        // exercise
        // assert
        Assert.assertEquals(message,classToTest.printMessage());
    }
}
duffymo
  • 305,152
  • 44
  • 369
  • 561
  • The test runs without the testrunner for me too. But what about the TestRunner? Isn't it obligatory to use? – Cute Shark Mar 24 '18 at 14:07
  • We have two data points that tell us "no". That's why I recommended that you not write the test runner. IDEs, Maven, Gradle - all tests are run without you having to write a TestRunner. – duffymo Mar 24 '18 at 14:16
  • Actually now I think that maybe the TestRunner has something to do with the command line. In the tutorial that I read, the examples are being run from the command line. I get all the required results and information when I run the tests in netbeans without the test runner. So yes, it solves the issue. It will be even easier now without this additional class. Thank you. – Cute Shark Mar 24 '18 at 14:21