gpt4 book ai didi

java - 测试类的参数化

转载 作者:行者123 更新时间:2023-12-02 08:46:43 25 4
gpt4 key购买 nike

在我的测试中我有这个:

given:
def dueDateCol = App.instance.fxmlController.treeTableView.columns.get( 1 )

when:
def editorCell = dueDateCol.cellFactory.call( dueDateCol )

then:
editorCell instanceof TreeTableCell<TaskItem, LocalDate>

我的应用程序代码目前是这样的:

class DueDateEditor extends TreeTableCell {
}

...但是测试通过了!我希望它在应用程序代码正确参数化之前失败:

class DueDateEditor extends TreeTableCell<TaskItem, LocalDate>{
}

最佳答案

再次,就像 my other answer 中那样,一般性评论:你为什么要测试这样的东西?如果您对其进行测试,我宁愿确保以某种方式将静态类型检查集成到编译(或后编译)过程中。理想情况下,如果违反这样的约定,编译器应该抛出错误。

无论如何,无论其值(value)如何,您都可以这样做(应用程序和测试类都是我在另一个答案中的 MCVE 的逻辑延续):

package de.scrum_master.stackoverflow.q61032514;

public class TaskItem {}
package de.scrum_master.stackoverflow.q61032514;

public class TreeTableCell<A, B> {}
package de.scrum_master.stackoverflow.q61032514;

import java.time.LocalDate;

public class DueDateEditor extends TreeTableCell<TaskItem, LocalDate> {
String text;

public boolean isEmpty() {
return text == null || text.trim() == "";
}

public void startEdit() {
if (!isEmpty())
callSuperStartEdit();
}

public void callSuperStartEdit() {}
}

现在以下测试通过:

package de.scrum_master.stackoverflow.q61032514

import spock.lang.Specification

import java.lang.reflect.ParameterizedType
import java.time.LocalDate

class DueDateEditorTest extends Specification {
def "test generic type arguments for DueDateEditor"() {
given:
def superclass = DueDateEditor.superclass
def genericSuperclass = DueDateEditor.genericSuperclass

expect:
superclass == TreeTableCell
genericSuperclass instanceof ParameterizedType
genericSuperclass.actualTypeArguments == [TaskItem, LocalDate]
// Or, if you want to avoid the underlined 'actualTypeArguments' in your IDE:
// (genericSuperclass as ParameterizedType).actualTypeArguments == [TaskItem, LocalDate]
}
}

如果将被测类更改为 DueDateEditor extends TreeTableCell ,测试失败如下:

Condition not satisfied:

genericSuperclass instanceof ParameterizedType
| | |
| false interface java.lang.reflect.ParameterizedType
class de.scrum_master.stackoverflow.q61032514.TreeTableCell (java.lang.Class)

如果您更改为类似 DueDateEditor extends TreeTableCell<String, LocalDate> 的内容,它会像这样失败:

genericSuperclass.actualTypeArguments == [TaskItem, LocalDate]
| | | | |
| | | | class java.time.LocalDate
| | | class de.scrum_master.stackoverflow.q61032514.TaskItem
| | false
| [<java.lang.Class@192d43ce cachedConstructor=null newInstanceCallerCache=null name=java.lang.String reflectionData=java.lang.ref.SoftReference@54709809 classRedefinedCount=0 genericInfo=sun.reflect.generics.repository.ClassRepository@2a2da905 enumConstants=null enumConstantDirectory=null annotationData=java.lang.Class$AnnotationData@24f360b2 annotationType=null classValueMap=[java.lang.ClassValue$Identity@39c0f4a:java.lang.ClassValue$Entry@60cf80e7]>, <java.lang.Class@38e79ae3 cachedConstructor=null newInstanceCallerCache=null name=java.time.LocalDate reflectionData=java.lang.ref.SoftReference@302fec27 classRedefinedCount=0 genericInfo=sun.reflect.generics.repository.ClassRepository@770d0ea6 enumConstants=null enumConstantDirectory=null annotationData=null annotationType=null classValueMap=[java.lang.ClassValue$Identity@39c0f4a:java.lang.ClassValue$Entry@48c40605]>]
de.scrum_master.stackoverflow.q61032514.TreeTableCell<java.lang.String, java.time.LocalDate>

或者,如果您想更好地比较错误消息中的列表,您可以使用 toList()像这样:

    genericSuperclass.actualTypeArguments.toList() == [TaskItem, LocalDate]
// Or, if you want to avoid the underlined 'actualTypeArguments' in your IDE:
//(genericSuperclass as ParameterizedType).actualTypeArguments.toList() == [TaskItem, LocalDate]

然后最后的错误消息将更改为:

Condition not satisfied:

genericSuperclass.actualTypeArguments.toList() == [TaskItem, LocalDate]
| | | | | |
| | | | | class java.time.LocalDate
| | | | class de.scrum_master.stackoverflow.q61032514.TaskItem
| | | false
| | [class java.lang.String, class java.time.LocalDate]
| [<java.lang.Class@192d43ce cachedConstructor=null newInstanceCallerCache=null name=java.lang.String reflectionData=java.lang.ref.SoftReference@60cf80e7 classRedefinedCount=0 genericInfo=sun.reflect.generics.repository.ClassRepository@302fec27 enumConstants=null enumConstantDirectory=null annotationData=java.lang.Class$AnnotationData@770d0ea6 annotationType=null classValueMap=[java.lang.ClassValue$Identity@39c0f4a:java.lang.ClassValue$Entry@48c40605]>, <java.lang.Class@2d2ffcb7 cachedConstructor=null newInstanceCallerCache=null name=java.time.LocalDate reflectionData=java.lang.ref.SoftReference@54107f42 classRedefinedCount=0 genericInfo=sun.reflect.generics.repository.ClassRepository@1b11ef33 enumConstants=null enumConstantDirectory=null annotationData=null annotationType=null classValueMap=[java.lang.ClassValue$Identity@39c0f4a:java.lang.ClassValue$Entry@476aac9]>]
de.scrum_master.stackoverflow.q61032514.TreeTableCell<java.lang.String, java.time.LocalDate>

关于java - 测试类的参数化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61031440/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com