I normally try to avoid duplication and adhere to the DRY principle. However, I’m wondering about a case like this:
public class Feature { final static String FEATURE_LABEL = 'blah'; public void doSomething() { ... } ... } public class FeatureTest { ... @Test public void doSomethingShouldMakeSomethingHappen() { assertEquals(Feature.FEATURE_LABEL, feature.getSomethingHappens().getLabel()); }
If the requirement is that the the label be ‘blah’ and someone changes FEATURE_LABEL to ‘bleh’, the test will pass even though it no longer meets the requirement. Is this a valid place to violate DRY?
Yes, use a literal here.
Quoting myself from a question on literals:
Hardcoded literals should appear in unit tests for the test values, unless there is so much reuse of a value within a single test class that a local constant is useful.
The unit tests are a description of expected values without any abstraction or redirection. Imagine yourself reading the test – you want the information literally in front of you.