Running Junit test from command line

marknorkin

So I wrote a class called Item and using Junit wrote a test called ItemTest and I want to execute my test from a command line. This is steps that I followed:

  1. set the path, to run java.exe and set the classpath to libraries

    ...> set path="C:\Program Files (x86)\Java\jdk1.7.0_40\bin"
    ...> set classpath=C:\Program Files (x86)\Java\hamcrest-core-1.3.jar; C:\Program Files (x86)\Java\junit-4.11.jar
    
  2. then i simply run org.junit.runner.JUnitCore to see if added libs were in classpath

    ...>java org.junit.runner.JUnitCore
    

    the output was

    JUnit version 4.11
    Time: 0,001
    OK (0 tests)
    
  3. Then I compiled without an error Item and ItemTest

    C:\Users\User\IdeaProjects\My_project\src>javac .\main\java\Item.java .\test\java\ItemTest.java
    
  4. Then try ro run this classes

    C:\Users\User\IdeaProjects\My_project\src>java org.junit.runner.JUnitCore main.java.Item test.java.ItemTest
    
  5. In the end I got this

    JUnit version 4.11
    Exception in thread "main" java.lang.NoClassDefFoundError: main/java/Item (wrong name: Item)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:792)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:14 2)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:190)
    at org.junit.runner.JUnitCore.runMain(JUnitCore.java:86)
    at org.junit.runner.JUnitCore.runMainAndExit(JUnitCore.java:47)
    at org.junit.runner.JUnitCore.main(JUnitCore.java:40)
    

    How to fix this ? Should I put Itemin classpath ?

UPD#1

Put this path in my classpath doesn't work (with and without src):

    C:\Users\User\IdeaProjects\My_project\src> 

UPD#2

When I want to run the class I adress to him as he stores in file system or in what packages he lays ? Because I don't have packages and so My_project apart from being project name is a module name.

SOLVED

This is the way - I add to classpath next paths:

  1. The path to class Item:

    C:\Users\User\IdeaProjects\My_project\src\main\java\;
    
  2. The path to test class ItemTest:

    C:\Users\User\IdeaProjects\My_project\src\test\java\;
    
  3. Then simply run:

    ...>java org.junit.runner.JUnitCore ItemTest
    
Alexandre Santos

You have to put the location of the .class files in the classpath. Even if you are in the same folder it won't find them if they are not in the classpath. That comes from DOS x .... anyone else.

In DOS the OS looks first in the current folder. Everywhere else the OS looks first in the path (or classpath for Java).

If Item is in the package com.example.Item and the class is in c:\users\test\myproj\target\com\example\Item.class then you must add c:\users\test\myproj\target\ to the classpath

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Issues with building and running junit test files with command line

From Dev

Error when running JUnit Tests from DOS Command line

From Dev

How to add arguments to junit test triggered from command line?

From Java

Running single test from unittest.TestCase via command line

From Java

Karma: Running a single test file from command line

From Dev

Running a mocha test using Chai and TypeScript from the command line

From Dev

What are the columns in the output of running a jmeter test from the command line?

From Dev

Run Junit Test via command line

From Dev

How to pass command line arguments to Junit Test

From Dev

Run JUnit Test from command line: non-android test in an android project

From Dev

Running command line from C

From Dev

Running Python from Command Line

From Dev

running pug from command line

From Dev

Running PackageMaker from command line

From Dev

Running this from command line in Java

From Dev

JUnit set timeout from command line

From Dev

Getting list of tests from JUnit command line

From Dev

Running individual XCTest (UI, Unit) test cases for iOS apps from the command line

From Dev

Why doesn't running test-with-groovy from ant command line in a netbeans project run the tests?

From Dev

Jmeter - How do I see sampler data following running a test from Command line?

From Dev

Is there anyway to mimic or mock a Linux command line in a Java JUnit test

From Dev

Compile and run test with JUnit in command line on a single Java class

From Dev

Is there anyway to mimic or mock a Linux command line in a Java JUnit test

From Dev

Test cases are not running in JUnit

From Dev

Running Spring Batch jobs from the command line

From Dev

Building/running iOS app from command line

From Dev

running dos command line from C#?

From Dev

Running java in package from command line

From Dev

Running exe from command line with parameters

Related Related

  1. 1

    Issues with building and running junit test files with command line

  2. 2

    Error when running JUnit Tests from DOS Command line

  3. 3

    How to add arguments to junit test triggered from command line?

  4. 4

    Running single test from unittest.TestCase via command line

  5. 5

    Karma: Running a single test file from command line

  6. 6

    Running a mocha test using Chai and TypeScript from the command line

  7. 7

    What are the columns in the output of running a jmeter test from the command line?

  8. 8

    Run Junit Test via command line

  9. 9

    How to pass command line arguments to Junit Test

  10. 10

    Run JUnit Test from command line: non-android test in an android project

  11. 11

    Running command line from C

  12. 12

    Running Python from Command Line

  13. 13

    running pug from command line

  14. 14

    Running PackageMaker from command line

  15. 15

    Running this from command line in Java

  16. 16

    JUnit set timeout from command line

  17. 17

    Getting list of tests from JUnit command line

  18. 18

    Running individual XCTest (UI, Unit) test cases for iOS apps from the command line

  19. 19

    Why doesn't running test-with-groovy from ant command line in a netbeans project run the tests?

  20. 20

    Jmeter - How do I see sampler data following running a test from Command line?

  21. 21

    Is there anyway to mimic or mock a Linux command line in a Java JUnit test

  22. 22

    Compile and run test with JUnit in command line on a single Java class

  23. 23

    Is there anyway to mimic or mock a Linux command line in a Java JUnit test

  24. 24

    Test cases are not running in JUnit

  25. 25

    Running Spring Batch jobs from the command line

  26. 26

    Building/running iOS app from command line

  27. 27

    running dos command line from C#?

  28. 28

    Running java in package from command line

  29. 29

    Running exe from command line with parameters

HotTag

Archive