-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathWhenCalledWith_ThenThrowOnce.cls
34 lines (30 loc) · 1.11 KB
/
WhenCalledWith_ThenThrowOnce.cls
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/*
* Copyright (c) 2022, salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
@isTest
private class WhenCalledWith_ThenThrowOnce {
@isTest
static void recipe() {
// Arrange
Mock deliveryServiceMock = Mock.forType(DeliveryService.class);
MethodSpy planDeliverySpy = deliveryServiceMock.spyOn('planDelivery');
Bakery myBakery = new Bakery((DeliveryService) deliveryServiceMock.stub);
planDeliverySpy.whenCalledWith(new Pastry('Opera')).thenReturn(Date.today().addDays(3));
planDeliverySpy.whenCalledWith(new Pastry('Opera')).thenThrowOnce(new RecipeException());
// Act
try {
myBakery.order(new Pastry('Opera'));
// Assert
Assert.fail('Expected exception was not thrown');
} catch (Exception ex) {
Assert.isInstanceOfType(ex, RecipeException.class);
}
// Act
OrderConfirmation order = myBakery.order(new Pastry('Opera'));
// Assert
Assert.areEqual(Date.today().addDays(3), order.deliveryDate);
}
}