Java 在Junit中组合预期和超时

Java 在Junit中组合预期和超时,java,junit,Java,Junit,我正在为一个函数编写一个测试,我想把预期的和超时结合起来,我不想做全局超时 比如: 我的例子是: @Test(expected = IllegalStateException.class) @Test(timeout = 200) public void lattesRequireMilk() throws InterruptedException { // given Cafe cafe = new Cafe(); caf

我正在为一个函数编写一个测试,我想把预期的和超时结合起来,我不想做全局超时 比如:

我的例子是:

    @Test(expected = IllegalStateException.class)
    @Test(timeout = 200)
    public void lattesRequireMilk() throws InterruptedException {
        // given
        Cafe cafe = new Cafe();
        cafe.restockBeans(7);

        // when
        cafe.brew(Latte); 
        Thread.sleep(400);
    }

哦,我只是为我愚蠢的问题感到抱歉:

 @Test(expected = IllegalStateException.class,timeout = 1000)
    public void lattesRequireMilk() {
        // given
        Cafe cafe = new Cafe();
        cafe.restockBeans(7);

        // when
        cafe.brew(Latte);
    }

还要看一下ExpectedException()。您只能针对应该引发异常的行(没有误报)。
 @Test(expected = IllegalStateException.class,timeout = 1000)
    public void lattesRequireMilk() {
        // given
        Cafe cafe = new Cafe();
        cafe.restockBeans(7);

        // when
        cafe.brew(Latte);
    }