By providing string arguments that makes the enum instance
This is what is happening here, but without all the copy pasting needed to write it manually. Also it is far more robust in that all future extensions to the enum are covered aswell.
This one literally tests that all valid inputs are handled correctly
But I would expect there to be second test with some invalid inputs to see what from does in that case.
What is really tested when you just do Enum.fom(enum.getValue())
That both methods behave as one would expect them to behave. It is one of the cases were two different functions are best tested together. It states that they are the inverses of each other.
The correctness criterion for such functions is usually expressed as
forall v, Enum.from(v.getValue()) = v
This is the perfect Java test implementation for that proposition.
One could also make a test for the opposite direction (assuming s is a valid input):
For those thinking “I don’t see the problem” — Imagine you are doing something super common in your app, like parsing strings or ints into an enumeration as part of a REST payload, and those string values come from a JavaScript front-end and not the Java code itself. If you change the enum values in the Java code, this test will continue to pass. THIS TEST WILL NEVER FAIL. Meanwhile, unless you are somehow generating JavaScript enum values from Java (and thus tightly coupling your front end and back end releases), you just broke the front end.
A test that will never fail is a useless test, and 100% test coverage is a false god.
I think you're missing that from() and getValue() are defined and implemented by the code under test, those aren't built-in enum methods. This test fails if the implementation of those methods is not correct. That is what's under test here.
This tests whether from and getValue are inverses of each other. Sure, they should be, but that's the point of testing: checking that things that should be true are in fact, true.
it's an enum, though. at this point you're writing tests for language features.
there's also the fact that soft asserts mean that were something to break there would be no indication to the dev making changes until this was in a live environment.
fair, I was confusing the methods he calls with toString() and valueOf(), which are built ins and look an awful lot like they would do exactly what this person's methods do
True. I suspect they wrote their own because they want to be able to have the Enums to map to strings that are different from the declaration text (which could be done by overriding the native methods, but that could introduce other surprises).
23
u/Host127001 Oct 13 '21
I don't quite see the bad code here. Are these methods auto generated by the compiler? If not then this test seems reasonable
71
u/STNeto1 Oct 13 '21
softly lmao
56
u/RichCorinthian Oct 13 '21 edited Oct 13 '21
For those thinking “I don’t see the problem” — Imagine you are doing something super common in your app, like parsing strings or ints into an enumeration as part of a REST payload, and those string values come from a JavaScript front-end and not the Java code itself. If you change the enum values in the Java code, this test will continue to pass. THIS TEST WILL NEVER FAIL. Meanwhile, unless you are somehow generating JavaScript enum values from Java (and thus tightly coupling your front end and back end releases), you just broke the front end.
A test that will never fail is a useless test, and 100% test coverage is a false god.
EDIT: clarifications
24
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Oct 13 '21
Image Transcription: Code
I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!
18
u/trutheality Oct 13 '21
This tests whether
fromandgetValueare inverses of each other. Sure, they should be, but that's the point of testing: checking that things that should be true are in fact, true.2
u/bmcle071 Oct 14 '21
I dont understand the problem.
I had a class with a method .serialize and static factory method .deserialize, I tested them exactly like this.
Whats the issue?
1
u/[deleted] Oct 13 '21
SOFTLY ASSERT THAT 🤣
-1
u/Kaynee490 Oct 13 '21
Softly assert that result is equal to mepsCardStatus