测试 class 的参数化
Test for parameterisation of a class
在我的测试中我有这个:
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>{
}
同样,就像在 中一样,一般性评论:您为什么要测试这样的东西?如果您测试它,我宁愿确保以某种方式将静态类型检查集成到编译(或 post-编译)过程中。理想情况下,如果违反这样的约定,编译器应该抛出错误。
无论如何,不管它值多少钱,你都可以这样做(应用程序和测试 classes 都是我在另一个答案中 MCVE 的逻辑延续):
package de.scrum_master.Whosebug.q61032514;
public class TaskItem {}
package de.scrum_master.Whosebug.q61032514;
public class TreeTableCell<A, B> {}
package de.scrum_master.Whosebug.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.Whosebug.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]
}
}
如果把被测的class改成DueDateEditor extends TreeTableCell
,测试失败是这样的:
Condition not satisfied:
genericSuperclass instanceof ParameterizedType
| | |
| false interface java.lang.reflect.ParameterizedType
class de.scrum_master.Whosebug.q61032514.TreeTableCell (java.lang.Class)
如果改成DueDateEditor extends TreeTableCell<String, LocalDate>
之类的,失败是这样的:
genericSuperclass.actualTypeArguments == [TaskItem, LocalDate]
| | | | |
| | | | class java.time.LocalDate
| | | class de.scrum_master.Whosebug.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.Whosebug.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.Whosebug.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.Whosebug.q61032514.TreeTableCell<java.lang.String, java.time.LocalDate>
在我的测试中我有这个:
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>{
}
同样,就像在
无论如何,不管它值多少钱,你都可以这样做(应用程序和测试 classes 都是我在另一个答案中 MCVE 的逻辑延续):
package de.scrum_master.Whosebug.q61032514;
public class TaskItem {}
package de.scrum_master.Whosebug.q61032514;
public class TreeTableCell<A, B> {}
package de.scrum_master.Whosebug.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.Whosebug.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]
}
}
如果把被测的class改成DueDateEditor extends TreeTableCell
,测试失败是这样的:
Condition not satisfied:
genericSuperclass instanceof ParameterizedType
| | |
| false interface java.lang.reflect.ParameterizedType
class de.scrum_master.Whosebug.q61032514.TreeTableCell (java.lang.Class)
如果改成DueDateEditor extends TreeTableCell<String, LocalDate>
之类的,失败是这样的:
genericSuperclass.actualTypeArguments == [TaskItem, LocalDate]
| | | | |
| | | | class java.time.LocalDate
| | | class de.scrum_master.Whosebug.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.Whosebug.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.Whosebug.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.Whosebug.q61032514.TreeTableCell<java.lang.String, java.time.LocalDate>