How to use method of org.mockito.Mockito class

Best Mockito code snippet using org.mockito.Mockito.

Run Mockito automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

copy
1/*
2 *   Copyright 2016 the original author or authors.
3 *
4 *   Licensed under the Apache License, Version 2.0 (the "License");
5 *   you may not use this file except in compliance with the License.
6 *   You may obtain a copy of the License at
7 *
8 *        http://www.apache.org/licenses/LICENSE-2.0
9 *
10 *   Unless required by applicable law or agreed to in writing, software
11 *   distributed under the License is distributed on an "AS IS" BASIS,
12 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 *   See the License for the specific language governing permissions and
14 *   limitations under the License.
15 *
16 */
17package org.powermock.api.mockito.expectation;
18
19import org.mockito.Mockito;
20import org.mockito.stubbing.Answer;
21import org.mockito.stubbing.Stubber;
22
23import java.lang.reflect.Method;
24
25/**
26 * Setup stubbing for private or void methods in final class, final void
27 * methods, or static (final) methods.
28 */
29public interface PowerMockitoStubber extends Stubber {
30    
31    /**
32     * Allows to choose a static method when stubbing in
33     * doThrow()|doAnswer()|doNothing()|doReturn() style
34     * <p>
35     * Example:
36     * </p>
37     * <pre>
38     * doThrow(new RuntimeException()).when(StaticList.class);
39     * StaticList.clear();
40     *
41     * //following throws RuntimeException:
42     * StaticList.clear();
43     * </pre>
44     * <p>
45     * Read more about those methods:
46     * </p>
47     * <p>
48     * {@link Mockito#doThrow(Class)}
49     * </p>
50     * <p>
51     * {@link Mockito#doAnswer(Answer)}
52     * </p>
53     * <p>
54     * {@link Mockito#doNothing()}
55     * </p>
56     * <p>
57     * {@link Mockito#doReturn(Object)}
58     * </p>
59     *
60     * @param classMock the mock class
61     * @see Mockito
62     */
63    void when(Class<?> classMock);
64    
65    /**
66     * Allows to mock a private instance method when stubbing in
67     * doThrow()|doAnswer()|doNothing()|doReturn() style.
68     * <p>
69     * Example:
70     * <pre>
71     * doThrow(new RuntimeException()).when(instance, method(&quot;myMethod&quot;)).withNoArguments();
72     * </pre>
73     * </p>
74     * <p>
75     * Read more about those methods:
76     * </p>
77     * <p>
78     * {@link Mockito#doThrow(Class)}
79     * </p>
80     * <p>
81     * {@link Mockito#doAnswer(Answer)}
82     * </p>
83     * <p>
84     * {@link Mockito#doNothing()}
85     * </p>
86     * <p>
87     * {@link Mockito#doReturn(Object)}
88     * </p>
89     *
90     * @param mock   the method
91     * @param method private  method to be mocked
92     * @see Mockito
93     */
94    <T> PrivatelyExpectedArguments when(T mock, Method method) throws Exception;
95    
96    /**
97     * Allows to mock a private instance method based on the parameters when
98     * stubbing in doThrow()|doAnswer()|doNothing()|doReturn() style.
99     * <p>
100     * Example:
101     * </p>
102     * <p>
103     * <pre>
104     * doThrow(new RuntimeException()).when(instance, parameter1, parameter2);
105     * </pre>
106     * </p>
107     * <p>
108     * Read more about those methods:
109     * </p>
110     * <p>
111     * {@link Mockito#doThrow(Throwable...)}
112     * </p>
113     * <p>
114     * {@link Mockito#doAnswer(Answer)}
115     * </p>
116     * <p>
117     * {@link Mockito#doNothing()}
118     * </p>
119     * <p>
120     * {@link Mockito#doReturn(Object)}
121     * </p>
122     *
123     * @param mock      the Mock
124     * @param arguments array of arguments is used to find suitable method to be mocked.
125     * @see Mockito
126     */
127    <T> void when(T mock, Object... arguments) throws Exception;
128    
129    /**
130     * Allows to mock a private instance method based on method name and
131     * parameters when stubbing in doThrow()|doAnswer()|doNothing()|doReturn()
132     * style.
133     * <p>
134     * Example:
135     * </p>
136     * <pre>
137     * doThrow(new RuntimeException()).when(instance, &quot;methodName&quot;, parameter1, parameter2);
138     * </pre>
139     * </p>
140     * <p>
141     * Read more about those methods:
142     * </p>
143     * <p>
144     * {@link Mockito#doThrow(Throwable...)}
145     * </p>
146     * <p>
147     * {@link Mockito#doAnswer(Answer)}
148     * </p>
149     * <p>
150     * {@link Mockito#doNothing()}
151     * </p>
152     * <p>
153     * {@link Mockito#doReturn(Object)}
154     * </p>
155     *
156     * @param mock           the Mock
157     * @param methodToExpect name of method which have to mocked
158     * @param arguments      array of arguments of <code>methodToExpect</code>
159     * @see Mockito
160     */
161    <T> void when(T mock, String methodToExpect, Object... arguments) throws Exception;
162    
163    /**
164     * Allows to mock a static private method when stubbing in
165     * doThrow()|doAnswer()|doNothing()|doReturn() style.
166     * <p>
167     * Example:
168     * <pre>
169     * doThrow(new RuntimeException()).when(MyClass.class, method(&quot;myMethod&quot;)).withNoArguments();
170     * </pre>
171     * </p>
172     * <p>
173     * Read more about those methods:
174     * </p>
175     * <p>
176     * {@link Mockito#doThrow(Throwable...)}
177     * </p>
178     * <p>
179     * {@link Mockito#doAnswer(Answer)}
180     * </p>
181     * <p>
182     * {@link Mockito#doNothing()}
183     * </p>
184     * <p>
185     * {@link Mockito#doReturn(Object)}
186     * </p>
187     *
188     * @param classMock class owner of private static method
189     * @param method    private static method to be mocked
190     * @see Mockito
191     */
192    <T> PrivatelyExpectedArguments when(Class<T> classMock, Method method) throws Exception;
193    
194    /**
195     * Allows to mock a static private method based on the parameters when
196     * stubbing in doThrow()|doAnswer()|doNothing()|doReturn() style.
197     * <p>
198     * Example:
199     * <pre>
200     * doThrow(new RuntimeException()).when(MyClass.class, parameter1, parameter2);
201     * </pre>
202     * </p>
203     * <p>
204     * Read more about those methods:
205     * </p>
206     * <p>
207     * {@link Mockito#doThrow(Throwable...)}
208     * </p>
209     * <p>
210     * {@link Mockito#doAnswer(Answer)}
211     * </p>
212     * <p>
213     * {@link Mockito#doNothing()}
214     * </p>
215     * <p>
216     * {@link Mockito#doReturn(Object)}
217     * </p>
218     *
219     * @param classMock class owner of private static method
220     * @param arguments array of arguments is used to find suitable method to be mocked.
221     * @see Mockito
222     */
223    <T> void when(Class<T> classMock, Object... arguments) throws Exception;
224    
225    /**
226     * Allows to mock a static private method based on method name and
227     * parameters when stubbing in doThrow()|doAnswer()|doNothing()|doReturn()
228     * style.
229     * <p>
230     * Example:
231     * <pre>
232     * doThrow(new RuntimeException()).when(MyClass.class, &quot;methodName&quot;, parameter1, parameter2);
233     * </pre>
234     * </p>
235     * <p>
236     * Read more about those methods:
237     * </p>
238     * <p>
239     * {@link Mockito#doThrow(Throwable...)}
240     * </p>
241     * <p>
242     * {@link Mockito#doAnswer(Answer)}
243     * </p>
244     * <p>
245     * {@link Mockito#doNothing()}
246     * </p>
247     * <p>
248     * {@link Mockito#doReturn(Object)}
249     * </p>
250     *
251     * @param classMock      the class owner of static private method
252     * @param methodToExpect name of method which have to mocked
253     * @param arguments      array of arguments of <code>methodToExpect</code>
254     * @see Mockito
255     */
256    <T> void when(Class<T> classMock, String methodToExpect, Object... arguments) throws Exception;
257}
258
Full Screen
copy
1/*******************************************************************************
2 * Copyright 2018 T Mobile, Inc. or its affiliates. All Rights Reserved.
3 * 
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License.  You may obtain a copy
6 * of the License at
7 * 
8 *   http://www.apache.org/licenses/LICENSE-2.0
9 * 
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 ******************************************************************************/
16package com.tmobile.cso.pacman.inventory.file;
17
18import static org.mockito.Matchers.anyObject;
19import static org.mockito.Matchers.anyString;
20import static org.powermock.api.mockito.PowerMockito.mockStatic;
21import static org.powermock.api.mockito.PowerMockito.when;
22
23import java.io.IOException;
24import java.util.ArrayList;
25import java.util.HashMap;
26import java.util.List;
27import java.util.Map;
28
29import org.junit.Before;
30import org.junit.Test;
31import org.junit.runner.RunWith;
32import org.mockito.InjectMocks;
33import org.mockito.Mock;
34import org.mockito.MockitoAnnotations;
35import org.powermock.api.mockito.PowerMockito;
36import org.powermock.core.classloader.annotations.PowerMockIgnore;
37import org.powermock.core.classloader.annotations.PrepareForTest;
38import org.powermock.modules.junit4.PowerMockRunner;
39import org.springframework.test.util.ReflectionTestUtils;
40
41import com.amazonaws.auth.BasicSessionCredentials;
42import com.tmobile.cso.pacman.inventory.InventoryConstants;
43import com.tmobile.cso.pacman.inventory.auth.CredentialProvider;
44import com.tmobile.cso.pacman.inventory.util.ASGInventoryUtil;
45import com.tmobile.cso.pacman.inventory.util.DirectConnectionInventoryUtil;
46import com.tmobile.cso.pacman.inventory.util.EC2InventoryUtil;
47import com.tmobile.cso.pacman.inventory.util.ESInventoryUtil;
48import com.tmobile.cso.pacman.inventory.util.InventoryUtil;
49import com.tmobile.cso.pacman.inventory.util.SNSInventoryUtil;
50
51
52
53/**
54 * The Class AssetFileGeneratorTest.
55 */
56@RunWith(PowerMockRunner.class)
57@PrepareForTest({FileManager.class,ErrorManageUtil.class,InventoryUtil.class,EC2InventoryUtil.class,SNSInventoryUtil.class,DirectConnectionInventoryUtil.class,
58    ASGInventoryUtil.class,ESInventoryUtil.class})
59@PowerMockIgnore("javax.management.*")
60public class AssetFileGeneratorTest {
61    
62    /** The asset file generator. */
63    @InjectMocks
64    AssetFileGenerator assetFileGenerator;
65    
66    /** The cred provider. */
67    @Mock
68    CredentialProvider credProvider;
69
70    /**
71     * Sets the up.
72     */
73    @Before
74    public void setUp() {
75        MockitoAnnotations.initMocks(this);
76    }
77    
78    /**
79     * Generate files test.
80     *
81     * @throws Exception the exception
82     */
83    @SuppressWarnings({"unchecked", "rawtypes"})
84    @Test
85    public void generateFilesTest() throws Exception{
86        
87        mockStatic(FileManager.class);
88        PowerMockito.doNothing().when(FileManager.class,"initialise",anyString());
89        mockStatic(ErrorManageUtil.class);
90        PowerMockito.doNothing().when(ErrorManageUtil.class,"initialise");
91        
92        ReflectionTestUtils.setField(assetFileGenerator, "targetTypes", "ec2,asg,stack,dynamodb,efs,emr,lambda,classicelb,appelb,targetgroup,"
93                + "nat,rdsdb,rdscluster,s3,eni,sg,subnet,checks,redshift,volume,snapshot,vpc,api,iamuser,rdssnapshot,iamrole,kms,cloudfront,beanstalk,phd,"
94                + "routetable,networkacl,elasticip,launchconfig,internetgw,vpngw,asgpolicy,snstopic,egressgateway,dhcpoption,peeringconnection,customergateway,"
95                + "vpnconnection,directconnect,virtualinterface,elasticsearch,reserved instances,ssm");
96        when(credProvider.getCredentials(anyString(), anyString())).thenReturn(new BasicSessionCredentials("awsAccessKey", "awsSecretKey", "sessionToken"));
97        
98        mockStatic(InventoryUtil.class);
99        mockStatic(EC2InventoryUtil.class);
100        mockStatic(ASGInventoryUtil.class);
101        mockStatic(DirectConnectionInventoryUtil.class);
102        mockStatic(ESInventoryUtil.class);
103        mockStatic(SNSInventoryUtil.class);
104        
105        when(InventoryUtil.fetchInstances(anyObject(), anyString(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());
106        PowerMockito.doNothing().when(FileManager.class,"generateInstanceFiles",new HashMap<>());
107        
108        when(InventoryUtil.fetchAsg(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());
109        PowerMockito.doNothing().when(FileManager.class,"generateAsgFiles",new HashMap<>());
110        
111        when(InventoryUtil.fetchCloudFormationStack(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());
112        PowerMockito.doNothing().when(FileManager.class,"generateCloudFormationStackFiles",new HashMap<>());
113        
114        when(InventoryUtil.fetchDynamoDBTables(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());
115        PowerMockito.doNothing().when(FileManager.class,"generateDynamoDbFiles",new HashMap<>());
116        
117        when(InventoryUtil.fetchEFSInfo(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());
118        PowerMockito.doNothing().when(FileManager.class,"generateEfsFiles",new HashMap<>());
119        
120        when(InventoryUtil.fetchEMRInfo(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());
121        PowerMockito.doNothing().when(FileManager.class,"generateEmrFiles",new HashMap<>());
122        
123        when(InventoryUtil.fetchLambdaInfo(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());
124        PowerMockito.doNothing().when(FileManager.class,"generateLamdaFiles",new HashMap<>());
125        
126        when(InventoryUtil.fetchClassicElbInfo(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());
127        PowerMockito.doNothing().when(FileManager.class,"generateClassicElbFiles",new HashMap<>());
128        
129        when(InventoryUtil.fetchElbInfo(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());
130        PowerMockito.doNothing().when(FileManager.class,"generateApplicationElbFiles",new HashMap<>());
131        
132        when(InventoryUtil.fetchTargetGroups(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());
133        PowerMockito.doNothing().when(FileManager.class,"generateTargetGroupFiles",new HashMap<>());
134        
135        when(InventoryUtil.fetchNATGatewayInfo(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());
136        PowerMockito.doNothing().when(FileManager.class,"generateNatGatewayFiles",new HashMap<>());
137        
138        when(InventoryUtil.fetchRDSInstanceInfo(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());
139        PowerMockito.doNothing().when(FileManager.class,"generateRDSInstanceFiles",new HashMap<>());
140        
141        when(InventoryUtil.fetchRDSClusterInfo(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());
142        PowerMockito.doNothing().when(FileManager.class,"generateRDSClusterFiles",new HashMap<>());
143        
144        when(InventoryUtil.fetchS3Info(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap());
145        PowerMockito.doNothing().when(FileManager.class,"generateS3Files",new HashMap<>());
146        
147        when(InventoryUtil.fetchNetworkIntefaces(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());
148        PowerMockito.doNothing().when(FileManager.class,"generateNwInterfaceFiles",new HashMap<>());
149        
150        when(InventoryUtil.fetchSecurityGroups(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());
151        PowerMockito.doNothing().when(FileManager.class,"generateSecGroupFile",new HashMap<>());
152        
153        when(InventoryUtil.fetchSubnets(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());
154        PowerMockito.doNothing().when(FileManager.class,"generateSubnetFiles",new HashMap<>());
155        
156        when(InventoryUtil.fetchTrusterdAdvisorsChecks(anyObject(), anyString(), anyString())).thenReturn(new HashMap<>());
157        PowerMockito.doNothing().when(FileManager.class,"generateTrustedAdvisorFiles",new HashMap<>());
158        
159        when(InventoryUtil.fetchRedshiftInfo(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());
160        PowerMockito.doNothing().when(FileManager.class,"generateRedshiftFiles",new HashMap<>());
161        
162        when(InventoryUtil.fetchVolumetInfo(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());
163        PowerMockito.doNothing().when(FileManager.class,"generatefetchVolumeFiles",new HashMap<>());
164        
165        when(InventoryUtil.fetchSnapshots(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());
166        PowerMockito.doNothing().when(FileManager.class,"generateSnapshotFiles",new HashMap<>());
167        
168        when(InventoryUtil.fetchVpcInfo(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());
169        PowerMockito.doNothing().when(FileManager.class,"generateVpcFiles",new HashMap<>());
170        
171        when(InventoryUtil.fetchApiGateways(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());
172        PowerMockito.doNothing().when(FileManager.class,"generateApiGatewayFiles",new HashMap<>());
173        
174        when(InventoryUtil.fetchIAMUsers(anyObject(), anyString(), anyString())).thenReturn(new HashMap<>());
175        PowerMockito.doNothing().when(FileManager.class,"generateIamUserFiles",new HashMap<>());
176        
177        when(InventoryUtil.fetchRDSDBSnapshots(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());
178        PowerMockito.doNothing().when(FileManager.class,"generateRDSSnapshotFiles",new HashMap<>());
179        
180        when(InventoryUtil.fetchIAMRoles(anyObject(), anyString(), anyString())).thenReturn(new HashMap<>());
181        PowerMockito.doNothing().when(FileManager.class,"generateIamRoleFiles",new HashMap<>());
182        
183        when(InventoryUtil.fetchKMSKeys(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());
184        PowerMockito.doNothing().when(FileManager.class,"generateKMSFiles",new HashMap<>());
185        
186        when(InventoryUtil.fetchCloudFrontInfo(anyObject(), anyString(), anyString())).thenReturn(new HashMap<>());
187        PowerMockito.doNothing().when(FileManager.class,"generateCloudFrontFiles",new HashMap<>());
188        
189        when(InventoryUtil.fetchEBSInfo(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());
190        PowerMockito.doNothing().when(FileManager.class,"generateEBSFiles",new HashMap<>());
191        
192        when(InventoryUtil.fetchPHDInfo(anyObject(), anyString(), anyString())).thenReturn(new HashMap<>());
193        PowerMockito.doNothing().when(FileManager.class,"generatePHDFiles",new HashMap<>());
194        
195        when(EC2InventoryUtil.fetchRouteTables(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());
196        PowerMockito.doNothing().when(FileManager.class,"generateEC2RouteTableFiles",new HashMap<>());
197        
198        when(EC2InventoryUtil.fetchNetworkACL(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());
199        PowerMockito.doNothing().when(FileManager.class,"generateNetworkAclFiles",new HashMap<>());
200        
201        when(EC2InventoryUtil.fetchElasticIPAddresses(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());
202        PowerMockito.doNothing().when(FileManager.class,"generateElasticIPFiles",new HashMap<>());
203        
204        when(ASGInventoryUtil.fetchLaunchConfigurations(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());
205        PowerMockito.doNothing().when(FileManager.class,"generateLaunchConfigurationsFiles",new HashMap<>());
206        
207        when(EC2InventoryUtil.fetchInternetGateway(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());
208        PowerMockito.doNothing().when(FileManager.class,"generateInternetGatewayFiles",new HashMap<>());
209        
210        when(EC2InventoryUtil.fetchVPNGateway(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());
211        PowerMockito.doNothing().when(FileManager.class,"generateVPNGatewayFiles",new HashMap<>());
212        
213        when(ASGInventoryUtil.fetchScalingPolicies(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());
214        PowerMockito.doNothing().when(FileManager.class,"generateScalingPolicies",new HashMap<>());
215        
216        when(SNSInventoryUtil.fetchSNSTopics(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());
217        PowerMockito.doNothing().when(FileManager.class,"generateSNSTopics",new HashMap<>());
218        
219        when(EC2InventoryUtil.fetchEgressGateway(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());
220        PowerMockito.doNothing().when(FileManager.class,"generateEgressGateway",new HashMap<>());
221        
222        when(EC2InventoryUtil.fetchDHCPOptions(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());
223        PowerMockito.doNothing().when(FileManager.class,"generateDhcpOptions",new HashMap<>());
224        
225        when(EC2InventoryUtil.fetchPeeringConnections(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());
226        PowerMockito.doNothing().when(FileManager.class,"generatePeeringConnections",new HashMap<>());
227        
228        when(EC2InventoryUtil.fetchCustomerGateway(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());
229        PowerMockito.doNothing().when(FileManager.class,"generateCustomerGateway",new HashMap<>());
230        
231        when(EC2InventoryUtil.fetchVPNConnections(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());
232        PowerMockito.doNothing().when(FileManager.class,"generateVpnConnection",new HashMap<>());
233        
234        when(DirectConnectionInventoryUtil.fetchDirectConnections(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());
235        PowerMockito.doNothing().when(FileManager.class,"generateDirectConnection",new HashMap<>());
236        
237        when(DirectConnectionInventoryUtil.fetchDirectConnectionsVirtualInterfaces(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());
238        PowerMockito.doNothing().when(FileManager.class,"generateDirectConnectionVirtualInterfaces",new HashMap<>());
239        
240        when(ESInventoryUtil.fetchESInfo(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());
241        PowerMockito.doNothing().when(FileManager.class,"generateESDomain",new HashMap<>());
242        
243        when(EC2InventoryUtil.fetchReservedInstances(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());
244        PowerMockito.doNothing().when(FileManager.class,"generateReservedInstances",new HashMap<>());
245        
246        when(EC2InventoryUtil.fetchSSMInfo(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());
247        PowerMockito.doNothing().when(FileManager.class,"generateSsmFiles",new HashMap<>());
248        
249        List<Map<String,String>> accounts = new ArrayList<>();
250        Map<String,String> account = new HashMap<>();
251        account.put(InventoryConstants.ACCOUNT_ID, "account");
252        account.put(InventoryConstants.ACCOUNT_NAME, "accountName");
253        accounts.add(account);
254        assetFileGenerator.generateFiles(accounts , "skipRegions", "filePath");
255        
256        ReflectionTestUtils.setField(assetFileGenerator, "targetTypes", "targetType");
257        assetFileGenerator.generateFiles(accounts , "skipRegions", "filePath");
258    }
259    
260    /**
261     * Generate files test exception.
262     *
263     * @throws Exception the exception
264     */
265    @SuppressWarnings({"unchecked", "rawtypes"})
266    @Test
267    public void generateFilesTest_Exception() throws Exception{
268        
269        mockStatic(FileManager.class);
270        PowerMockito.doThrow(new IOException()).when(FileManager.class,"initialise",anyString());
271        
272        ReflectionTestUtils.setField(assetFileGenerator, "targetTypes", "ec2,asg,stack,dynamodb,efs,emr,lambda,classicelb,appelb,targetgroup,"
273                + "nat,rdsdb,rdscluster,s3,eni,sg,subnet,checks,redshift,volume,snapshot,vpc,api,iamuser,rdssnapshot,iamrole,kms,cloudfront,beanstalk,phd,"
274                + "routetable,networkacl,elasticip,launchconfig,internetgw,vpngw,asgpolicy,snstopic,egressgateway,dhcpoption,peeringconnection,customergateway,"
275                + "vpnconnection,directconnect,virtualinterface,elasticsearch,reserved instances,ssm");
276        when(credProvider.getCredentials(anyString(), anyString())).thenReturn(new BasicSessionCredentials("awsAccessKey", "awsSecretKey", "sessionToken"));
277        
278        mockStatic(InventoryUtil.class);
279        mockStatic(EC2InventoryUtil.class);
280        mockStatic(ASGInventoryUtil.class);
281        mockStatic(DirectConnectionInventoryUtil.class);
282        mockStatic(ESInventoryUtil.class);
283        mockStatic(SNSInventoryUtil.class);
284        mockStatic(ErrorManageUtil.class);
285        
286        PowerMockito.doNothing().when(ErrorManageUtil.class,"uploadError",anyString(),anyString(),anyString(),anyString());
287        
288        when(InventoryUtil.fetchInstances(anyObject(), anyString(), anyString(), anyString(),anyString())).thenReturn(new HashMap<>());
289        PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateInstanceFiles",new HashMap<>());
290        
291        when(InventoryUtil.fetchAsg(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());
292        PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateAsgFiles",new HashMap<>());
293        
294        when(InventoryUtil.fetchCloudFormationStack(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());
295        PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateCloudFormationStackFiles",new HashMap<>());
296        
297        when(InventoryUtil.fetchDynamoDBTables(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());
298        PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateDynamoDbFiles",new HashMap<>());
299        
300        when(InventoryUtil.fetchEFSInfo(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());
301        PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateEfsFiles",new HashMap<>());
302        
303        when(InventoryUtil.fetchEMRInfo(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());
304        PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateEmrFiles",new HashMap<>());
305        
306        when(InventoryUtil.fetchLambdaInfo(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());
307        PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateLamdaFiles",new HashMap<>());
308        
309        when(InventoryUtil.fetchClassicElbInfo(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());
310        PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateClassicElbFiles",new HashMap<>());
311        
312        when(InventoryUtil.fetchElbInfo(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());
313        PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateApplicationElbFiles",new HashMap<>());
314        
315        when(InventoryUtil.fetchTargetGroups(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());
316        PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateTargetGroupFiles",new HashMap<>());
317        
318        when(InventoryUtil.fetchNATGatewayInfo(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());
319        PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateNatGatewayFiles",new HashMap<>());
320        
321        when(InventoryUtil.fetchRDSInstanceInfo(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());
322        PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateRDSInstanceFiles",new HashMap<>());
323        
324        when(InventoryUtil.fetchRDSClusterInfo(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());
325        PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateRDSClusterFiles",new HashMap<>());
326        
327        when(InventoryUtil.fetchS3Info(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap());
328        PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateS3Files",new HashMap<>());
329        
330        when(InventoryUtil.fetchNetworkIntefaces(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());
331        PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateNwInterfaceFiles",new HashMap<>());
332        
333        when(InventoryUtil.fetchSecurityGroups(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());
334        PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateSecGroupFile",new HashMap<>());
335        
336        when(InventoryUtil.fetchSubnets(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());
337        PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateSubnetFiles",new HashMap<>());
338        
339        when(InventoryUtil.fetchTrusterdAdvisorsChecks(anyObject(), anyString(), anyString())).thenReturn(new HashMap<>());
340        PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateTrustedAdvisorFiles",new HashMap<>());
341        
342        when(InventoryUtil.fetchRedshiftInfo(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());
343        PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateRedshiftFiles",new HashMap<>());
344        
345        when(InventoryUtil.fetchVolumetInfo(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());
346        PowerMockito.doThrow(new IOException()).when(FileManager.class,"generatefetchVolumeFiles",new HashMap<>());
347        
348        when(InventoryUtil.fetchSnapshots(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());
349        PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateSnapshotFiles",new HashMap<>());
350        
351        when(InventoryUtil.fetchVpcInfo(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());
352        PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateVpcFiles",new HashMap<>());
353        
354        when(InventoryUtil.fetchApiGateways(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());
355        PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateApiGatewayFiles",new HashMap<>());
356        
357        when(InventoryUtil.fetchIAMUsers(anyObject(), anyString(), anyString())).thenReturn(new HashMap<>());
358        PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateIamUserFiles",new HashMap<>());
359        
360        when(InventoryUtil.fetchRDSDBSnapshots(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());
361        PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateRDSSnapshotFiles",new HashMap<>());
362        
363        when(InventoryUtil.fetchIAMRoles(anyObject(), anyString(), anyString())).thenReturn(new HashMap<>());
364        PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateIamRoleFiles",new HashMap<>());
365        
366        when(InventoryUtil.fetchKMSKeys(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());
367        PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateKMSFiles",new HashMap<>());
368        
369        when(InventoryUtil.fetchCloudFrontInfo(anyObject(), anyString(), anyString())).thenReturn(new HashMap<>());
370        PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateCloudFrontFiles",new HashMap<>());
371        
372        when(InventoryUtil.fetchEBSInfo(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());
373        PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateEBSFiles",new HashMap<>());
374        
375        when(InventoryUtil.fetchPHDInfo(anyObject(), anyString(), anyString())).thenReturn(new HashMap<>());
376        PowerMockito.doThrow(new IOException()).when(FileManager.class,"generatePHDFiles",new HashMap<>());
377        
378        when(EC2InventoryUtil.fetchRouteTables(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());
379        PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateEC2RouteTableFiles",new HashMap<>());
380        
381        when(EC2InventoryUtil.fetchNetworkACL(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());
382        PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateNetworkAclFiles",new HashMap<>());
383        
384        when(EC2InventoryUtil.fetchElasticIPAddresses(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());
385        PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateElasticIPFiles",new HashMap<>());
386        
387        when(ASGInventoryUtil.fetchLaunchConfigurations(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());
388        PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateLaunchConfigurationsFiles",new HashMap<>());
389        
390        when(EC2InventoryUtil.fetchInternetGateway(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());
391        PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateInternetGatewayFiles",new HashMap<>());
392        
393        when(EC2InventoryUtil.fetchVPNGateway(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());
394        PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateVPNGatewayFiles",new HashMap<>());
395        
396        when(ASGInventoryUtil.fetchScalingPolicies(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());
397        PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateScalingPolicies",new HashMap<>());
398        
399        when(SNSInventoryUtil.fetchSNSTopics(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());
400        PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateSNSTopics",new HashMap<>());
401        
402        when(EC2InventoryUtil.fetchEgressGateway(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());
403        PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateEgressGateway",new HashMap<>());
404        
405        when(EC2InventoryUtil.fetchDHCPOptions(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());
406        PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateDhcpOptions",new HashMap<>());
407        
408        when(EC2InventoryUtil.fetchPeeringConnections(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());
409        PowerMockito.doThrow(new IOException()).when(FileManager.class,"generatePeeringConnections",new HashMap<>());
410        
411        when(EC2InventoryUtil.fetchCustomerGateway(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());
412        PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateCustomerGateway",new HashMap<>());
413        
414        when(EC2InventoryUtil.fetchVPNConnections(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());
415        PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateVpnConnection",new HashMap<>());
416        
417        when(DirectConnectionInventoryUtil.fetchDirectConnections(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());
418        PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateDirectConnection",new HashMap<>());
419        
420        when(DirectConnectionInventoryUtil.fetchDirectConnectionsVirtualInterfaces(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());
421        PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateDirectConnectionVirtualInterfaces",new HashMap<>());
422        
423        when(ESInventoryUtil.fetchESInfo(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());
424        PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateESDomain",new HashMap<>());
425        
426        when(EC2InventoryUtil.fetchReservedInstances(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());
427        PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateReservedInstances",new HashMap<>());
428        
429        when(EC2InventoryUtil.fetchSSMInfo(anyObject(), anyString(), anyString(), anyString())).thenReturn(new HashMap<>());
430        PowerMockito.doThrow(new IOException()).when(FileManager.class,"generateSsmFiles",new HashMap<>());
431        
432        List<Map<String,String>> accounts = new ArrayList<>();
433        Map<String,String> account = new HashMap<>();
434        account.put(InventoryConstants.ACCOUNT_ID, "account");
435        account.put(InventoryConstants.ACCOUNT_NAME, "accountName");
436        accounts.add(account);
437        assetFileGenerator.generateFiles(accounts , "skipRegions", "filePath");
438        
439    }
440}
441
Full Screen
copy
1/*
2 * Copyright (c) 2007 Mockito contributors
3 * This program is made available under the terms of the MIT License.
4 */
5package org.mockito.stubbing;
6
7import org.mockito.CheckReturnValue;
8import org.mockito.Mockito;
9import org.mockito.NotExtensible;
10
11/**
12 * Allows to choose a method when stubbing in doThrow()|doAnswer()|doNothing()|doReturn() style
13 * <p>
14 * Example:
15 * <pre class="code"><code class="java">
16 *   doThrow(new RuntimeException()).when(mockedList).clear();
17 *
18 *   //following throws RuntimeException:
19 *   mockedList.clear();
20 * </code></pre>
21 *
22 * Also useful when stubbing consecutive calls:
23 *
24 * <pre class="code"><code class="java">
25 *   doThrow(new RuntimeException("one")).
26 *   doThrow(new RuntimeException("two"))
27 *   .when(mock).someVoidMethod();
28 * </code></pre>
29 *
30 * Read more about those methods:
31 * <p>
32 * {@link Mockito#doThrow(Throwable[])}
33 * <p>
34 * {@link Mockito#doAnswer(Answer)}
35 * <p>
36 * {@link Mockito#doNothing()}
37 * <p>
38 * {@link Mockito#doReturn(Object)}
39 * <p>
40 *
41 * See examples in javadoc for {@link Mockito}
42 */
43@CheckReturnValue
44@SuppressWarnings("unchecked")
45@NotExtensible
46public interface Stubber extends BaseStubber {
47
48    /**
49     * Allows to choose a method when stubbing in doThrow()|doAnswer()|doNothing()|doReturn() style
50     * <p>
51     * Example:
52     * <pre class="code"><code class="java">
53     *   doThrow(new RuntimeException())
54     *   .when(mockedList).clear();
55     *
56     *   //following throws RuntimeException:
57     *   mockedList.clear();
58     * </code></pre>
59     *
60     * Read more about those methods:
61     * <p>
62     * {@link Mockito#doThrow(Throwable[])}
63     * <p>
64     * {@link Mockito#doAnswer(Answer)}
65     * <p>
66     * {@link Mockito#doNothing()}
67     * <p>
68     * {@link Mockito#doReturn(Object)}
69     * <p>
70     *
71     * See examples in javadoc for {@link Mockito}
72     *
73     * @param mock The mock
74     * @return select method for stubbing
75     */
76    <T> T when(T mock);
77}
78
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)