A good assert class for production use? Java's equivalent of Groovy's PowerAssert?
Asked Answered
I

4

7

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.

Internship answered 7/2, 2012 at 12:28 Comment(4)
isn't it worth to write a PowerAssert class from scratch?Crimpy
@Crimpy - I started to do just that ... but I usually prefer reusing rather than reimplementing, unless there's a good reason not to.Internship
yep me too, but you wrote "the absolute minimal, zero friction"...Crimpy
@Crimpy - well, what I really meant was minimal, zero friction given that I'm willing to add a small maven dependency.Internship
A
5

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.

Anechoic answered 7/2, 2012 at 12:36 Comment(0)
C
3

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.

Covert answered 7/2, 2012 at 12:51 Comment(1)
I haven't thought about causing the program to fail if asserts aren't turned on - interesting twist. For now I'm using option 3, and waiting for some more answers / votes.Internship
D
1

Spring has one in the spring-core module, Assert


Dwightdwindle answered 7/2, 2012 at 12:33 Comment(0)
C
0

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:

Custody answered 19/1, 2016 at 19:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.