Kotlin 跨 junit 测试共享伴随对象

Kotlin shared companion object across junit tests

我有一个集成测试,我在其中启动一个嵌入式 MongoDB 作为伴随对象。我想重用这段代码,但我不确定继承是否可行(如果可能的话)。

这是一个Spring启动应用程序:

这是我的测试:

@RunWith(SpringRunner::class)
@SpringBootTest
class RequestRepositoryTest {

@Autowired lateinit var requestRepository: RequestRepository

  companion object {
    private val starter = MongodStarter.getDefaultInstance()
    private var _mongod: MongodProcess? = null
    private var _mongo: MongoClient? = null

    @BeforeClass
    @JvmStatic fun beforeTest(){
        val port = 27017
        val _mongodExe = starter.prepare(MongodConfigBuilder()
                .version(Version.Main.DEVELOPMENT)
                .net(Net("localhost", port, Network.localhostIsIPv6()))
                .build())
        _mongod = _mongodExe.start()
        _mongo = MongoClient("localhost", port)
    }

    @AfterClass
    @JvmStatic fun afterTest(){
        _mongod?.stop()
    }
  }

  @Test
  fun store() {
    val id = requestRepository.store(Request(requestId = "123"))
    assertNotNull(id)
  }

}

我的存储库class:

@Repository
class RequestRepository @Autowired constructor(val datastore: Datastore) 
{
  fun store(request : Request) : String = 
  datastore.save(request).id.toString()
}

所以我的问题是 'correct' 在 Kotlin 中解决这个问题的方法是什么。

更新编辑:作为外部对象,测试现在看起来干净多了,JUnit 外部资源可在测试中完全重用 classes:

感谢@Lovis

@RunWith(SpringRunner::class)
@SpringBootTest
class RequestRepositoryTest {

  companion object {
      @ClassRule
      @JvmField
      val mongoServer = MongoServer
  }

  @Autowired lateinit var requestRepository: RequestRepository

  @Test
  fun store() {
    val id = requestRepository.store(Request(requestId = "123"))
    assertNotNull( id )
    assertTrue { ObjectId.isValid(id) }
  }

}

您应该能够使用 jUnit 的 @ClassRuleExternalResource 实现您想要的。不需要 Kotlin 魔法 :-)

在单独的文件中定义 object

object MongoServer  : ExternalResource() {
    @Throws(Throwable::class)
    override fun before() {
        // before class
    }

    override fun after() {
       // after class
    }
}

然后在每个测试中使用它:

companion object {
    @ClassRule
    @JvmField
    val mongoServer = MongoServer
}

ClassRule 注释在这里起到了作用,companion object 是使它静态所必需的,@JvmField 注释是使字段成为 public 所必需的。这些是 jUnit 规则系统的限制。