How to notate that the parameter value doesn't matter in a JUnit test?
I'm doing some TDD using JUnit4, Mockito, and Hamcrest. My current test looks like this:
FeatureFilter featureFilter = FeatureFilter .describeFeature() .addFeatureRef("myNewFeature", thatIsAvailable() .atEndpoint("/resourceWithFeature")) .andDisable(); assertThat(featureFilter.isDisabledFor("/resourceWithFeature", null, "myNewFeature"), is(true)); assertThat(featureFilter.isDisabledFor("/irrelevant", null, "myNewFeature"), is(false)); assertThat(featureFilter.isDisabledFor("/resourceWithFeature", null, "self"), is(false));
The second argument to featureFilter.isDisabledFor should be a username (String), and what I want the test to communicate is that the username is irrelevant in this case. If I were in the context of a test double I could use a Matcher such as any(String.class), but that's not an option here.
How would you, as a developer, communicate this assertion's intent?
There is a feature called JUnit Theories. @Test and Theory have a different focus on testing:
- a Test is input-output-related (on some input, expect some output)
- a Theory is algorithm related, i.e. it makes assertions on the behaviour of the called function, accepting a large set of input parameters (being representative for all possible input parameters. Often the output of such a theory is not fixed but computed.
Applied to your problem:
- Since an unused parameter does not affect the output, a test cannot assert this behavior.
- A theory with different data points can assert at least that the behavior of being user-independent holds for all user data points.