I recently started using layouts (using Java mockito) in my tests. Needless to say, they simplified the installation part of the tests and together with Dependency Injection, I would say that he made the code even more reliable.
However, I found myself disconnecting during testing from implementation, and not from the specification. I ended up creating expectations, which I would say is not part of the tests. In more technical terms, I will test the interaction between the SUT (test class) and its coauthors, and such a dependency is not part of the class contract or interface!
Note that you have the following: When working with XML node, suppose you have an attributeWithDefault() method that returns the value of the node attribute, if available, otherwise it will return the default value!
I would set the test as follows:
Element e = mock(Element.class); when(e.getAttribute("attribute")).thenReturn("what"); when(e.getAttribute("other")).thenReturn(null); assertEquals(attributeWithDefault(e, "attribute", "default"), "what"); assertEquals(attributeWithDefault(e, "other", "default"), "default");
Well, I not only verified that attributeWithDefault() adheres to the specification, but I also tested the implementation since I needed to use Element.getAttribute() instead of Element.getAttributeNode().getValue() or Element.getAttributes().getNamedItem().getNodeValue() etc.
I assume that I'm going to do it wrong, so all the tips on how I can improve my use of layouts and best practices will be appreciated.
EDIT: What's wrong with the test
I made the assumption above that the test is bad style, here is my rationale.
The specification does not indicate which method is being called. The library client does not have to care about how the attribute is retrieved, for example, if this is done correctly. The developer must be fluent in order to access any of the alternative approaches, in any way he considers necessary (in terms of productivity, consistency, etc.). This is an Element specification that ensures that all of these approaches return identical values.
It makes no sense to refactor Element into a single method interface using getElement() (in fact, Go is pretty good). For ease of use, the method client should be able to use the standard Element in the standard library. Interfaces and new classes are just stupid, IMHO, because it makes the client code ugly, and it's not worth it.
Assuming that the specification remains as it is and the test remains as it is, the new developer may decide to reorganize the code to use a different approach to using the state and cause the test to fail! Well, an untrue test is true.
Having a collaborator’s state in several formats is fairly common. The specification and test should not depend on which specific approach is used; only implementation should be!
java c # unit-testing testing mocking
notnoop
source share