【发布时间】:2021-06-22 05:23:55
【问题描述】:
我在测试 GRpcService 和从我的 SpringContext 获取 bean 时遇到问题。
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = {
Application.class},
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
public class MainFlowTest3 {
@Rule
public final GrpcCleanupRule grpcCleanup = new GrpcCleanupRule();
@Autowired
private RouteService routeService;
@Autowired
private RouteRequestService routeRequestService;
@Autowired
private VehicleService vehicleService;
}
当我使用 @RunWith(SpringJUnit4ClassRunner.class) 时,我在测试 grpc 时遇到问题。 我的例外是
java.lang.AbstractMethodError: Receiver class io.grpc.netty.NettyServerBuilder does not define or inherit an implementation of the resolved method abstract delegate()Lio/grpc/ServerBuilder; of abstract class io.grpc.internal.AbstractServerImplBuilder.
...
我找到了答案。我是因为我应该使用 @RunWith(JUnit4.class)。 但是当我使用它时,我所有的 Autowired bean 都是 null。
如何在我的测试中结合这两种逻辑?我需要在一个测试中同时使用 @Autowired bean 和测试 grpc 服务。
【问题讨论】: