JUnit - Java project structure - java

I have the following directory structure:
project/hamcrest-core-1.3.jar
project/junit-4.12.jar
project/build/
project/ija/ija2016/HomeWork2Test.java
project/ija/ija2016/homework2/model/cards/Card.java
project/ija/ija2016/homework2/model/cards/CardDeck.java
project/ija/ija2016/homework2/model/cards/CardStack.java
project/ija/ija2016/homework2/model/board/AbstractFactorySolitaire.java
project/ija/ija2016/homework2/model/cards/FactoryKlondike.java
The HomeWork2Test.java was given to us (I cannot edit this one) and contains tests for the other classes. In the header it has these imports:
package ija.ija2016.homework2;
import ija.ija2016.homework2.model.board.AbstractFactorySolitaire;
import ija.ija2016.homework2.model.board.FactoryKlondike;
import ija.ija2016.homework2.model.cards.Card;
import ija.ija2016.homework2.model.cards.CardDeck;
import ija.ija2016.homework2.model.cards.CardStack;
So I made the Card, CardStack and CardDeck classes into a package by specifying:
package ija.ija2016.homework2.model.cards;
in each of the files.
And the AbstractFactorySolitaire and FactoryKlondike have:
package ija.ija2016.homework2.model.board;
Now we are supposed to run the tests in the HomeWork2Test.java class using JUnit. However, when I try to run the following command from the project folder:
javac -cp junit-4.12.jar -d build ija/ija2016/homework2/HomeWork2Test.java
I get errors telling me that the:
package.ija2016.homework2.model.cards does not exist
package.ija2016.homework2.model.board does not exist
I don't exactly understand how to fix the project structure. Also how do I run the JUnit test?
Thank you for replies.

