I have the following test code which checks the content of a particular directory structure
assertThat(install_should_not_fail).isDirectory().satisfies(isnf -> {
assertThat(new File(isnf, "maven-metadata-local.xml")).isNotEmpty();
assertThat(new File(isnf, "1.0")).isDirectory().satisfies(v10 -> {
assertThat(v10).satisfies(file -> {
assertThat(new File(file, "install_should_not_fail-1.0.jar")).isNotEmpty();
assertThat(new File(file, "install_should_not_fail-1.0.pom")).isNotEmpty();
assertThat(new File(file, "_remote.repositories")).isNotEmpty();
});
});
});
and I have several tests which do very similar things. The question is: Can that be made simpler (I know I could refactor out a method which contains exactly that code) but I'm interested more in getting the above code simpler using AssertJ if it's possible?
I would suggest an approach that does not use satisfies to avoid the nested blocks:
assertThat(install_should_not_fail).isDirectory();
assertThat(new File(install_should_not_fail, "maven-metadata-local.xml")).isNotEmpty();
File v10 = new File(install_should_not_fail, "1.0");
assertThat(v10).isDirectory();
assertThat(new File(v10, "install_should_not_fail-1.0.jar")).isNotEmpty();
assertThat(new File(v10, "install_should_not_fail-1.0.pom")).isNotEmpty();
assertThat(new File(v10, "_remote.repositories")).isNotEmpty();
I feel this makes the code a bit lighter and more readable (to me at least).
Moreover, I think this is a good candidate for soft assertions so that all the assertions errors are reported instead of failing at the first one.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With