Search code examples
javajunithamcrest

Why am I launching the tests incorrectly?


Basically I am building a project for an assignment that has bunch of tests that needs to pass using Junit. But I seem to be trying to run it incorrectly and I don't know how or why

Basically i have directory like this

[project]
Test.java
junit-4.12.jar
hamcrest-core-1.3.jar
src/cards/bunch of files
src/boards/bunch of files

So first thing I did is compile the project using javac Test.java after few small fixes it compiled, so next thing I wanted to do is launch the actual tests.

So I launched it with java -cp ./junit-4.12.jar:./hamcrest-core-1.3.jar:. Test.java

I get the following error

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

Any idea what I'm doing wrong?

EDIT: As you can see compilation went through, i'm referencing at least presumably correctly to JUnit. Here's how's my directory structured

My directory.

And how's my test file looking

enter image description here


Solution

  • Maybe you should compile first your Test.java file.

    Then specify the correct packages if there is one

    java -cp ./junit-4.12.jar:./hamcrest-core-1.3.jar:. src.Test
    

    Take a look at where is your Test.class file

    Here you'll find a complete example that explains how to run your tests from command line.

    https://www3.ntu.edu.sg/home/ehchua/programming/java/JavaUnitTesting.html