适合生产使用的良好断言类吗? Java 相当于 Groovy 的 PowerAssert?

发布于 2025-01-02 20:08:58 字数 756 浏览 1 评论 0原文

我不喜欢 java assert 关键字,因为它并不总是在生产代码中启用。我正在寻找一个好的“ProductionAssert”类来使用,它总是运行所指出的断言。

其中一个候选者是 Guava 的先决条件< /a>.它很不错,但有点受限(例如没有 assertEquals()assertNull()assertGreaterEquals())。

一种替代方案是包含 jUnit 或另一个测试框架...但我不愿意仅仅为了一个简单的断言类而依赖整个测试框架。

如果我在 Groovy 中编程,我会使用

Java 是否有一个好的“ProductionAssert”类?

PS - 一种选择是最终查看类似 Java Contracts< /a> ...但我现在正在寻找的是绝对最小的、零摩擦的、只需将其放入而不需要对构建过程类型进行任何更改...我不确定合同是否适合描述。

I don't like the java assert keyword, because it is not always enabled in production code. I am looking for a good "ProductionAssert" class to use, that always runs the noted assertions.

One candidate is Guava's Preconditions. It's decent, but a bit limited (e.g. no assertEquals(), assertNull(), assertGreaterEquals()).

One alternative is including jUnit or another test framework ... but I'm reluctant to depend upon an entire testing framework just for a simple assert class.

If I were programming in Groovy, I would use PowerAssert.

Is there a good "ProductionAssert" class for Java?

P.S. - one option is to finally check out something like Java Contracts ... but what I'm looking for right now it the absolute minimal, zero friction, just drop-it-in without any changes in the build process kind of class ... I'm not sure contracts fits that description.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

甜心 2025-01-09 20:08:58

我倾向于使用 Spring 的 Assert< /a> class:

public void thing(String foo){
    Assert.hasText(foo, "'foo' is required");
}

显然,如果你不使用 spring 那么这不会让你的船漂浮,我不确定它比番石榴好得多。

I tend to use Spring's Assert class:

public void thing(String foo){
    Assert.hasText(foo, "'foo' is required");
}

Obviously if your not using spring then this isn't going to float your boat and I'm not sure it is much better than guava one.

在风中等你 2025-01-09 20:08:58

我会使用 Junit。它旨在使用这些测试。

另一种选择是确保断言始终打开。即如果您无法控制您的生产环境。如果不是,您可能会导致程序失败。

boolean assertOn = false;
assert assertOn = true;
if (!assertOn) 
   throw new AssertionError("Assertions must be turned on");

第三种选择是自己编写这些方法。通常只有两行代码。这样他们就会做你想做的一切。

I would use Junit. Its designed to use these tests.

Another option is to ensure asserts are always turned on. i.e. If you can't control your production environment. You can cause the program to fail if they are not.

boolean assertOn = false;
assert assertOn = true;
if (!assertOn) 
   throw new AssertionError("Assertions must be turned on");

A third option is to write these methods yourself. There are usually just two lines of code. That way they will do everything you want.

深府石板幽径 2025-01-09 20:08:58

Spring 在 spring-core 模块中有一个 断言


Spring has one in the spring-core module, Assert


久而酒知 2025-01-09 20:08:58

您可以将 valid4j 与 hamcrest-matchers 一起使用(在 Maven Central 上找到为 org.valid4j:valid4j)。默认提供程序会抛出 AssertionError,但如果需要,您可以注册自己的自定义全局策略。

对于前置条件(实际上就像断言):

import static org.valid4j.Assertive.*;

require(x, greaterThan(0)); // throws RequireViolation extends AssertionError

使用“ensure”对后置条件的类似支持。

旁注:您也可以使用相同的库进行常规输入验证(即抛出可恢复的异常):

import static org.valid4j.Validation.*;

validate(argument, isValid(), otherwiseThrowing(InvalidException.class));

链接:

You can use valid4j with hamcrest-matchers (found on Maven Central as org.valid4j:valid4j). The default provider throws AssertionError, but you are able to register your own customized global policy if needed.

For preconditions (like assertions really):

import static org.valid4j.Assertive.*;

require(x, greaterThan(0)); // throws RequireViolation extends AssertionError

Similar support for postconditions using 'ensure'.

On a side-note: You can use the same library for regular input validation as well (i.e. throwing recoverable exceptions):

import static org.valid4j.Validation.*;

validate(argument, isValid(), otherwiseThrowing(InvalidException.class));

Links:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文