[英]Junit 4 test inner class in kotlin
I use Junit4 and kotlin.我使用 Junit4 和 kotlin。
I use the Enclosed for the inner class test.我使用 Enclosed 进行内部类测试。
import org.junit.Test
import org.junit.experimental.runners.Enclosed
import org.junit.runner.RunWith
@RunWith(Enclosed::class)
class SampleTest {
var text = "parent class"
class Class1 {
@Test
fun `print the text`() {
println(text)
}
}
inner class Class2 {
@Test
fun `print the text`() {
println(text)
}
}
}
in Class1 and Class2, need the text
variable.在 Class1 和 Class2 中,需要text
变量。
I use the inner
for an access child class to parent class.我使用inner
来访问子类到父类。
but I have a problem, the test function is removed and I can't test that.但是我有一个问题,测试功能被删除了,我无法测试。 see the photo.看照片。
can I test the inner class in kaolin with Junit4?我可以用 Junit4 测试高岭土的内部类吗?
Class1
is a plain nested class that happens to be in SampleTest
. Class1
是一个普通的嵌套类,恰好在SampleTest
。 It's just an organisational thing, your Class1
instance doesn't have any reference to a SampleTest
instance, so it can't access text
(not without being passed an instance explicitly).这只是一个组织方面的事情,您的Class1
实例没有对SampleTest
实例的任何引用,因此它无法访问text
(除非明确传递实例)。
If you want a nested class to be able to access an enclosing instance like that, you need to mark it as inner
, like with Class2
.如果您希望嵌套类能够访问这样的封闭实例,则需要将其标记为inner
,就像Class2
。 That way you can create an instance of Class2
through an instance of SampleClass
, like这样您就可以通过SampleClass
的实例创建Class2
的实例,例如
val sample = SampleClass()
val class2 = sample.Class2()
sample.text = "wow!"
class2.`print the text`()
You can read about this stuff in the docs if it's unfamiliar如果不熟悉,您可以在文档中阅读有关这些内容的信息
So yeah, if JUnit constructs a Class1
instance, it doesn't know what text
is, since that's an instance variable in some other unrelated class.所以是的,如果 JUnit 构造一个Class1
实例,它不知道text
是什么,因为它是其他一些不相关类中的实例变量。
And I'm assuming it doesn't know how to create Class2
, since it requires a SampleTest
instance to do that.我假设它不知道如何创建Class2
,因为它需要一个SampleTest
实例来做到这一点。 All the examples for Enclosed
use static nested classes, and likethe Java docs say : Enclosed
所有示例都使用静态嵌套类,就像Java 文档所说:
A static nested class interacts with the instance members of its outer class (and other classes) just like any other top-level class.静态嵌套类与其外部类(和其他类)的实例成员交互,就像任何其他顶级类一样。 In effect, a static nested class is behaviorally a top-level class that has been nested in another top-level class for packaging convenience.实际上,静态嵌套类在行为上是一个顶层类,为了方便打包,它嵌套在另一个顶层类中。
so that's basically the same as Kotlin's (non-inner) nested classes, like your Class1
.所以这与 Kotlin 的(非内部)嵌套类基本相同,比如你的Class1
。 You probably can't use inner
classes (not to organise and automatically run tests, anyway)您可能无法使用inner
类(无论如何都不能组织和自动运行测试)
You probably want to stick text
in a companion object
if you're organising your tests in classes (or just a top-level object
outside of SampleClass
, or just in the top level of the file if you really want - just somewhere it can be accessed by any other class in a "static" way如果您在类中组织测试(或者只是SampleClass
之外的顶级object
,或者如果您真的需要,则仅在文件的顶级中 - 就在某个地方),您可能希望将text
粘贴在companion object
中任何其他类以“静态”方式访问
we can use 3 wayes我们可以使用 3 种方式
use HierarchicalContextRunner this使用 HierarchicalContextRunner 这个
use Enclosed and the companion object and don't use the inner class使用 Enclosed 和伴生对象,不要使用内部类
@RunWith(Enclosed::class) @RunWith(封闭::类)
class TEST {类测试{
companion object { var text = "parent class" } class Class1 { @Test fun `print the text`() { println(text) } } class Class2 { @Test fun `print the text`() { println(text) } }
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.