|
| 1 | +package com.iluwatar.business.delegate; |
| 2 | + |
| 3 | +import static org.mockito.Mockito.spy; |
| 4 | +import static org.mockito.Mockito.times; |
| 5 | +import static org.mockito.Mockito.verify; |
| 6 | + |
| 7 | +import org.junit.Before; |
| 8 | +import org.junit.Test; |
| 9 | + |
| 10 | +/** |
| 11 | + * The Business Delegate pattern adds an abstraction layer between the presentation and business |
| 12 | + * tiers. By using the pattern we gain loose coupling between the tiers. The Business Delegate |
| 13 | + * encapsulates knowledge about how to locate, connect to, and interact with the business objects |
| 14 | + * that make up the application. |
| 15 | + * |
| 16 | + * <p>Some of the services the Business Delegate uses are instantiated directly, and some can be |
| 17 | + * retrieved through service lookups. The Business Delegate itself may contain business logic too |
| 18 | + * potentially tying together multiple service calls, exception handling, retrying etc. |
| 19 | + */ |
| 20 | +public class BusinessDelegateTest { |
| 21 | + |
| 22 | + private EjbService ejbService; |
| 23 | + |
| 24 | + private JmsService jmsService; |
| 25 | + |
| 26 | + private BusinessLookup businessLookup; |
| 27 | + |
| 28 | + private BusinessDelegate businessDelegate; |
| 29 | + |
| 30 | + /** |
| 31 | + * This method sets up the instance variables of this test class. It is executed before the |
| 32 | + * execution of every test. |
| 33 | + */ |
| 34 | + @Before |
| 35 | + public void setup() { |
| 36 | + ejbService = spy(new EjbService()); |
| 37 | + jmsService = spy(new JmsService()); |
| 38 | + |
| 39 | + businessLookup = spy(new BusinessLookup()); |
| 40 | + businessLookup.setEjbService(ejbService); |
| 41 | + businessLookup.setJmsService(jmsService); |
| 42 | + |
| 43 | + businessDelegate = spy(new BusinessDelegate()); |
| 44 | + businessDelegate.setLookupService(businessLookup); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * In this example the client ({@link Client}) utilizes a business delegate ( |
| 49 | + * {@link BusinessDelegate}) to execute a task. The Business Delegate then selects the appropriate |
| 50 | + * service and makes the service call. |
| 51 | + */ |
| 52 | + @Test |
| 53 | + public void testBusinessDelegate() { |
| 54 | + |
| 55 | + // setup a client object |
| 56 | + Client client = new Client(businessDelegate); |
| 57 | + |
| 58 | + // set the service type |
| 59 | + businessDelegate.setServiceType(ServiceType.EJB); |
| 60 | + |
| 61 | + // action |
| 62 | + client.doTask(); |
| 63 | + |
| 64 | + // verifying that the businessDelegate was used by client during doTask() method. |
| 65 | + verify(businessDelegate).doTask(); |
| 66 | + verify(ejbService).doProcessing(); |
| 67 | + |
| 68 | + // set the service type |
| 69 | + businessDelegate.setServiceType(ServiceType.JMS); |
| 70 | + |
| 71 | + // action |
| 72 | + client.doTask(); |
| 73 | + |
| 74 | + // verifying that the businessDelegate was used by client during doTask() method. |
| 75 | + verify(businessDelegate, times(2)).doTask(); |
| 76 | + verify(jmsService).doProcessing(); |
| 77 | + } |
| 78 | +} |
0 commit comments