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.");
}
}
}
现在我想测试我的扩展按预期工作.
@ExtendWith(disallowUppercaseLetteratBeginning.class)
class MyTest {
@Test
void validtest() {
}
@Test
void TestShouldNotBeCalled() {
fail("test should have Failed before");
}
}
如何编写测试以验证执行第二个方法的尝试是否会抛出带有特定消息的RuntimeException?
最佳答案
另一种方法可能是使用新的JUnit 5-Jupiter框架提供的工具.
我把我在Eclipse Oxygen上用Java 1.8测试过的代码放在下面.代码缺乏优雅和简洁,但有望成为为元测试用例构建强大解决方案的基础.
请注意,这实际上是JUnit 5的测试方法,我推荐你the unit tests of the Jupiter engine on Github.
public final class disallowUppercaseLetteratBeginningTest {
@Test
void testIt() {
// Warning here: I checked the test container created below will
// execute on the same thread as used for this test. We should remain
// careful though,as the map used here is not thread-safe.
final Maptest()) {
events.put(descriptor.getdisplayName(),result);
}
// skip class and container reports
}
@Override
public void reportingEntryPublished(TestDescriptor testDescriptor,ReportEntry entry) {}
@Override
public void executionStarted(TestDescriptor testDescriptor) {}
@Override
public void executionSkipped(TestDescriptor testDescriptor,String reason) {}
@Override
public void dynamicTestRegistered(TestDescriptor testDescriptor) {}
};
// Build our test container and use Jupiter fluent API to launch our test. The following static imports are assumed:
//
// import static org.junit.platform.engine.discovery.discoverySelectors.selectClass
// import static org.junit.platform.launcher.core.LauncherdiscoveryRequestBuilder.request
JupiterTestEngine engine = new JupiterTestEngine();
LauncherdiscoveryRequest request = request().selectors(selectClass(MyTest.class)).build();
TestDescriptor td = engine.discover(request,UniqueId.forEngine(engine.getId()));
engine.execute(new ExecutionRequest(td,listener,request.getConfigurationParameters()));
// Bunch of verbose assertions,should be refactored and simplified in real code.
assertEquals(new HashSet<>(asList("validtest()","TestShouldNotBeCalled()")),events.keySet());
assertEquals(Status.SUCCESSFUL,events.get("validtest()").getStatus());
assertEquals(Status.Failed,events.get("TestShouldNotBeCalled()").getStatus());
Throwable t = events.get("TestShouldNotBeCalled()").getThrowable().get();
assertEquals(RuntimeException.class,t.getClass());
assertEquals("test method names should start with lowercase.",t.getMessage());
}
虽然有点冗长,但这种方法的一个优点是它不需要在同一个JUnit容器中进行模拟和执行测试,这将在以后用于实际单元测试.
通过一些清理,可以实现更易读的代码.同样,JUnit-Jupiter资源可以成为灵感的重要来源.
原文地址:https://www.jb51.cc/java/437450.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。