检查JUnit Extension抛出特定的异常 [英] Check that JUnit Extension throws specific Exception

查看:96
本文介绍了检查JUnit Extension抛出特定的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我开发了一个扩展,该扩展不允许测试方法的名称以大写字母开头.

Suppose I develop an extension which disallows test method names to start with an uppercase character.

public class DisallowUppercaseLetterAtBeginning implements BeforeEachCallback {

    @Override
    public void beforeEach(ExtensionContext context) {
        char c = context.getRequiredTestMethod().getName().charAt(0);
        if (Character.isUpperCase(c)) {
            throw new RuntimeException("test method names should start with lowercase.");
        }
    }
}

现在,我想测试我的扩展程序是否按预期工作.

Now I want to test that my extension works as expected.

@ExtendWith(DisallowUppercaseLetterAtBeginning.class)
class MyTest {

    @Test
    void validTest() {
    }

    @Test
    void TestShouldNotBeCalled() {
        fail("test should have failed before");
    }
}

如何编写测试以验证尝试执行第二种方法是否抛出带有特定消息的RuntimeException?

How can I write a test to verify that the attempt to execute the second method throws a RuntimeException with a specific message?

推荐答案

尝试了答案中的解决方案和注释中链接的问题后,我最终使用了JUnit Platform Launcher解决方案.

After trying the solutions in the answers and the question linked in the comments, I ended up with a solution using the JUnit Platform Launcher.

class DisallowUppercaseLetterAtBeginningTest {

    @Test
    void should_succeed_if_method_name_starts_with_lower_case() {
        TestExecutionSummary summary = runTestMethod(MyTest.class, "validTest");

        assertThat(summary.getTestsSucceededCount()).isEqualTo(1);
    }

    @Test
    void should_fail_if_method_name_starts_with_upper_case() {
        TestExecutionSummary summary = runTestMethod(MyTest.class, "InvalidTest");

        assertThat(summary.getTestsFailedCount()).isEqualTo(1);
        assertThat(summary.getFailures().get(0).getException())
                .isInstanceOf(RuntimeException.class)
                .hasMessage("test method names should start with lowercase.");
    }

    private TestExecutionSummary runTestMethod(Class<?> testClass, String methodName) {
        SummaryGeneratingListener listener = new SummaryGeneratingListener();

        LauncherDiscoveryRequest request = request().selectors(selectMethod(testClass, methodName)).build();
        LauncherFactory.create().execute(request, listener);

        return listener.getSummary();
    }

    @ExtendWith(DisallowUppercaseLetterAtBeginning.class)
    static class MyTest {

        @Test
        void validTest() {
        }

        @Test
        void InvalidTest() {
            fail("test should have failed before");
        }
    }
}

JUnit本身不会运行MyTest,因为它是没有@Nested的内部类.因此,在构建过程中不会有失败的测试.

JUnit itself will not run MyTest because it is an inner class without @Nested. So there are no failing tests during the build process.

JUnit本身不会运行MyTest,因为它是没有@Nested的内部类.因此,在构建过程中不会有失败的测试.

JUnit itself will not run MyTest because it is an inner class without @Nested. So there are no failing tests during the build process.

这不是完全正确的. JUnit本身也可以运行MyTest,例如如果在IDE或Gradle构建中启动了运行所有测试".

This is not completly correct. JUnit itself would also run MyTest, e.g. if "Run All Tests" is started within the IDE or within a Gradle build.

之所以没有执行MyTest的原因是因为我使用了Maven并用mvn test对其进行了测试. Maven使用Maven Surefire插件执行测试.此插件具有默认配置,其中排除所有嵌套类,例如MyTest.

The reason why MyTest was not executed is because I used Maven and I tested it with mvn test. Maven uses the Maven Surefire Plugin to execute tests. This plugin has a default configuration which excludes all nested classes like MyTest.

另请参阅此答案有关通过Maven从内部类运行测试"以及注释中的链接问题.

See also this answer about "Run tests from inner classes via Maven" and the linked issues in the comments.

这篇关于检查JUnit Extension抛出特定的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