Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Kotlin GrpcServerRule找不到实现_Kotlin_Grpc_Grpc Java - Fatal编程技术网

Kotlin GrpcServerRule找不到实现

Kotlin GrpcServerRule找不到实现,kotlin,grpc,grpc-java,Kotlin,Grpc,Grpc Java,我正在尝试为GRPC服务之一编写一些单元测试 我正在使用GrpcServerRule来执行此操作 一旦我运行了测试,服务的实现似乎没有正确加载。我不知道为什么 下面是测试的代码 @RunWith(JUnit4::class) class CreateListTest { // Workaround for public @get:Rule val serverRule: GrpcServerRule = GrpcServerRule().directExecutor() @Before f

我正在尝试为GRPC服务之一编写一些单元测试

我正在使用GrpcServerRule来执行此操作

一旦我运行了测试,服务的实现似乎没有正确加载。我不知道为什么

下面是测试的代码

@RunWith(JUnit4::class)
class CreateListTest {

// Workaround for public
@get:Rule  val serverRule: GrpcServerRule = GrpcServerRule().directExecutor()

@Before
fun setup() {
    serverRule.serviceRegistry.addService(ShopperServerImplementation())
}


@Test
fun testCreateListValidRequest() {
    val request = CreateListRequest.newBuilder()
            .setName("a test list")
            .setAuthorId(25)
            .addAuthorizedUsers(12)
            .build()
    val reply = ShopperServiceGrpc.newBlockingStub(serverRule.channel).createList(request)
    assertEquals(45, reply.createdListId)
}

}
下面是服务实现的代码

class ShopperServerImplementation : ShopperGRPCGrpc.ShopperGRPCImplBase() {
override fun createList(request: CreateListRequest, responseObserver: StreamObserver<CreateListReply>) {
    val reply = CreateListReply.newBuilder().setCreatedListId(80).build()
    responseObserver.onNext(reply)
    responseObserver.onCompleted()
}
}

提前感谢您的帮助

看来对项目的彻底清理使一切都按预期运行。不知道会发生什么。如果我再次遇到问题,将进行调查并将其发布到github上。

尝试使用
@JvmField
注释作为规则字段。您还可能遇到gRPC服务未正确清理的问题。因此,另一种方法是使用一个带有清理规则的单独类,例如:

class LoopbackServer {
    val client: ShopperServiceGrpc.ShopperServiceBlockingStub
    private val yourService = ShopperServiceController()

    @Rule
    @JvmField
    val grpcCleanup = GrpcCleanupRule()

    init {
        // Generate a unique in-process server name.
        val serverName = InProcessServerBuilder.generateName()

        // Create a server, add service, start, and register for automatic graceful shutdown.
        grpcCleanup.register(InProcessServerBuilder.forName(serverName).directExecutor()
                .addService(yourService).build().start())

        client = ShopperServiceGrpc.newBlockingStub(
                // Create a client channel and register for automatic graceful shutdown.
                grpcCleanup.register(InProcessChannelBuilder.forName(serverName).directExecutor().build()))
    }
}
然后,您可以在测试类中引用:

class CreateListTest {
    private val server = LoopbackServer()
    private val client = server.client

    @Test
    fun `When service called with a valid List request, it succeeds`() {
        val request = CreateListRequest.newBuilder()
            .setName("a test list")
            .setAuthorId(25)
            .addAuthorizedUsers(12)
            .build()
        val reply = client.createList(request)
        assertEquals(45, reply.createdListId)
    }

}

我不认识科特林。但这似乎应该行得通。也许是Kotlin与JUnit的特定互动?是的,我也不知道。我用java编写测试只是为了尝试一下。真正的服务器和客户端都在Kotlin中工作。JAVA也没有带来任何好运。服务器仍在Kotlin中,因此它可能仍然意味着一些可移植性问题请查看我的回答,因为您的解决方案可能会遇到间歇性问题--测试完成后,它可能无法正确释放资源。您可能还希望查看将Kotlin测试与JUnit 5而不是直接与JUnit 4一起使用,支持新扩展系统(取代RunWith)的较新框架版本有以下优点:
class CreateListTest {
    private val server = LoopbackServer()
    private val client = server.client

    @Test
    fun `When service called with a valid List request, it succeeds`() {
        val request = CreateListRequest.newBuilder()
            .setName("a test list")
            .setAuthorId(25)
            .addAuthorizedUsers(12)
            .build()
        val reply = client.createList(request)
        assertEquals(45, reply.createdListId)
    }

}