Type: docs and refactoring Co-authored-by: Subhrodip Mohanta <hello@subho.xyz>
This commit is contained in:
@ -26,25 +26,20 @@ package com.iluwatar.business.delegate;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
/**
|
||||
* The Business Delegate pattern adds an abstraction layer between the presentation and business
|
||||
* tiers. By using the pattern we gain loose coupling between the tiers. The Business Delegate
|
||||
* encapsulates knowledge about how to locate, connect to, and interact with the business objects
|
||||
* that make up the application.
|
||||
*
|
||||
* <p>Some of the services the Business Delegate uses are instantiated directly, and some can be
|
||||
* retrieved through service lookups. The Business Delegate itself may contain business logic too
|
||||
* potentially tying together multiple service calls, exception handling, retrying etc.
|
||||
* Tests for the {@link BusinessDelegate}
|
||||
*/
|
||||
class BusinessDelegateTest {
|
||||
|
||||
private EjbService ejbService;
|
||||
private NetflixService netflixService;
|
||||
|
||||
private JmsService jmsService;
|
||||
private YouTubeService youTubeService;
|
||||
|
||||
private BusinessDelegate businessDelegate;
|
||||
|
||||
@ -54,19 +49,19 @@ class BusinessDelegateTest {
|
||||
*/
|
||||
@BeforeEach
|
||||
public void setup() {
|
||||
ejbService = spy(new EjbService());
|
||||
jmsService = spy(new JmsService());
|
||||
netflixService = spy(new NetflixService());
|
||||
youTubeService = spy(new YouTubeService());
|
||||
|
||||
BusinessLookup businessLookup = spy(new BusinessLookup());
|
||||
businessLookup.setEjbService(ejbService);
|
||||
businessLookup.setJmsService(jmsService);
|
||||
businessLookup.setNetflixService(netflixService);
|
||||
businessLookup.setYouTubeService(youTubeService);
|
||||
|
||||
businessDelegate = spy(new BusinessDelegate());
|
||||
businessDelegate.setLookupService(businessLookup);
|
||||
}
|
||||
|
||||
/**
|
||||
* In this example the client ({@link Client}) utilizes a business delegate (
|
||||
* In this example the client ({@link MobileClient}) utilizes a business delegate (
|
||||
* {@link BusinessDelegate}) to execute a task. The Business Delegate then selects the appropriate
|
||||
* service and makes the service call.
|
||||
*/
|
||||
@ -74,26 +69,20 @@ class BusinessDelegateTest {
|
||||
void testBusinessDelegate() {
|
||||
|
||||
// setup a client object
|
||||
var client = new Client(businessDelegate);
|
||||
|
||||
// set the service type
|
||||
businessDelegate.setServiceType(ServiceType.EJB);
|
||||
var client = new MobileClient(businessDelegate);
|
||||
|
||||
// action
|
||||
client.doTask();
|
||||
client.playbackMovie("Die hard");
|
||||
|
||||
// verifying that the businessDelegate was used by client during doTask() method.
|
||||
verify(businessDelegate).doTask();
|
||||
verify(ejbService).doProcessing();
|
||||
|
||||
// set the service type
|
||||
businessDelegate.setServiceType(ServiceType.JMS);
|
||||
// verifying that the businessDelegate was used by client during playbackMovie() method.
|
||||
verify(businessDelegate).playbackMovie(anyString());
|
||||
verify(netflixService).doProcessing();
|
||||
|
||||
// action
|
||||
client.doTask();
|
||||
client.playbackMovie("Maradona");
|
||||
|
||||
// verifying that the businessDelegate was used by client during doTask() method.
|
||||
verify(businessDelegate, times(2)).doTask();
|
||||
verify(jmsService).doProcessing();
|
||||
verify(businessDelegate, times(2)).playbackMovie(anyString());
|
||||
verify(youTubeService).doProcessing();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user