我是新来的。我正在使用JUnit5测试JavaSpark程序。下面的测试应该使用@from注解创建一个datasetgenerator为我编写的数据集。下面是测试代码:
@RunWith(JUnitQuickcheck.class)
@ExtendWith(DatasetParameterResolver.class)
public class TempDataFrameTest implements SparkSessionWrapper{
@Test
@Property(trials = 5)
public void datesAvg(@From(DatasetGenerator.class) Dataset<Temperature> temperature) {
temperature.show();
}
}
数据集生成器代码:
private String[] name = {"Dossena","Lugro","Sacilese"};
public DatasetGenerator(){
super(DatasetGenerator.class);
}
@Override
public Dataset<Temperature> generate(final SourceOfRandomness random, final GenerationStatus status){
List<Temperature> temperatureList = new ArrayList<>();
for (int i = 0; i < 4; i++){
Temperature t = new Temperature();
String mine = name[RandomInt(3,0)];
t.setMine(mine);
float temperature = RandomInt(51,-14);
t.setTemperature(temperature);
temperatureList.add(t);
}
Dataset<Temperature> temperatureDataset = spark.createDataset(temperatureList,Encoders.bean(Temperature.class));
return temperatureDataset;
}
private static int RandomInt(int max, int min){
int range = max - min;
int randValue = (int) (Math.random() * range + min);
return randValue;
}
}
这里是parameterresolver的代码:
public class DatasetParameterResolver implements ParameterResolver, SparkSessionWrapper {
@Override
public Object resolveParameter(ParameterContext parameterContext,
ExtensionContext extensionContext) throws ParameterResolutionException {
return spark.emptyDataset(Encoders.bean(Temperature.class));
}
@Override
public boolean supportsParameter(ParameterContext parameterContext,
ExtensionContext extensionContext) throws ParameterResolutionException{
return (parameterContext.getParameter().getType() == Dataset.class);
}
}
datasetgenerator返回dataset类型的变量,输入参数test是dataset。但是,当我运行测试时,出现以下错误:
java.lang.IllegalArgumentException: The generator class DatasetGenerator named in @com.pholser.junit.quickcheck.From on parameter arg0 does not produce a type-compatible object
at com.pholser.junit.quickcheck.internal.ParameterTypeContext.ensureCorrectType(ParameterTypeContext.java:280)
at com.pholser.junit.quickcheck.internal.ParameterTypeContext.addGenerators(ParameterTypeContext.java:249)
at com.pholser.junit.quickcheck.internal.ParameterTypeContext.annotate(ParameterTypeContext.java:208)
at com.pholser.junit.quickcheck.internal.PropertyParameterContext.annotate(PropertyParameterContext.java:52)
at com.pholser.junit.quickcheck.runner.PropertyStatement.parameterContextFor(PropertyStatement.java:179)
at com.pholser.junit.quickcheck.runner.PropertyStatement.lambda$evaluate$0(PropertyStatement.java:94)
at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
at java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:948)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:482)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:472)
at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:566)
at com.pholser.junit.quickcheck.runner.PropertyStatement.evaluate(PropertyStatement.java:102)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at org.junit.vintage.engine.execution.RunnerExecutor.execute(RunnerExecutor.java:43)
at java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:183)
at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
at java.util.Iterator.forEachRemaining(Iterator.java:116)
at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:482)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:472)
at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:150)
at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:173)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:485)
at org.junit.vintage.engine.VintageTestEngine.executeAllChildren(VintageTestEngine.java:82)
at org.junit.vintage.engine.VintageTestEngine.execute(VintageTestEngine.java:73)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:108)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:88)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.lambda$execute$0(EngineExecutionOrchestrator.java:54)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.withInterceptedStreams(EngineExecutionOrchestrator.java:67)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:52)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:96)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:75)
at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:71)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:220)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:53)
Process finished with exit code 255
有人能帮我吗?
暂无答案!
目前还没有任何答案,快来回答吧!