0

Context I've just started working with Java (Ruby background), and need to test-drive a Hello World CLA. I'm using JUnit (no managers like Gradle/Maven), but cannot compile my test to run it.

Structure

HelloWorldJava/src

  • main/java/HelloWorld.java
  • test/java/HelloWorldTest.java

Program

public class HelloWorld{
    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

Test

import org.junit.*;
import static org.junit.jupiter.api.Assertions.*;

import example.util.Calculator;

import org.junit.jupiter.apiTest;

class HelloWorldTest {

@Test
public void testAddition() {
    HelloWorld helloWorld = new HwlloWorld();
        assertTrue(helloWorld.equals("Hello World!"));
    }
}

Problem

From src: $ java -cp :'./test/java/*' HelloWorldTest.java returns

Error: Could not find or load main class HelloWorldTest.java

My best guess is that it's something to do with the classpath, but the error isn't giving much.

What do I need to do to compile and run my tests? I have tried changing the test method to 'main', which doesn't change the error.

Edit: I have looked at similar questions and haven't succeeded in implementing a solution.

Jules
  • 303
  • 2
  • 13
  • Please check your package structure – Oozeerally Feb 19 '20 at 13:14
  • 1
    Does this answer your question? [How to run JUnit test cases from the command line](https://stackoverflow.com/questions/2235276/how-to-run-junit-test-cases-from-the-command-line) – Aleksandr Semyannikov Feb 19 '20 at 13:20
  • @AdilOoze As far as I can tell, my package structure is correct per https://stackoverflow.com/a/1540376 Although it looks like there are alternatives – Jules Feb 19 '20 at 13:21
  • @AleksandrSemyannikov I've looked at it and haven't managed to make anything there work. I've had a look at several similar questions and either cannot understand them, or have tried and the solutions haven't worked. – Jules Feb 19 '20 at 13:29

1 Answers1

1

HelloWorldTest does not contain the main method, that is why it is not found. The main Method is in HelloWorld. To run it type

java -cp ./test/java HelloWorld

But that want run the test. It will print the message.

To run the test you need a main method that starts the JUnit test runner.

import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;

public class MyTestRunner {
  public static void main(String[] args) {
    Result result = JUnitCore.runClasses(MyClassTest.class);
    for (Failure failure : result.getFailures()) {
      System.out.println(failure.toString());
    }
  }
}

Please read this great tutorial: https://www.vogella.com/tutorials/JUnit/article.html

As a side note I recommend to structure your code in packages. That is put them in folders and use the package instruction. See here: https://docs.oracle.com/javase/tutorial/java/package/createpkgs.html

David
  • 1,204
  • 12
  • 16
  • Thanks for your response, I appreciate the resources. Could you clarify where the JUnit test runner should be? Would it go in my HelloWorldTest file, or its own? – Jules Feb 19 '20 at 14:00
  • I was following that tutorial before, I feel that something was glossed over around the part you mentioned, but it was going well until then. – Jules Feb 19 '20 at 14:22
  • 1
    I would put the test runner in its own class. Then remember to put that class, your test class, the classes under test and not to mention the junit library on the class path when compiling and when running. It is a nice exercise but in real life that is one reason why we use tools like IDEs or Maven to manage all the dependencies. – David Feb 19 '20 at 15:49
  • JUnit comes with a test runner, there is no need to include this in your own classes. – Mark Rotteveel Feb 20 '20 at 17:03