Skip to content

Commit 012b638

Browse files
mfaridmfarid
authored andcommitted
Added UnitTest cases for business delegate.
1 parent b577890 commit 012b638

File tree

6 files changed

+127
-44
lines changed

6 files changed

+127
-44
lines changed

business-delegate/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,10 @@
1515
<artifactId>junit</artifactId>
1616
<scope>test</scope>
1717
</dependency>
18+
<dependency>
19+
<groupId>org.mockito</groupId>
20+
<artifactId>mockito-core</artifactId>
21+
<scope>test</scope>
22+
</dependency>
1823
</dependencies>
1924
</project>

business-delegate/src/main/java/com/iluwatar/business/delegate/App.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,34 @@
11
package com.iluwatar.business.delegate;
22

33
/**
4-
*
54
* The Business Delegate pattern adds an abstraction layer between the presentation and business
65
* tiers. By using the pattern we gain loose coupling between the tiers. The Business Delegate
76
* encapsulates knowledge about how to locate, connect to, and interact with the business objects
87
* that make up the application.
9-
* <p>
10-
* Some of the services the Business Delegate uses are instantiated directly, and some can be
8+
*
9+
* <p>Some of the services the Business Delegate uses are instantiated directly, and some can be
1110
* retrieved through service lookups. The Business Delegate itself may contain business logic too
1211
* potentially tying together multiple service calls, exception handling, retrying etc.
13-
* <p>
14-
* In this example the client ({@link Client}) utilizes a business delegate (
12+
*
13+
* <p>In this example the client ({@link Client}) utilizes a business delegate (
1514
* {@link BusinessDelegate}) to execute a task. The Business Delegate then selects the appropriate
1615
* service and makes the service call.
17-
*
1816
*/
1917
public class App {
2018

2119
/**
22-
* Program entry point
23-
*
20+
* Program entry point.
21+
*
2422
* @param args command line args
2523
*/
2624
public static void main(String[] args) {
2725

2826
BusinessDelegate businessDelegate = new BusinessDelegate();
27+
BusinessLookup businessLookup = new BusinessLookup();
28+
businessLookup.setEjbService(new EjbService());
29+
businessLookup.setJmsService(new JmsService());
30+
31+
businessDelegate.setLookupService(businessLookup);
2932
businessDelegate.setServiceType(ServiceType.EJB);
3033

3134
Client client = new Client(businessDelegate);
Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
package com.iluwatar.business.delegate;
22

33
/**
4-
*
54
* BusinessDelegate separates the presentation and business tiers
6-
*
75
*/
86
public class BusinessDelegate {
97

10-
private BusinessLookup lookupService = new BusinessLookup();
11-
private BusinessService businessService;
12-
private ServiceType serviceType;
8+
private BusinessLookup lookupService;
9+
private BusinessService businessService;
10+
private ServiceType serviceType;
1311

14-
public void setServiceType(ServiceType serviceType) {
15-
this.serviceType = serviceType;
16-
}
12+
public void setLookupService(BusinessLookup businessLookup) {
13+
this.lookupService = businessLookup;
14+
}
1715

18-
public void doTask() {
19-
businessService = lookupService.getBusinessService(serviceType);
20-
businessService.doProcessing();
21-
}
16+
public void setServiceType(ServiceType serviceType) {
17+
this.serviceType = serviceType;
18+
}
19+
20+
public void doTask() {
21+
businessService = lookupService.getBusinessService(serviceType);
22+
businessService.doProcessing();
23+
}
2224
}
Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,31 @@
11
package com.iluwatar.business.delegate;
22

33
/**
4-
*
5-
* Class for performing service lookups
6-
*
4+
* Class for performing service lookups.
75
*/
86
public class BusinessLookup {
97

8+
private EjbService ejbService;
9+
10+
private JmsService jmsService;
11+
12+
/**
13+
* @param serviceType Type of service instance to be returned.
14+
* @return Service instance.
15+
*/
1016
public BusinessService getBusinessService(ServiceType serviceType) {
1117
if (serviceType.equals(ServiceType.EJB)) {
12-
return new EjbService();
18+
return ejbService;
1319
} else {
14-
return new JmsService();
20+
return jmsService;
1521
}
1622
}
23+
24+
public void setJmsService(JmsService jmsService) {
25+
this.jmsService = jmsService;
26+
}
27+
28+
public void setEjbService(EjbService ejbService) {
29+
this.ejbService = ejbService;
30+
}
1731
}

business-delegate/src/test/java/com/iluwatar/business/delegate/AppTest.java

Lines changed: 0 additions & 19 deletions
This file was deleted.
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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

Comments
 (0)