简体   繁体   English

vertx junit 配置面临问题,vertx.deployVerticle 中的错误,如何在部署测试中设置 spring verticalfactory

[英]Facing issue with vertx junit configuratoin , error in vertx.deployVerticle , How to setup spring verticalfactory in deployment test

I want to test particular REST APIs written in vertx with spring boot.我想用 spring 引导测试用 vertx 编写的特定 REST API。

below is test class下面是测试 class

@RunWith(VertxUnitRunner.class)
public class sampleTest {
    private Vertx vertx;
    private Integer port;

    @Before
    public void setUp(TestContext context) throws IOException {
        vertx = Vertx.vertx();

        ServerSocket socket = new ServerSocket(0);
        port = socket.getLocalPort();
        socket.close();

        DeploymentOptions options = new DeploymentOptions()
                .setConfig(new JsonObject().put("http.port", port)
                );
        vertx.deployVerticle(sampleVerticle.class.getName(), options, context.asyncAssertSuccess());
    }

    @After
    public void tearDown(TestContext context) {
        vertx.close(context.asyncAssertSuccess());
    }

    @Test
    public void givenId_whenReceivedArticle_thenSuccess(TestContext testContext) {
        final Async async = testContext.async();

        vertx.createHttpClient()
                .getNow(port, "localhost", "/tracking/id/12345/12345/12345", response -> {
                    response.handler(responseBody -> {
                        testContext.assertTrue(responseBody.toString()
                                .contains("\"articalId\" : \"12345\""));
                        testContext.assertTrue(responseBody.toString()
                                .contains("\"pageId\" : \"12345\""));
                        testContext.assertTrue(responseBody.toString()
                                .contains("\"eventId\" : \"12345\""));
                        async.complete();
                    });
                });
    }

}

here im just checking url parameter.这里我只是检查 url 参数。 Now when i run above test class it throws error现在,当我在测试 class 上面运行时,它会抛出错误

java.lang.InstantiationException: com.articals.sampleVerticle java.lang.InstantiationException: com.articals.sampleVerticle

Below is sampleVerticle class where multiple routers are handled,下面是处理多个路由器的 sampleVerticle class,

@Component
@Slf4j
@Scope(SCOPE_PROTOTYPE)
class sampleVerticle extends AbstractVerticle {

    private final Router router
    private final ArticalHandler articalHandler
    private final FailureHandler failureHandler
    private final MetricsHandler metricsHandler

    ClickVerticle(
            Router router,
            ArticalHandler articalHandler,
            FailureHandler failureHandler,
            MetricsHandler metricsHandler
    )
    {
        this.articalHandler= articalHandler
        this.router = router
        this.failureHandler = failureHandler
        this.metricsHandler = metricsHandler
    }


    @Override
    void start() throws Exception {

        router.get('/tracking/id/:articalId/:pageId/:eventId')
              .handler(articalHandler)
              .failureHandler(failureHandler)


        router.get('/metrics')
              .handler(metricsHandler)
              .failureHandler(failureHandler)

    }
}

If i include default constructor then i error如果我包含默认构造函数,那么我会出错

Cannot invoke method get() on null object无法在 null object 上调用方法 get()

I am following this example for unit testing https://www.baeldung.com/vertx我正在按照此示例进行单元测试https://www.baeldung.com/vertx

Please help me with above issue.请帮我解决上述问题。

Thanks & Regards感谢和问候

When you call vertx.deploy providing a class name, Vert.x expects the verticle class to have a no-arg constructor.当您调用vertx.deploy提供 class 名称时,Vert.x 期望垂直 class 具有无参数构造函数。

This is why the test fails with InstantiationException .这就是测试因InstantiationException而失败的原因。

There is a vertx.deploy overload that takes a verticle object instance.有一个vertx.deploy重载,它采用一个 verticle object 实例。 You should create your instance first and then use this method:您应该先创建实例,然后使用此方法:

sampleVerticle verticle = new sampleVerticle(router, handler1, ...)
vertx.deploy(verticle, options, context.asyncAssertSuccess());

To create the required router and handlers, you can either mock them (eg with Mockito) or start an embedded Spring application context.要创建所需的路由器和处理程序,您可以模拟它们(例如使用 Mockito)或启动嵌入式 Spring 应用程序上下文。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM