Because GrpcMock uses ThreadLocal for its static API, calling stubFor() or verifyThat() from a thread different from the one that initialized the test will result in UNIMPLEMENTED errors.
To fix this, you must use an explicit GrpcMock instance instead of static methods:
Option 1: Use the instance directly
- In Spring Boot:
@Autowire the GrpcMock instance. - In other setups: Create it via
new GrpcMock.Builder()...build().start(). - Call
grpcMock.register(...) and grpcMock.verifyThat(...) directly.
Option 2: Use configureFor(instance)
- Call
configureFor(grpcMockInstance) at the start of your test method or in @BeforeEach. This allows you to continue using the static stubFor() and verifyThat() DSL.
// Option 1: Direct instance usage
@Test
void should_test_something() {
grpcMock.register(unaryMethod(SimpleServiceGrpc.getUnaryRpcMethod())
.willReturn(response));
grpcMock.verifyThat(calledMethod(SimpleServiceGrpc.getUnaryRpcMethod()).build(), CountMatcher.once());
}
// Option 2: Using configureFor
@Test
void should_test_something() {
configureFor(grpcMock);
stubFor(unaryMethod(SimpleServiceGrpc.getUnaryRpcMethod())
.willReturn(response));
}