junit - 测试不会运行并收到大量错误

时间:2017-05-17 19:39:42

标签: java ruby testing junit

我已经下载了在不同计算机上构建的旧项目。我在当前安装了Java环境。我还下载了junit以使工作正常。当我使用build.rb和run_test.rb运行我的测试 - 以前工作的文件时,我收到很多错误,否定了整个代码。这是示例

$ ruby build.rb
shop_basket\Cashdesk.java:4: error: class CashDesk is public, should be 
declared in a file named CashDesk.java
public class CashDesk{
   ^
1 error
shop_basketSpec\CashdeskTest.java:5: error: class CashDeskTest is public, 
should be declared in a file named CashDeskTest.java
public class CashDeskTest{
   ^
shop_basketSpec\BasketTest.java:2: error: package org.junit does not exist
import org.junit.*;
^
shop_basketSpec\BasketTest.java:3: error: package org.junit does not exist
import static org.junit.Assert.*;
                   ^
shop_basketSpec\CashdeskTest.java:2: error: package org.junit does not exist
import org.junit.*;
^
shop_basketSpec\CashdeskTest.java:3: error: package org.junit does not exist
import static org.junit.Assert.*;
                   ^
shop_basketSpec\CashdeskTest.java:7: error: cannot find symbol
CashDesk cashdesk;
 ^
symbol:   class CashDesk
location: class CashDeskTest
shop_basketSpec\CustomerTest.java:2: error: package org.junit does not exist
import org.junit.*;
^
 shop_basketSpec\CustomerTest.java:3: error: package org.junit does not 
 exist
 import static org.junit.Assert.*;   ^
 shop_basketSpec\ProductTest.java:2: error: package org.junit does not exist
 import org.junit.*;
  ^

并且有更多这样的错误,好像它们涉及整个代码结构。我不明白为什么。 所有CLASSPATH等似乎都在我的Windows操作系统上设置。整个事情都令人恼火,因为我不能随着我的编码而移动。谢谢你的帮助

这是我的设置CLASSPATH CLASSPATH image

我的ruby文件的内容

run_tests.rb

require 'find'

def find_valid_files
  files = []
  Find.find('bin') do |path|
  files << path if path.include?(".class") && path.include?("Test")
end
return files
end

def run_tests(files)
  for file in files
   fileName = File.basename(file, ".*")
   puts "Running #{fileName}"

  system("java org.junit.runner.JUnitCore #{fileName}")
 end
 end

valid_files = find_valid_files()

Dir.chdir "bin"

run_tests(valid_files)

build.rb

require 'fileutils'

def filter_directories

  excluded_directories = ["bin"]

  all_files = Dir.glob('*')

 return all_files.select do |file|
   next if excluded_directories.include?(file)
   File.directory?(file)
 end
end

def create_bin
  FileUtils.rm_rf('bin')
  FileUtils.mkdir_p('bin')
end

def run_tests directories
  for directory in directories 
    puts "building #{directory}"
   system("javac -d bin #{directory}/*.java")
  end
end

create_bin()

valid_directories = filter_directories()

run_tests(valid_directories)

2 个答案:

答案 0 :(得分:0)

对于与CashDesk和CashDeskTest相关的前两个错误 - 您的文件名具有小写的“d”(Cashdesk.java和CashdeskTest.java)。文件名中的大小写应与代码中声明的实际类名匹配。修复它。

如果之后仍然看到JUnit错误,请检查类路径上是否有JUnit JAR。

答案 1 :(得分:0)

错误Cashdesk.java:4: error: class CashDesk is public, should be declared in a file named CashDesk.java是由.java文件名与其包含的公共类之间的不一致引起的。在这个exaple类中,CashDesk保存在Cashdesk.java文件中 - 注释d与D在桌面上。

第二组错误error: package org.junit does not exist意味着您应该将JUnit库包含到项目的类路径中