1

我正在生成类和枚举,生成的代码看起来不错。生成的枚举位于生成类的子包中。

Generated class package - com.pkg1.pkg2
Generated enum package  - com.pkg1.pkg2.enums

我有一个类必须编译的 java 测试用例。所以为此我首先编译枚举。这工作正常。现在测试用例失败了,类的 setter 方法将枚举作为输入参数。

1) Enum-Weather 的示例代码:

package com.pkg1.pkg2.enums;

public enum Weather{

SUMMER("summer"),
WINTER("winter");

public final String value;

public Weather(String value) {
    this.value = value;
}

public String getValue() {
return value;
}

}

2)一个类的示例代码-天气:

package com.pkg1.pkg2;

import com.pkg1.pkg2.enums.Weather;

public interface Weather extends CityWeather{

public void setWeather(Weather weather);

}

3)测试用例的示例代码:

@BeforeClass
public static void beforeAllTestMethods() throws Exception {

       /* First 3 lines runs without any error */
       String enumsPath = Paths.get("").toAbsolutePath().toString()+ "com/pkg1/pkg2/enums/"
       JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
       compiler.run(null,null,null,enumsPath + "Weather.java");

       /* The below code fails.
           error: package com.pkg1.pkg2.enums does not exist
           import com.pkg1.pkg2.enums.Weather;
                                      ^ 
       */
       String classPath = Paths.get("").toAbsolutePath().toString()+ "com/pkg1/pkg2/"
       JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
       compiler.run(null,null,null,classPath + "Weather.java");

请有任何建议。

4

0 回答 0