单元测试Verticle部署

时间:2017-06-13 17:26:05

标签: java junit rx-java vert.x

我有一个简单的Verticle,它从属性文件读取配置并加载到vertx配置中。我编写了一个单元测试来测试这个Verticle的部署,测试失败的可能原因是该位置的属性文件不可用。

当我运行测试时,单元测试通过,无论我是否更改属性文件名或路径,处理程序说Verticle已成功部署。

我在这里做错了吗?以下是我的代码

import io.vertx.config.ConfigRetrieverOptions;
import io.vertx.config.ConfigStoreOptions;
import io.vertx.core.DeploymentOptions;
import io.vertx.core.json.JsonObject;
import io.vertx.rxjava.config.ConfigRetriever;
import io.vertx.rxjava.core.AbstractVerticle;


/**
 * This is the main launcher verticle, the following operations will be executed in start() method of this verticle:
 * 1. Read configurations from application.properties file
 * 2. Deploy all other verticles in the application
 */
public class LauncherVerticle extends AbstractVerticle {


    @Override
    public void start() throws Exception {

        //set up configuration from the properties file
        ConfigStoreOptions fileStore = new ConfigStoreOptions()
                .setType("file")
                .setFormat("properties")
                .setConfig(new JsonObject().put("path", System.getProperty("vertex.config.path"));

        //create config retriever options add properties to filestore
        ConfigRetrieverOptions options = new ConfigRetrieverOptions().addStore(fileStore);
        ConfigRetriever configRetriever = ConfigRetriever.create(vertx, options);

        DeploymentOptions deploymentOptions = new DeploymentOptions();

        //Deploy verticles after the config has been loaded
        //The configurations are loaded into JsonConfig object
        //This JsonConfig object can be accessed in other verticles using the config() method.
        configRetriever.rxGetConfig().subscribe(s -> {

            //pass on the JsonConfig object to other verticles through deployment options
            deploymentOptions.setConfig(s);
            vertx.deployVerticle(AnotherVerticle.class.getName(), deploymentOptions);

        }, e -> {
            log.error("Failed to start application : " + e.getMessage(), e);
            try {
                stop();
            } catch (Exception e1) {
                log.error("Unable to stop vertx, terminate the process manually : "+e1.getMessage(), e1);
            }
        });
    }
}

这是我的单元测试

import io.vertx.ext.unit.TestContext;
import io.vertx.ext.unit.junit.VertxUnitRunner;
import io.vertx.rxjava.core.Vertx;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import rx.Single;


@RunWith(VertxUnitRunner.class)
public class LoadConfigurationTest {


    /**
     * Config should be loaded successfully
     *
     * @param context
     */
    @Test
    public void loadConfigTest(TestContext context) {
        /*
         * Set the system property "vertx.config.path" with value "application.properties"
         * This system property will be used in the Launcher verticle to read the config file
         */
        System.setProperty("vertx.config.path", "/opt/vertx/config/application.properties");

        //create vertx instance
        Vertx vertx = Vertx.vertx();

        Single<String> single = vertx.rxDeployVerticle(LauncherVerticle.class.getName());
        single.subscribe(s -> {
            vertx.rxUndeploy(s);
        }, e -> {
            Assert.fail(e.getMessage());
        });

    }

    /**
     * Test for negative use case - file not available in the specified location
     *
     * @param context
     */
    @Test
    public void loadConfigFailTest(TestContext context) {

         //set path = non existing path
        System.setProperty("vertx.config.path", "/non/existing/path/application.properties");

        //create vertx instance
        Vertx vertx = Vertx.vertx();

        Single single = vertx.rxDeployVerticle(LauncherVerticle.class.getName());

        single.subscribe(s -> {
            //not executing this statement
            Assert.fail("Was expecting error but Verticle deployed successfully");
        }, e -> {
            //not executing this statement either
            System.out.println("pass");
        });
    }
}

2 个答案:

答案 0 :(得分:2)

您可以在LauncherVerticle内尝试以下代码吗?只有使用AbstractVerticles startFuture一起使用,这是一种处理配置加载和周围一切的简洁方法在你的明星期间也一样。

public class LauncherVerticle extends AbstractVerticle {
@Override
public void start(Future<Void> startFuture) throws Exception {
        ConfigStoreOptions fileStore = new ConfigStoreOptions()
                .setType("file")
                .setFormat("properties")
                .setConfig(new JsonObject().put("path", System.getProperty("vertex.config.path")));

        ConfigRetrieverOptions options = new ConfigRetrieverOptions().addStore(fileStore);
        ConfigRetriever configRetriever = ConfigRetriever.create(vertx, options);

        DeploymentOptions deploymentOptions = new DeploymentOptions();
        configRetriever.rxGetConfig().subscribe(s -> {
                    deploymentOptions.setConfig(s);
                    vertx.deployVerticle(AnotherVerticle.class.getName(),
                            deploymentOptions,
                            result -> startFuture.complete()
                    );
                },
                startFuture::fail
        );
    }
}

startFuture,可以帮助您控制垂直加载的状态。

还要记住@Constantine处理测试的方法是最好的方法,使用Async来阻止你的测试通过,而不会实际断言。

答案 1 :(得分:1)

似乎你的垂直没有问题。但是,测试中有一些东西 - 不考虑垂直部署的异步性质。这些测试方法立即完成,而不是等待Verticle部署,而不导致AssertionError的JUnit测试是通过测试。您必须使用Async明确表示完成。

请参阅下面的负面情况示例:

import io.vertx.ext.unit.Async;
import io.vertx.ext.unit.TestContext;
import io.vertx.ext.unit.junit.RunTestOnContext;
import io.vertx.ext.unit.junit.VertxUnitRunner;
import io.vertx.rxjava.core.Vertx;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(VertxUnitRunner.class)
public class LoadConfigurationTest {

    @Rule
    public RunTestOnContext runTestOnContextRule = new RunTestOnContext();

    @Test
    public void testConfigLoading_shouldFail_whenConfigDoesNotExist(TestContext context) {
        // create an Async instance that controls the completion of the test
        Async async = context.async();

        // set non existing path
        System.setProperty("vertx.config.path", "/non/existing/path/application.properties");

        // take vertx instance and wrap it with rx-ified version
        Vertx vertx = Vertx.newInstance(runTestOnContextRule.vertx());

        vertx.rxDeployVerticle(LauncherVerticle.class.getName()).subscribe(s -> {
            context.fail("Was expecting error but Verticle deployed successfully"); // failure
        }, e -> {
            async.complete(); // success
        });
    }
}

另请注意,您可以从Vertx规则中获取RunTestOnContext个实例(如上面的代码段所示)。