Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/370.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
Java 用于Karaf启动的JUnitTest_Java_Testing_Junit_Apache Karaf_Smoke Testing - Fatal编程技术网

Java 用于Karaf启动的JUnitTest

Java 用于Karaf启动的JUnitTest,java,testing,junit,apache-karaf,smoke-testing,Java,Testing,Junit,Apache Karaf,Smoke Testing,我想编写一个JUnitTest,它确保我的Karaf服务器启动良好,并且所有(需要的)包都已安装并处于活动状态 为此,我有一个测试,调用一个助手方法“AssertBundleEstate”,以确保给定的Bundle处于给定的状态。测试如下所示: @Test (timeout=30000L) public void testBundlesStarted() throws Exception { assertBundleState("bundle.Name", BundleLifec

我想编写一个JUnitTest,它确保我的Karaf服务器启动良好,并且所有(需要的)包都已安装并处于活动状态

为此,我有一个测试,调用一个助手方法“AssertBundleEstate”,以确保给定的Bundle处于给定的状态。测试如下所示:

@Test (timeout=30000L)    
public void testBundlesStarted() throws Exception {
    assertBundleState("bundle.Name", BundleLifecycleState.Active);
... <other bundles in similar way>
}
有人知道我怎样才能更好地解决这个问题吗?
谢谢

听起来好像
assertBundleState
可以成功地确定Karaf服务器是否已经启动并且“准备就绪”。但问题是,你不知道要等多久才能检查这个;如果您等待的时间不够长,则可能出现误判,如果等待的时间过长,则构建所用的时间会错误地延长

我假定您无法注册某种侦听器或回调挂钩,Karaf服务器在“准备就绪”时将调用这些侦听器或回调挂钩。事实上,即使这是可能的,您仍然必须考虑故障情况,即Karaf服务器没有启动(因此永远不能调用侦听器)

因此,我认为您只需等待Karaf服务器可用,并隐藏
Thread.sleep
调用的丑陋之处以及等待太久可能会损失的时间。你可以在这些框中打勾。例如:

@Test
public void aTest() {
    // run your test

    // this will wait until Karaf is available for at most 10 seconds
    // and will check every 100ms so if Karaf becomes available earlier 
    // than 10 seconds then the test will complete almost as soon as
    // it becomes available but if Karaf does not become available 
    // within 10 seconds then the test will fail
    await().atMost(10, SECONDS).until(karafIsAvailable());

    // assert 
    // ...
}

private Callable<Boolean> karafIsAvailable() {
    return new Callable<Boolean>() {
        public Boolean call() throws Exception {
            // return true if your condition has been met
            // e.g. assertBundleState
            return ...;
        }
    };
}
@测试
公共无效测试(){
//运行您的测试
//这将等待卡拉夫最多10秒
//并将每100毫秒检查一次,因此如果Karaf提前可用
//超过10秒后,测试几乎会尽快完成
//它可以使用,但如果卡拉夫不可用
//在10秒内,测试将失败
等待().atMost(10秒),直到(karafisavable());
//断言
// ...
}
私有可调用的可调用函数(){
返回新的可调用(){
公共布尔调用()引发异常{
//如果满足条件,则返回true
//例如,资产和不动产
返回。。。;
}
};
}
@Test
public void aTest() {
    // run your test

    // this will wait until Karaf is available for at most 10 seconds
    // and will check every 100ms so if Karaf becomes available earlier 
    // than 10 seconds then the test will complete almost as soon as
    // it becomes available but if Karaf does not become available 
    // within 10 seconds then the test will fail
    await().atMost(10, SECONDS).until(karafIsAvailable());

    // assert 
    // ...
}

private Callable<Boolean> karafIsAvailable() {
    return new Callable<Boolean>() {
        public Boolean call() throws Exception {
            // return true if your condition has been met
            // e.g. assertBundleState
            return ...;
        }
    };
}