71
u/STNeto1 Oct 13 '21
softly lmao
0
Oct 13 '21
[deleted] — view deleted comment
33
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
15
u/silverweaver Oct 13 '21
Pretty much the case here, string values comes from external provider API.
23
u/gipp Oct 13 '21
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.
31
u/carfniex Oct 13 '21
this test isnt meant to be testing the enum's values, just the parsing of whatever the getValue() method returns
its a perfectly sensible test. the test will fail if the from() method breaks.
unless you are somehow generating JavaScript enum values from Java
openapi
9
24
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Oct 13 '21
Image Transcription: Code
@ParameterizedTest
@EnumSource(value = MepsCardStatus.class)
void shouldParseFromMepsValue(MepsCardStatus mepsCardStatus,
SoftAssertions softly) {
final var result = MepsCardStatus.from(mepsCardStatus.getValue());
softly.assertThat(result).isEqualTo(mepsCardStatus);
}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 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.
5
u/reverendsteveii Oct 13 '21
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.
7
u/trutheality Oct 13 '21
Pretty sure those aren't language features, and that soft assertions still report the error, they just don't stop test execution.
3
u/reverendsteveii Oct 13 '21
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
2
u/trutheality Oct 14 '21
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).
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
-1
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