简体   繁体   English

从命令行运行Junit测试

[英]Running Junit test from command line

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. 因此,我编写了一个名为Item的类,并使用Junit编写了一个名为ItemTest的测试,我想从命令行执行测试。 This is steps that I followed: 这是我遵循的步骤:

  1. set the path, to run java.exe and set the classpath to libraries 设置路径以运行java.exe并将类路径设置为库

     ...> 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 然后我只需运行org.junit.runner.JUnitCore来查看是否在类路径中添加了库

     ...>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 然后我编译没有错误的ItemItemTest

     C:\\Users\\User\\IdeaProjects\\My_project\\src>javac .\\main\\java\\Item.java .\\test\\java\\ItemTest.java 
  4. Then try ro run this classes 然后尝试ro运行此类

     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 Item in classpath ? 我应该将Item放在classpath中吗?

UPD#1 UPD#1

Put this path in my classpath doesn't work (with and without src ): 将此路径放在我的类路径中不起作用(使用和不使用src ):

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

UPD#2 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. 因为我没有软件包,所以My_project除了是项目名外,还包括模块名。

SOLVED 解决了

This is the way - I add to classpath next paths: 这是方法-我将以下路径添加到classpath中:

  1. The path to class Item : Item的路径:

     C:\\Users\\User\\IdeaProjects\\My_project\\src\\main\\java\\; 
  2. The path to test class ItemTest : 测试类ItemTest的路径:

     C:\\Users\\User\\IdeaProjects\\My_project\\src\\test\\java\\; 
  3. Then simply run: 然后只需运行:

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

You have to put the location of the .class files in the classpath. 您必须将.class文件的位置放在类路径中。 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. 那是来自DOS x...。

In DOS the OS looks first in the current folder. 在DOS中,操作系统首先在当前文件夹中查找。 Everywhere else the OS looks first in the path (or classpath for Java). 操作系统在其他任何地方都首先在路径(或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 如果Item在com.example.Item包中,并且类在c:\\ users \\ test \\ myproj \\ target \\ com \\ example \\ Item.class中,则必须将c:\\ users \\ test \\ myproj \\ target \\添加到类路径

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM