At the end of implementing the REST API adaptor, we have mocked out two domain services GetStockPositionService and GetStockMarketValueService.
As you go through the code below, notice that we will write the domain services functionally, and we can test them without using the Spring framework for dependency injection. Testing domain services functionally and without the Spring framework enables very fast testing.
Also, notice that where the domain services need to call some adaptors, whether data repository or external services, a port to interface is used. Again, in the tests, we specify the required behaviour of these adaptors through these ports, and the domain services does not have to know any further details of these adaptors beyond their behaviours.
Let’s start with the test for GetStockPositionService, GetStockPositionServiceTest as shown here:
The repository required for this domain service is specified as an interface and provided directly to the service as a constructor parameter.
The other domain service GetStockMarketValueService and its test GetStockMarketValueServiceTest are written in a similar style.
GetStockMarketValueService requires a source for the current market price of the stock for calculating its result. For now, we’ll specify an port as an interface GetStockMarketPricePort for this service.