如何正确初始化ConstraintVerifier以测试Kotlin中的Optaplanner ConstraintSt



在不使用Drools和Quarkus的情况下,如何在Kotlin中初始化ConstraintVerifier?我已经为optaplanner 8.6.0.Final添加了optaplanner测试JAR和Maven依赖项,并以以下方式进行了尝试:

var constraintVerifier : ConstraintVerifier<GroupAssignmentConstraintProvider, GroupAssignmentSolution> =
ConstraintVerifier.build(GroupAssignmentConstraintProvider(),
GroupAssignmentSolution::class.java,
GroupAssignment::class.java) 

(约束提供者:组分配约束提供者,解决方案:组分配解决方案,规划实体:组分配)Solution类获得了一组可变的学生、讲师和一些用于求解的配置(一个组的参与者/学生的最小/最大数量、最佳组大小和加权关键字)。

我的测试结果

@Test
fun lessThanMinGroupSize() {
val students = Student().generateStudents(2)
val configuration = Configuration(3, 9, 5, specialKeywords)
val lecturer = mutableSetOf(
Lecturer("4664807", mutableListOf(KeyWord("kotlin"), KeyWord("logistics"), KeyWord("java"), KeyWord("bigdata"), KeyWord("c")), mutableListOf("BIN", "BWI", "BEC"),
mutableListOf(LocalDate.parse("2021-08-05"), LocalDate.parse("2021-08-08")),
false, true))
constraintVerifier.verifyThat(GroupAssignmentConstraintProvider::minGroupSize)
.given(students, lecturer, configuration)
.penalizes()
}

使用minGroupSize约束:

fun minGroupSize(constraintFactory: ConstraintFactory): Constraint {
return constraintFactory
.from(GroupAssignment::class.java)
.groupBy(GroupAssignment::group, count())
.filter { group, number ->
number < group!!.minsize
}
.penalize("group min Conflict", ofHard(medium))
}

告诉我:(GroupAssignmentTest.kt:65表示测试中的.penalizes())

java.lang.AssertionError:期望值被打破。

约束:groupAssignment.solver/group最小冲突

分数说明(0硬/0软):

约束匹配总数:

标记:

预期的处罚,但没有。

位于org.optaplanner.test.impl.score.stream.DefaultSingleConstraintAssertion.assertMatch(DefaultSingleConstraintAssertion.java:185)网址:org.optaplanner.test.impl.score.stream.DefaultSingleConstraintAssertion.pumpes(DefaultSingleConstraintAssertion.java:82)网址:org.optaplanner.test.api.score.stream.SingleConstraintAssertion.pumines(SingleConstraintAssertion.java:121)在GroupAssignmentTest.lessThanMinGroupSize(GroupAssignmentTest.kt:65)在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)位于sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)位于java.lang.reflect.Method.ioke(Method.java:498)网址:org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:675)网址:org.junit.jupiter.engine.execulation.MethodInvocation.prough(MethodInvocation.java:60)位于org.junit.jupiter.engine.exexecution.InvocationInterceptorChain$ValidatingInvocation.procent(InvocationIntersectorChain.java:131)网址:org.junit.jupiter.engine.exextension.TimeoutExtension.incept(TimeoutExtension.java:149)网址:org.junit.jupiter.engine.exextension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:140)网址:org.junit.jupiter.engine.exextension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:84)位于org.junit.jupiter.engine.exexecution.ExecutableInvoker$ReflectiveInterceptorCallLambda$ofVoidMethod$0(ExecutableInvoker.java:115)位于org.junit.jupiter.engine.exexecution.ExecutableInvoker.lambda$invoke$0(ExecutableInvoker.java:105)在org.junit.jupiter.engine.exexecution.InvocationInterceptorChain$InterceptedInvocation.prough(InvocationIntersectorChain.java:106)位于org.junit.jupiter.engine.exexecution.InvocationInterceptorChain.proceed(InvocationIntersectorChain.java:64)位于org.junit.jupiter.engine.execulation.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:45)位于org.junit.jupiter.engine.execulation.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:37)网址:org.junit.jjupiter.engine.execulation.ExecutableInvoker.invoke(ExecutableInvoker.java:104)网址:org.junit.jupiter.engine.execulation.ExecutableInvoker.invoke(ExecutableInvoker.java:98)位于org.junit.jjupiter.engine.descriptor.TestMethodTestDescriptorLambda$invokeTestMethod$6(TestMethodTestDescriptor.java:205)网址:org.junit.platform.engine.support.hhierarchy.ThrowableCollectioner.execute(ThrowableCollector.java:73)位于org.junit.jjupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:201)位于org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.exexecute(TestMethodTestDescriptor.java:137)位于org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.exexecute(TestMethodTestDescriptor.java:71)位于org.junit.platform.engine.support.hhierarchy.NodeTestTaskLambda$executeSecurivey$5(NodeTestTask.java:135)网址:org.junit.platform.engine.support.hhierarchy.ThrowableCollectioner.execute(ThrowableCollector.java:73)位于org.junit.platform.engine.support.histrared.NodeTestTaskLambda$executeSecurive$7(NodeTestTask.java:125)网址:org.junit.platform.engine.support.histrared.Node.around(Node.java:135)位于org.junit.platform.engine.support.histrared.NodeTestTaskLambda$executeSecurive$8(NodeTestTask.java:123)网址:org.junit.platform.engine.support.hhierarchy.ThrowableCollectioner.execute(ThrowableCollector.java:73)位于org.junit.platform.engine.support.histrared.NodeTestTask.executeSecurively(NodeTestTask.java:122)在org.junit.platform.engine.support.histrared.NodeTestTask.execute(NodeTestTask.java:80)

除此之外,约束也起作用。

我想,我没有以正确的方式初始化或使用ConstraintVerifier。有人能帮我解决这个问题吗?非常感谢。

您的测试有几个问题。首先:

.given(students, lecturer, configuration)

CCD_ 2调用期望单独的实例。您正在为它提供实例集合,特别是一组可变的学生。您的约束不执行from(Set.class)或类似操作,因此它不会看到这些实例。你必须列举你的学生,或者根本不给他们,因为你的约束对学生类既没有from()也没有join()

此外,您的约束执行from(GroupAssignment.class),但我在您的given()参数中没有看到GroupAssignment。再一次,约束流没有什么可开始的,因此断言消息是正确的——没有什么可惩罚的。

最新更新