Well I think you are mixing concepts (compiling and running junit tests)
1st you need to compile your Classes (let's compile them to build
dir):javac -d build ija/ija2016/homework2/model/cards/*.java ija/ija2016/homework2/model/board/*.java
2nd you need to compile your test class (you will need to add to classpath what you just compiled and the junit.jar dependency): javac -d build -cp build;junit-4.12.jar ija/ija2016/HomeWork2Test.java
Now you can run your test class (in order to run you need to add to the classpath the build dir and the jar dependencies): java -cp build;junit-4.12.jar;hamcrest-core-1.3.jar org.junit.runner.JUnitCore ija.ija2016.HomeWork2Test
If you want to know more check JUnit 4 Doc
if you are using Linux or MacOS use ":" instead ";" between dirs in classpath

Related

Compile non-modular code that depends on modules

I have a folder with two compiled modules simple-modules/module1 and simple-modules/module2 these contained exploded code, so not its jar and both modules contain a module-info.java
I have other non-modular source code in folder normal-java-packages that uses the API exposed by the mentioned modules.
So, as a first step I would like to compile that.
Reading: https://openjdk.org/projects/jigsaw/spec/sotms/#module-artifacts
As far as I understand, the code in normal-java-packages should be part of the classpath, so it will be included in the unnamedmodule, and it will able to access all JRE modules as well as my user-defined modules simple-modules/module1 and simple-modules/module2.
I am trying to run:
javac -p simple-modules -d output -cp normal-java-packages
However, it looks like something is off as I get:
error: no source files
Any suggestions or pointers on what may be the issue here?
EDIT:
Also tried this command as suggested:
javac -p simple-modules -d output classpath_entry/com/myuser/modules/main/MainApp.java
Getting these errors:
classpath_entry/com/myuser/modules/main/MainApp.java:3: error: package com.myuser.hello is not visible
import com.myuser.hello.HelloModules;
^
(package com.myuser.hello is declared in module hellomodule, which is not in the module graph)
classpath_entry/com/myuser/modules/main/MainApp.java:4: error: package com.myuser.greetings is not visible
import com.myuser.greetings.EnglishGreeting;
^
(package com.myuser.greetings is declared in module hellomodule, which is not in the module graph)
classpath_entry/com/myuser/modules/main/MainApp.java:5: error: package com.myuser.other is not visible
import com.myuser.other.Other;
^
(package com.myuser.other is declared in module greetings, which is not in the module graph)
3 errors
So it looks like the compiled exploded modules in simple-modules are not visible, any idea why ?
Ok, so I will answer my own question as I found out what the issue is:
the command to use is:
javac -p simple-modules -d output --add-modules ALL-MODULE-PATH classpath_entry/com/myuser/modules/main/MainApp.java
I still find a bit redundant the --add-modules ALL-MODULE-PATH
The JRE modules are loaded fine without it, I don't see why we need it even when we are already specifying the user defined modules in the module path -p already.

How to run a JUnit test from the terminal [duplicate]

I would like to run JUnit test cases from the command line.
How can I do this?
For JUnit 5.x it's:
java -jar junit-platform-console-standalone-<version>.jar <Options>
Find a brief summary at https://stackoverflow.com/a/52373592/1431016 and full details at https://junit.org/junit5/docs/current/user-guide/#running-tests-console-launcher
For JUnit 4.X it's really:
java -cp .:/usr/share/java/junit.jar org.junit.runner.JUnitCore [test class name]
But if you are using JUnit 3.X note the class name is different:
java -cp .:/usr/share/java/junit.jar junit.textui.TestRunner [test class name]
You might need to add more JARs or directories with your class files to the classpath and separate that with semicolons (Windows) or colons (UNIX/Linux). It depends on your environment.
Edit: I've added current directory as an example. Depends on your environment and how you build your application (can be bin/ or build/ or even my_application.jar etc). Note Java 6+ does support globs in classpath, you can do:
java -cp lib/*.jar:/usr/share/java/junit.jar ...
Write tests! :-)
Maven way
If you use Maven, you can run the following command to run all your test cases:
mvn clean test
Or you can run a particular test as below
mvn clean test -Dtest=your.package.TestClassName
mvn clean test -Dtest=your.package.TestClassName#particularMethod
If you would like to see the stack trace (if any) in the console instead of report files in the target\surefire-reports folder, set the user property surefire.useFile to false. For example:
mvn clean test -Dtest=your.package.TestClassName -Dsurefire.useFile=false
Gradle way
If you use Gradle, you can run the following command to run all your test cases:
gradle test
Or you can run a particular test as below
gradle test --tests your.package.TestClassName
gradle test --tests your.package.TestClassName.particularMethod
If you would like more information, you can consider options such as --stacktrace, or --info, or --debug.
For example, when you run Gradle with the info logging level --info, it will show you the result of each test while they are running. If there is any exception, it will show you the stack trace, pointing out what the problem is.
gradle test --info
If you would like to see the overall test results, you can open the report in the browser, for example (Open it using Google Chrome in Ubuntu):
google-chrome build/reports/tests/index.html
Ant way
Once you set up your Ant build file build.xml, you can run your JUnit test cases from the command line as below:
ant -f build.xml <Your JUnit test target name>
You can follow the link below to read more about how to configure JUnit tests in the Ant build file:
https://ant.apache.org/manual/Tasks/junit.html
Normal way
If you do not use Maven, or Gradle or Ant, you can follow the following way:
First of all, you need to compile your test cases. For example (in Linux):
javac -d /absolute/path/for/compiled/classes -cp /absolute/path/to/junit-4.12.jar /absolute/path/to/TestClassName.java
Then run your test cases. For example:
java -cp /absolute/path/for/compiled/classes:/absolute/path/to/junit-4.12.jar:/absolute/path/to/hamcrest-core-1.3.jar org.junit.runner.JUnitCore your.package.TestClassName
The answer that #lzap gave is a good solution. However, I would like to add that you should add . to the class path, so that your current directory is not left out, resulting in your own classes to be left out. This has happened to me on some platforms. So an updated version for JUnit 4.x would be:
java -cp .:/usr/share/java/junit.jar org.junit.runner.JUnitCore [test class name]
Ensure that JUnit.jar is in your classpath, then invoke the command line runner from the console
java org.junit.runner.JUnitCore [test class name]
Reference: junit FAQ
With JUnit 4.12 the following didn't work for me:
java -cp .:/usr/share/java/junit.jar org.junit.runner.JUnitCore [test class name]
Apparently, from JUnit 4.11 onwards you should also include hamcrest-core.jar in your classpath:
java -cp .:/usr/share/java/junit.jar:/usr/share/java/hamcrest-core.jar org.junit.runner.JUnitCore [test class name]
In windows it is
java -cp .;/path/junit.jar org.junit.runner.JUnitCore TestClass [test class name without .class extension]
for example:
c:\>java -cp .;f:/libraries/junit-4.8.2 org.junit.runner.JUnitCore TestSample1 TestSample2 ... and so on, if one has more than one test classes.
-cp stands for class path and the dot (.) represents the existing classpath while semi colon (;) appends the additional given jar to the classpath , as in above example junit-4.8.2 is now available in classpath to execute JUnitCore class that here we have used to execute our test classes.
Above command line statement helps you to execute junit (version 4+) tests from command prompt(i-e MSDos).
Note: JUnitCore is a facade to execute junit tests, this facade is included in 4+ versions of junit.
If your project is Maven-based you can run all test-methods from test-class CustomTest which belongs to module 'my-module' using next command:
mvn clean test -pl :my-module -Dtest=CustomTest
Or run only 1 test-method myMethod from test-class CustomTest using next command:
mvn clean test -pl :my-module -Dtest=CustomTest#myMethod
For this ability you need Maven Surefire Plugin v.2.7.3+ and Junit 4.
More details is here: http://maven.apache.org/surefire/maven-surefire-plugin/examples/single-test.html
Actually you can also make the Junit test a runnable Jar and call the runnable jar as
java -jar
Personally I would use the Maven surefire JUnit runner to do that.
Alternatively you can use the following methods in JunitCore class http://junit.sourceforge.net/javadoc/org/junit/runner/JUnitCore.html
run (with Request , Class classes and Runner) or runClasses from your java file.

How to execute a JUnit package inside a jar

I have a project where a one single package contains all JUnit tests. (But those are not in src/test, but in src/main). In eclipse environment I can select the package and do run as JUnit. Then it will execute classes in alphabetical order. But what I want to do now is to do the same but using the jar i built. How can i do it ? (in command line)
Closest solution to this problem I can think of is to add a suite class to your src code and run the suite from command line. If you want you can use the dynamic classpath search that cpsuite gives you and do the following (this will also be more generic and not involves in adding suite class to your project):
Write a jar with with this class:
import org.junit.extensions.cpsuite.ClasspathSuite;
import org.junit.extensions.cpsuite.ClasspathSuite.IncludeJars;
import org.junit.runner.RunWith;
#RunWith(ClasspathSuite.class)
#IncludeJars(true)
public class MySuite {
}
Include this jar in the classpath and run something like this from the command line:
java -cp <path-to-where MySuite jar>:<path-to-tested-jar + all its dependencies>:<path-to cpsuite.jar + all its dependencies> org.junit.runner.JUnitCore <full-package-name-to-where-MySuite-is-in>.MySuite

How to use 'java' command options/arguments when executing JUnit tests

I'm developing a C# application to run JUnit tests automatically. Since it's complicated to move the current location where my C# application is running, i need to execute the JUnit tests without directly go inside their folder.
So, i have this folder tree:
» ProjectFolder
»» ClassesFolder
»» TestsFolder
»»» testX.java
»»» testX.class
I'm already able to compile the files, using:
javac ...\ProjectFolder\ClassesFolder\*.java
javac ...\ProjectFolder\TestsFolder\*.java
But i can't execute the tests. I tried:
java -classpath ...\ProjectFolder org.junit.runner.JUnitCore TestsFolder.testX
And got this error:
Error: Could not find or load main class org.junit.runner.JUnitCore
I even try this way:
java org.junit.runner.JUnitCore ...\ProjectFolder TestsFolder.testX
But, once again, it doesn't work:
JUnit version 4.10
Could not find class: -classpath
Could not find class: ...\ProjectFolder
Could not find class: TestsFolder.testX
Time: 0,003
OK (0 tests)
So, my main doubt, is how i can use the options (like -cp or -classpath) of the java command, when executing JUnit tests using org.junit.runner.JUnitCore.
Thanks in advance.
The problem is that the jUnit-runner is not in the classpath you specify. You need to add the path to junit.jar to the classpath.
Google is nice http://www.jsystemtest.org/?q=node/44
But why not just use something that already exists and can take care of this? Like Maven or Ant?

error package org.python.util does not exist, compilation with ant

I am trying to call a python method in java, similar functionality as Unresolved import org.python / working with jython and java?
However, I am using ant to compile and run my files. using import org.python.util will give me this error
package org.python.util does not exist
I can see that python.org.util exists in jython-2.5.0.jar.
So, here is the class path I have in my build.xml ant file:
classpath="${java.class.path}:./jgrapht/lib/jgrapht-jdk1.5.jar:\
./jgrapht/lib/jgraph.jar:./jgraphx/lib/jgraphx.jar:\
./jython/lib/jython-2.5.0.jar:./colt/lib/colt.jar:."
and I also I added the path to jython jar files to my class path. i.e. looks like echo $path gives me all the required paths. Is there anything missing here that I am not aware of?
Try this to make all classes in the package available:
import org.python.util.*;
Or this to include a particular class:
import org.python.util.TemplateAntTask;
EDIT: Also, looks like there is an extra slash after jython-2.5.0.jar in your classpath.

Categories

Resources