对于初学者,我不得不说,我正在使用 IntelliJ IDEA Community Edition 2020.3.1 并运行 java 15.0.1 2020-10-20,当我在启用断言并单击运行按钮后运行程序时也是如此,它按预期工作。这就是我的文件结构:
这是我的 TestRunner.java 文件中的代码:
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;
public final class TestRunner {
private static final List<Class<?>> TESTS = List.of(CalculatorTest.class);
public static void main(String[] args) throws Exception {
List<String> passed = new ArrayList<>();
List<String> failed = new ArrayList<>();
for (Class<?> klass : TESTS) {
if(!UnitTest.class.isAssignableFrom(klass)){
throw new IllegalArgumentException("Class "+ klass + " must implement UnitTest");
}
for(Method method : klass.getDeclaredMethods()){
if(method.getAnnotation(Test.class) != null){
try{
UnitTest test = (UnitTest) klass.getConstructor().newInstance();
test.beforeEachTest();
method.invoke(test);
System.out.println(method.invoke(test));
test.afterEachTest();
passed.add(getTestName(klass, method));
}catch(Throwable throwable){
failed.add(getTestName(klass, method));
}
}
}
}
System.out.println("Passed tests: " + passed);
System.out.println("FAILED tests: " + failed);
}
private static String getTestName(Class<?> klass, Method method) {
return klass.getName() + "#" + method.getName();
}
}
这是我的问题:
当我使用javac TestRunner.java
编译我的主类
TestRunner.java时,它无法找到这些符号
CalculatorTest.class
,UnitTest.class
,Test.class
,UnitTest
。这是错误消息:
当我使用
javac *.java
时,我的文件会编译并生成我的.class
文件,这是屏幕截图:
但是当我尝试使用
java TestRunner
运行我的文件时,它显示:“错误无法找到或加载类 TestRunner
,这是屏幕截图:
如果有人能帮助我解决这些问题,我会很高兴。到目前为止,我在谷歌上搜索时还没有找到解决方案。谢谢!
我解决不了。您的代码无法针对测试运行。 您的代码中的语法使用可能有问题。您可以使用错误详细信息来修复它。
公共课卧室{
private String name;
private Wall wall1;
private Wall wall2;
private Wall wall3;
private Wall wall4;
private Ceiling ceiling;
private Bed bed;
private Lamp lamp;
public Bedroom(String name, Wall wall1, Wall wall2, Wall wall3, Wall wall4, Ceiling ceiling, Bed bed, Lamp lamp) {
this.name = name;
this.wall1 = wall1;
this.wall2 = wall2;
this.wall3 = wall3;
this.wall4 = wall4;
this.ceiling = ceiling;
this.bed = bed;
this.lamp = lamp;
}
public Lamp getLamp() {
return this.lamp;
}
public void makeBed() {
System.out.print("Bedroom -> Making bed | ");
bed.make();
}
}
公开课墙{
private String direction;
public Wall(String direction) {
this.direction = direction;
}
public String getDirection() {
return direction;
}
}
公开课天花板{
private int height;
private int paintedColor;
public Ceiling(int height, int paintedColor) {
this.height = height;
this.paintedColor = paintedColor;
}
public int getHeight() {
return height;
}
public int getPaintedColor() {
return paintedColor;
}
}
公共课床位{
private String style;
private int pillows;
private int height;
private int sheets;
private int quilt;
public Bed(String style, int pillows, int height, int sheets, int quilt) {
this.style = style;
this.pillows = pillows;
this.height = height;
this.sheets = sheets;
this.quilt = quilt;
}
public void make() {
System.out.print("Bed -> Making | ");
}
public String getStyle() {
return style;
}
public int getPillows() {
return pillows;
}
public int getHeight() {
return height;
}
public int getSheets() {
return sheets;
}
public int getQuilt() {
return quilt;
}
}
公开课灯{
private String style;
private boolean battery;
private int globRating;
public Lamp(String style, boolean battery, int globRating) {
this.style = style;
this.battery = battery;
this.globRating = globRating;
}
public void turnOn() {
System.out.println("Lamp -> Turning on");
}
public String getStyle() {
return style;
}
public boolean isBattery() {
return battery;
}
public int getGlobRating() {
return globRating;
}
}