Thanks, Peter, I’m glad you liked my article.
Taking my best guess at what your example does, it looks like there are a few misconceptions:
1. Nullable Infrastructure is a pattern used within production code to make that code testable. It looks like you’re trying to apply it to test code.
2. I only use the Nullable Infrastructure pattern on “Infrastructure” objects, which are specifically wrappers/adapters for external systems and state. In your example, I’m not sure where the external system is. I’m guessing it’s “SlowLDAPImplementationAdapter.”
Taking my best guess at what this example is supposed to do, I would rewrite ShopShould as follows:
@Test void
calculateTotalPrice_ViaCalculator() {
? Shop testee = new Shop(
? ? RebateServiceAdapter.createNull(),
? ??//?‘1’ is the desired response from the StateTaxesServiceAdapter.
? ? // A real system would have more sophisticated response configuration.
? ? StateTaxesServiceAdapter.createNull(1),
? ? // NotificationServiceAdapter might use SlowLDAPImplementationAdapter, via
? ? // composition rather than inheritance, under the hood.
? ? NotificationServiceAdapter.createNull()
? );
? double totalPrice = testee.getTotalPrice(1, 2,?“any ignored”);
? assertThat(totalPrice).isEqual(2);
}
All the classes inside ShopShould would go away, as would the Port interfaces. The various Adapters would get an Embedded Stub to support their new createNull() methods.
Technically, you could keep the Port interfaces, if you wanted, but I find them to be unnecessary noise, except in the rare case that they actually have multiple production implementations. (Even then, I prefer to abstract the multiple implementations behind a higher-level adapter that uses composition and has the “smarts” to delegate to the appropriate service.) They aren’t needed for testing purposes when using Nullable Infrastructure.
(As an aside, my naming would typically be “RebateServiceClient,” not “RebateServiceAdapter,” but I wanted to stay close to your naming convention.)
Cheers,
James
On Mar 26, 2021, at 3:16 AM, Peter Gfader <
peter@...> wrote:
Hi Group
I am playing around with the concept of Nullable Infrastructure and Embedded Stub
Thanks James for publishing this. I really enjoyed it!
Could be a question to James but maybe someone else has also a good tips for the below:
Where would you put the createNull() method?
? 1. on the interface?
? 2. on 1 of the implementations of the interface?
createNull() would create this class "NotificationPort" that does nothing.
Or am I missing here something bigger?
THANKS for help!
<image.png>
Yes, the code is a total over engineered piece to teach different concepts :)
Any feedback is appreciated?
? ?Peter Gfader?