Skip to content

Commit b7752ed

Browse files
committed
Added some integration tests to contrast with mock tests
1 parent fd2b6e6 commit b7752ed

4 files changed

+221
-0
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/**
2+
* Copyright (c) 2012, FinancialForce.com, inc
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without modification,
6+
* are permitted provided that the following conditions are met:
7+
*
8+
* - Redistributions of source code must retain the above copyright notice,
9+
* this list of conditions and the following disclaimer.
10+
* - Redistributions in binary form must reproduce the above copyright notice,
11+
* this list of conditions and the following disclaimer in the documentation
12+
* and/or other materials provided with the distribution.
13+
* - Neither the name of the FinancialForce.com, inc nor the names of its contributors
14+
* may be used to endorse or promote products derived from this software without
15+
* specific prior written permission.
16+
*
17+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19+
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
20+
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21+
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22+
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
23+
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25+
**/
26+
27+
@IsTest
28+
private class OpportunitiesServiceTestIntegration
29+
{
30+
@IsTest
31+
private static void testServiceClass()
32+
{
33+
// Create Opportunity, Lines, Pricebooks and Products
34+
List<Opportunity> opps = new List<Opportunity>();
35+
List<List<Product2>> productsByOpp = new List<List<Product2>>();
36+
List<List<PricebookEntry>> pricebookEntriesByOpp = new List<List<PricebookEntry>>();
37+
List<List<OpportunityLineItem>> oppLinesByOpp = new List<List<OpportunityLineItem>>();
38+
Opportunity opp = new Opportunity();
39+
opp.Name = 'Test';
40+
opp.StageName = 'Open';
41+
opp.CloseDate = System.today();
42+
opps.add(opp);
43+
List<Product2> products = new List<Product2>();
44+
List<PricebookEntry> pricebookEntries = new List<PricebookEntry>();
45+
List<OpportunityLineItem> oppLineItems = new List<OpportunityLineItem>();
46+
for(Integer i=0; i<10; i++) {
47+
Product2 product = new Product2();
48+
product.Name = opp.Name + ' : Product : ' + i;
49+
products.add(product);
50+
PricebookEntry pbe = new PricebookEntry();
51+
pbe.UnitPrice = 10;
52+
pbe.IsActive = true;
53+
pbe.UseStandardPrice = false;
54+
pbe.Pricebook2Id = Test.getStandardPricebookId();
55+
pricebookEntries.add(pbe);
56+
OpportunityLineItem oppLineItem = new OpportunityLineItem();
57+
oppLineItem.Quantity = 1;
58+
oppLineItem.TotalPrice = 10;
59+
oppLineItems.add(oppLineItem);
60+
}
61+
productsByOpp.add(products);
62+
pricebookEntriesByOpp.add(pricebookEntries);
63+
oppLinesByOpp.add(oppLineItems);
64+
// Insert Opportunities
65+
insert opps;
66+
// Insert Products
67+
List<Product2> allProducts = new List<Product2>();
68+
for(List<Product2> oppProducts : productsByOpp)
69+
allProducts.addAll(oppProducts);
70+
insert allProducts;
71+
// Insert Pricebooks
72+
Integer oppIdx = 0;
73+
List<PricebookEntry> allPricebookEntries = new List<PricebookEntry>();
74+
for(List<PricebookEntry> oppPricebookEntries : pricebookEntriesByOpp) {
75+
List<Product2> pbproducts = productsByOpp[oppIdx++];
76+
Integer lineIdx = 0;
77+
for(PricebookEntry pricebookEntry : oppPricebookEntries)
78+
pricebookEntry.Product2Id = pbproducts[lineIdx++].Id;
79+
allPricebookEntries.addAll(oppPricebookEntries);
80+
}
81+
insert allPricebookEntries;
82+
// Insert Opportunity Lines
83+
oppIdx = 0;
84+
List<OpportunityLineItem> allOppLineItems = new List<OpportunityLineItem>();
85+
for(List<OpportunityLineItem> oppLines : oppLinesByOpp) {
86+
List<PricebookEntry> oppPricebookEntries = pricebookEntriesByOpp[oppIdx];
87+
Integer lineIdx = 0;
88+
for(OpportunityLineItem oppLine : oppLines) {
89+
oppLine.OpportunityId = opps[oppIdx].Id;
90+
oppLine.PricebookEntryId = oppPricebookEntries[lineIdx++].Id;
91+
}
92+
allOppLineItems.addAll(oppLines);
93+
oppIdx++;
94+
}
95+
insert allOppLineItems;
96+
97+
// Test
98+
Test.startTest();
99+
OpportunitiesService.applyDiscounts(new Set<Id> { opp.Id }, 10);
100+
Test.stopTest();
101+
102+
// Assert
103+
System.assertEquals(90, [select Amount from Opportunity limit 1][0].Amount);
104+
}
105+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
3+
<apiVersion>31.0</apiVersion>
4+
</ApexClass>
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/**
2+
* Copyright (c) 2012, FinancialForce.com, inc
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without modification,
6+
* are permitted provided that the following conditions are met:
7+
*
8+
* - Redistributions of source code must retain the above copyright notice,
9+
* this list of conditions and the following disclaimer.
10+
* - Redistributions in binary form must reproduce the above copyright notice,
11+
* this list of conditions and the following disclaimer in the documentation
12+
* and/or other materials provided with the distribution.
13+
* - Neither the name of the FinancialForce.com, inc nor the names of its contributors
14+
* may be used to endorse or promote products derived from this software without
15+
* specific prior written permission.
16+
*
17+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19+
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
20+
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21+
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22+
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
23+
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25+
**/
26+
27+
@IsTest
28+
private class OpportunityApplyDiscountControllerTestI
29+
{
30+
@IsTest
31+
private static void testControllerClass()
32+
{
33+
// Create Opportunity, Lines, Pricebooks and Products
34+
List<Opportunity> opps = new List<Opportunity>();
35+
List<List<Product2>> productsByOpp = new List<List<Product2>>();
36+
List<List<PricebookEntry>> pricebookEntriesByOpp = new List<List<PricebookEntry>>();
37+
List<List<OpportunityLineItem>> oppLinesByOpp = new List<List<OpportunityLineItem>>();
38+
Opportunity opp = new Opportunity();
39+
opp.Name = 'Test';
40+
opp.StageName = 'Open';
41+
opp.CloseDate = System.today();
42+
opps.add(opp);
43+
List<Product2> products = new List<Product2>();
44+
List<PricebookEntry> pricebookEntries = new List<PricebookEntry>();
45+
List<OpportunityLineItem> oppLineItems = new List<OpportunityLineItem>();
46+
for(Integer i=0; i<10; i++) {
47+
Product2 product = new Product2();
48+
product.Name = opp.Name + ' : Product : ' + i;
49+
products.add(product);
50+
PricebookEntry pbe = new PricebookEntry();
51+
pbe.UnitPrice = 10;
52+
pbe.IsActive = true;
53+
pbe.UseStandardPrice = false;
54+
pbe.Pricebook2Id = Test.getStandardPricebookId();
55+
pricebookEntries.add(pbe);
56+
OpportunityLineItem oppLineItem = new OpportunityLineItem();
57+
oppLineItem.Quantity = 1;
58+
oppLineItem.TotalPrice = 10;
59+
oppLineItems.add(oppLineItem);
60+
}
61+
productsByOpp.add(products);
62+
pricebookEntriesByOpp.add(pricebookEntries);
63+
oppLinesByOpp.add(oppLineItems);
64+
// Insert Opportunities
65+
insert opps;
66+
// Insert Products
67+
List<Product2> allProducts = new List<Product2>();
68+
for(List<Product2> oppProducts : productsByOpp)
69+
allProducts.addAll(oppProducts);
70+
insert allProducts;
71+
// Insert Pricebooks
72+
Integer oppIdx = 0;
73+
List<PricebookEntry> allPricebookEntries = new List<PricebookEntry>();
74+
for(List<PricebookEntry> oppPricebookEntries : pricebookEntriesByOpp) {
75+
List<Product2> pbproducts = productsByOpp[oppIdx++];
76+
Integer lineIdx = 0;
77+
for(PricebookEntry pricebookEntry : oppPricebookEntries)
78+
pricebookEntry.Product2Id = pbproducts[lineIdx++].Id;
79+
allPricebookEntries.addAll(oppPricebookEntries);
80+
}
81+
insert allPricebookEntries;
82+
// Insert Opportunity Lines
83+
oppIdx = 0;
84+
List<OpportunityLineItem> allOppLineItems = new List<OpportunityLineItem>();
85+
for(List<OpportunityLineItem> oppLines : oppLinesByOpp) {
86+
List<PricebookEntry> oppPricebookEntries = pricebookEntriesByOpp[oppIdx];
87+
Integer lineIdx = 0;
88+
for(OpportunityLineItem oppLine : oppLines) {
89+
oppLine.OpportunityId = opps[oppIdx].Id;
90+
oppLine.PricebookEntryId = oppPricebookEntries[lineIdx++].Id;
91+
}
92+
allOppLineItems.addAll(oppLines);
93+
oppIdx++;
94+
}
95+
insert allOppLineItems;
96+
97+
// Test
98+
Test.startTest();
99+
ApexPages.StandardController standardController = new ApexPages.StandardController([select Id from Opportunity limit 1]);
100+
OpportunityApplyDiscountController controller = new OpportunityApplyDiscountController(standardController);
101+
controller.DiscountPercentage = 10;
102+
controller.applyDiscount();
103+
Test.stopTest();
104+
105+
// Assert
106+
System.assertEquals(90, [select Amount from Opportunity where Id = :standardController.getId()].Amount);
107+
}
108+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
3+
<apiVersion>31.0</apiVersion>
4+
</ApexClass>

0 commit comments

Comments
 (0)