The error messages you are getting indicate that there is a null pointer exception occurring because the sharedConfig dependency in the class is not being injected properly during the JUnit tests. In particular, it states that it cannot invoke because is null.
public class ZolozClientTest extends IDVBaseTest {
code
@InjectMocks
private ZolozClient zolozClient;
@Mock
private RestTemplate restTemplate;
@Value("${zoloz.url}")
private String ZOLOZ_URL;
@Mock
private ZolozEncryptAndGenSignService zolozEncryptAndSignService;
@Mock
private ZolozCheckSignAndDecryptService checkSignatureAndDecryptService;
@BeforeEach
public void setup() {
MockitoAnnotations.openMocks(this);
ReflectionTestUtils.setField(zolozClient, "ZOLOZ_URL", ZOLOZ_URL);
ReflectionTestUtils.setField(zolozClient, "clientId", "2188438842658122");
}
@Test
public void testGetDemographics() throws Exception {
// Given
ZolozCheckResultRequest req = buildReq();
EncryptRequestDTO dto = new EncryptRequestDTO();
when(zolozEncryptAndSignService.encryptService(any())).thenReturn(dto);
when(zolozEncryptAndSignService.generateSignature(any(), any())).thenReturn(dto);
when(zolozEncryptAndSignService.generateSignature(any(), any())).thenReturn(dto);
when(restTemplate.exchange(anyString(), any(), any(), any(Class.class)))
.thenReturn(new ResponseEntity<String>(TestCommonUtils.getDemographyResponseJson(), HttpStatus.OK));
when(checkSignatureAndDecryptService.checkSignatureAndReturnData(any(), any())).thenReturn("dummy Content");
when(checkSignatureAndDecryptService.decryptResponse(any(), any(), any()))
.thenReturn(TestCommonUtils.getDemographyResponseJson());
// When
ZolozCheckResultResponse res = zolozClient.getDemographics(req);
// Then
assertNotNull(res);
}
@Test
public void testGetDemographicsNoData() throws Exception {
// Given
ZolozCheckResultRequest req = buildReq();
EncryptRequestDTO dto = new EncryptRequestDTO();
when(zolozEncryptAndSignService.encryptService(any())).thenReturn(dto);
when(zolozEncryptAndSignService.generateSignature(any(), any())).thenReturn(dto);
when(zolozEncryptAndSignService.generateSignature(any(), any())).thenReturn(dto);
when(restTemplate.exchange(anyString(), any(), any(), any(Class.class)))
.thenReturn(new ResponseEntity<>(HttpStatus.NO_CONTENT));
Exception exception = assertThrows(Exception.class, () -> {
zolozClient.getDemographics(req);
});
// Then
assertEquals("No data from the External system", exception.getMessage());
}
@Test
public void testGetDemographicsTimeoutException() throws Exception {
// Given
ZolozCheckResultRequest req = buildReq();
EncryptRequestDTO dto = new EncryptRequestDTO();
when(zolozEncryptAndSignService.encryptService(any())).thenReturn(dto);
when(zolozEncryptAndSignService.generateSignature(any(), any())).thenReturn(dto);
when(zolozEncryptAndSignService.generateSignature(any(), any())).thenReturn(dto);
when(restTemplate.exchange(anyString(), any(), any(), any(Class.class)))
.thenThrow(new RestClientException("Timeout", new TimeoutErrorException(ZOLOZ_URL, ZOLOZ_URL)));
;
Exception exception = assertThrows(DownstreamSystemErrorException.class, () -> {
// When
zolozClient.getDemographics(req);
});
// Then
assertEquals("Timeout", exception.getMessage());
}
@Test
public void testGetDemographicsServiceUnavailalbleException() throws Exception {
// Given
ZolozCheckResultRequest req = buildReq();
EncryptRequestDTO dto = new EncryptRequestDTO();
when(zolozEncryptAndSignService.encryptService(any())).thenReturn(dto);
when(zolozEncryptAndSignService.generateSignature(any(), any())).thenReturn(dto);
when(zolozEncryptAndSignService.generateSignature(any(), any())).thenReturn(dto);
when(restTemplate.exchange(anyString(), any(), any(), any(Class.class)))
.thenThrow(new RestClientException("Services Unavailable", new ConnectException()));
Exception exception = assertThrows(DownstreamSystemErrorException.class, () -> {
// When
zolozClient.getDemographics(req);
});
// Then
assertEquals("Services Down", exception.getMessage());
}
@Test
public void testGetDemographicsEndpointNotFoundException() throws Exception {
// Given
ZolozCheckResultRequest req = buildReq();
EncryptRequestDTO dto = new EncryptRequestDTO();
when(zolozEncryptAndSignService.encryptService(any())).thenReturn(dto);
when(zolozEncryptAndSignService.generateSignature(any(), any())).thenReturn(dto);
when(zolozEncryptAndSignService.generateSignature(any(), any())).thenReturn(dto);
when(restTemplate.exchange(anyString(), any(), any(), any(Class.class)))
.thenThrow(new RestClientException("Endpoint not found", new FileNotFoundException()));
Exception exception = assertThrows(DownstreamSystemErrorException.class, () -> {
// When
zolozClient.getDemographics(req);
});
// Then
assertEquals("Endpoint not found", exception.getMessage());
}
@Test
public void testGetDemographicsOtherException() throws Exception {
// Given
ZolozCheckResultRequest req = buildReq();
EncryptRequestDTO dto = new EncryptRequestDTO();
when(zolozEncryptAndSignService.encryptService(any())).thenReturn(dto);
when(zolozEncryptAndSignService.generateSignature(any(), any())).thenReturn(dto);
when(zolozEncryptAndSignService.generateSignature(any(), any())).thenReturn(dto);
when(restTemplate.exchange(anyString(), any(), any(), any(Class.class)))
.thenThrow(new RestClientException("Unknown exception"));
Exception exception = assertThrows(Exception.class, () -> {
// When
zolozClient.getDemographics(req);
});
// Then
assertEquals("Unknown exception", exception.getMessage());
}
@Test
public void testInitialize() throws Exception {
// Given
ZolozInitializeRequest req = buildZolozInitializeReq();
EncryptRequestDTO dto = new EncryptRequestDTO();
when(zolozEncryptAndSignService.encryptService(any())).thenReturn(dto);
when(zolozEncryptAndSignService.generateSignature(any(), any())).thenReturn(dto);
when(zolozEncryptAndSignService.generateSignature(any(), any())).thenReturn(dto);
when(restTemplate.exchange(anyString(), any(), any(), any(Class.class)))
.thenReturn(new ResponseEntity<String>(TestCommonUtils.getInitializeResponseJson(), HttpStatus.OK));
when(checkSignatureAndDecryptService.checkSignatureAndReturnData(any(), any())).thenReturn("dummy Content");
when(checkSignatureAndDecryptService.decryptResponse(any(), any(), any()))
.thenReturn(TestCommonUtils.getInitializeResponseJson());
// When
ZolozInitializeResponse res = zolozClient.initilize(req);
// Then
assertNotNull(res);
}
@Test
public void testInitializeNoData() throws Exception {
// Given
ZolozInitializeRequest req = buildZolozInitializeReq();
EncryptRequestDTO dto = new EncryptRequestDTO();
when(zolozEncryptAndSignService.encryptService(any())).thenReturn(dto);
when(zolozEncryptAndSignService.generateSignature(any(), any())).thenReturn(dto);
when(zolozEncryptAndSignService.generateSignature(any(), any())).thenReturn(dto);
when(restTemplate.exchange(anyString(), any(), any(), any(Class.class)))
.thenReturn(new ResponseEntity<>(HttpStatus.NO_CONTENT));
Exception exception = assertThrows(Exception.class, () -> {
zolozClient.initilize(req);
});
// Then
assertEquals("No data from the External system", exception.getMessage());
}
@Test
public void testInitializeTimeoutException() throws Exception {
// Given
ZolozInitializeRequest req = buildZolozInitializeReq();
EncryptRequestDTO dto = new EncryptRequestDTO();
when(zolozEncryptAndSignService.encryptService(any())).thenReturn(dto);
when(zolozEncryptAndSignService.generateSignature(any(), any())).thenReturn(dto);
when(zolozEncryptAndSignService.generateSignature(any(), any())).thenReturn(dto);
when(restTemplate.exchange(anyString(), any(), any(), any(Class.class)))
.thenThrow(new RestClientException("", new SocketTimeoutException()));
;
Exception exception = assertThrows(TimeoutErrorException.class, () -> {
// When
zolozClient.initilize(req);
});
// Then
assertEquals("Time Out", exception.getMessage());
}
@Test
public void testInitializeServiceUnavailalbleException() throws Exception {
// Given
ZolozInitializeRequest req = buildZolozInitializeReq();
EncryptRequestDTO dto = new EncryptRequestDTO();
when(zolozEncryptAndSignService.encryptService(any())).thenReturn(dto);
when(zolozEncryptAndSignService.generateSignature(any(), any())).thenReturn(dto);
when(zolozEncryptAndSignService.generateSignature(any(), any())).thenReturn(dto);
when(restTemplate.exchange(anyString(), any(), any(), any(Class.class)))
.thenThrow(new RestClientException("Services Unavailable", new ConnectException()));
Exception exception = assertThrows(DownstreamSystemErrorException.class, () -> {
// When
zolozClient.initilize(req);
});
// Then
assertEquals("Services Down", exception.getMessage());
}
@Test
public void testInitializeEndpointNotFoundException() throws Exception {
// Given
ZolozInitializeRequest req = buildZolozInitializeReq();
EncryptRequestDTO dto = new EncryptRequestDTO();
when(zolozEncryptAndSignService.encryptService(any())).thenReturn(dto);
when(zolozEncryptAndSignService.generateSignature(any(), any())).thenReturn(dto);
when(zolozEncryptAndSignService.generateSignature(any(), any())).thenReturn(dto);
when(restTemplate.exchange(anyString(), any(), any(), any(Class.class)))
.thenThrow(new RestClientException("Endpoint not found", new FileNotFoundException()));
Exception exception = assertThrows(DownstreamSystemErrorException.class, () -> {
// When
zolozClient.initilize(req);
});
// Then
assertEquals("Endpoint not found", exception.getMessage());
}
@Test
public void testInitializeOtherException() throws Exception {
// Given
ZolozInitializeRequest req = buildZolozInitializeReq();
EncryptRequestDTO dto = new EncryptRequestDTO();
when(zolozEncryptAndSignService.encryptService(any())).thenReturn(dto);
when(zolozEncryptAndSignService.generateSignature(any(), any())).thenReturn(dto);
when(zolozEncryptAndSignService.generateSignature(any(), any())).thenReturn(dto);
when(restTemplate.exchange(anyString(), any(), any(), any(Class.class)))
.thenThrow(new RestClientException("Unknown exception"));
Exception exception = assertThrows(Exception.class, () -> {
// When
zolozClient.initilize(req);
});
// Then
assertEquals("Unknown exception", exception.getMessage());
}
@Test
public void testDeleteCustomerDataFromVendor() throws Exception {
String vendorTrasactionId = "XXXXXXXXXXXXXXXXXXXXX1223";
DeleteCustomerRequest req = new DeleteCustomerRequest(vendorTrasactionId);
EncryptRequestDTO dto = new EncryptRequestDTO();
when(zolozEncryptAndSignService.encryptService(any())).thenReturn(dto);
when(zolozEncryptAndSignService.generateSignature(any(), any())).thenReturn(dto);
when(zolozEncryptAndSignService.generateSignature(any(), any())).thenReturn(dto);
when(restTemplate.exchange(anyString(), any(), any(), any(Class.class)))
.thenReturn(new ResponseEntity<String>(TestCommonUtils.getInitializeResponseJson(), HttpStatus.OK));
when(checkSignatureAndDecryptService.checkSignatureAndReturnData(any(), any())).thenReturn("dummy Content");
when(checkSignatureAndDecryptService.decryptResponse(any(), any(), any()))
.thenReturn(TestCommonUtils.getInitializeResponseJson());
// When
DeleteCustomerResponse res = zolozClient.deleteCustomerDataFromVendor(req);
// Then
assertNotNull(res);
}
@Test
public void testDeleteCustomerDataFromVendorNoData() throws Exception {
// Given
EncryptRequestDTO dto = new EncryptRequestDTO();
String vendorTrasactionId = "XXXXXXXXXXXXXXXXXXXXX1223";
DeleteCustomerRequest req = new DeleteCustomerRequest(vendorTrasactionId);
dto = new EncryptRequestDTO();
when(zolozEncryptAndSignService.encryptService(any())).thenReturn(dto);
when(zolozEncryptAndSignService.generateSignature(any(), any())).thenReturn(dto);
when(zolozEncryptAndSignService.generateSignature(any(), any())).thenReturn(dto);
when(restTemplate.exchange(anyString(), any(), any(), any(Class.class)))
.thenReturn(new ResponseEntity<>(HttpStatus.NO_CONTENT));
Exception exception = assertThrows(Exception.class, () -> {
zolozClient.deleteCustomerDataFromVendor(req);
});
// Then
assertEquals("No data from the External system", exception.getMessage());
}
@Test
public void testDeleteCustomerDataFromVendorTimeoutException() throws Exception {
// Given
String vendorTrasactionId = "XXXXXXXXXXXXXXXXXXXXX1223";
DeleteCustomerRequest req = new DeleteCustomerRequest(vendorTrasactionId);
EncryptRequestDTO dto = new EncryptRequestDTO();
when(zolozEncryptAndSignService.encryptService(any())).thenReturn(dto);
when(zolozEncryptAndSignService.generateSignature(any(), any())).thenReturn(dto);
when(zolozEncryptAndSignService.generateSignature(any(), any())).thenReturn(dto);
when(restTemplate.exchange(anyString(), any(), any(), any(Class.class))).thenThrow(
new RestClientException("Timeout", new TimeoutErrorException(vendorTrasactionId, vendorTrasactionId)));
;
Exception exception = assertThrows(DownstreamSystemErrorException.class, () -> {
// When
zolozClient.deleteCustomerDataFromVendor(req);
});
// Then
assertEquals("Timeout", exception.getMessage());
}
@Test
public void testDeleteCustomerDataFromVendorServiceUnavailalbleException() throws Exception {
// Given
String vendorTrasactionId = "XXXXXXXXXXXXXXXXXXXXX1223";
DeleteCustomerRequest req = new DeleteCustomerRequest(vendorTrasactionId);
EncryptRequestDTO dto = new EncryptRequestDTO();
when(zolozEncryptAndSignService.encryptService(any())).thenReturn(dto);
when(zolozEncryptAndSignService.generateSignature(any(), any())).thenReturn(dto);
when(zolozEncryptAndSignService.generateSignature(any(), any())).thenReturn(dto);
when(restTemplate.exchange(anyString(), any(), any(), any(Class.class)))
.thenThrow(new RestClientException("Services Unavailable", new ConnectException()));
Exception exception = assertThrows(DownstreamSystemErrorException.class, () -> {
// When
zolozClient.deleteCustomerDataFromVendor(req);
});
// Then
assertEquals("Services Down", exception.getMessage());
}
@Test
public void testDeleteCustomerDataFromVendorEndpointNotFoundException() throws Exception {
// Given
String vendorTrasactionId = "XXXXXXXXXXXXXXXXXXXXX1223";
DeleteCustomerRequest req = new DeleteCustomerRequest(vendorTrasactionId);
EncryptRequestDTO dto = new EncryptRequestDTO();
when(zolozEncryptAndSignService.encryptService(any())).thenReturn(dto);
when(zolozEncryptAndSignService.generateSignature(any(), any())).thenReturn(dto);
when(zolozEncryptAndSignService.generateSignature(any(), any())).thenReturn(dto);
when(restTemplate.exchange(anyString(), any(), any(), any(Class.class)))
.thenThrow(new RestClientException("Endpoint not found", new FileNotFoundException()));
Exception exception = assertThrows(DownstreamSystemErrorException.class, () -> {
// When
zolozClient.deleteCustomerDataFromVendor(req);
});
// Then
assertEquals("Endpoint not found", exception.getMessage());
}
@Test
public void testDeleteCustomerDataFromVendorOtherException() throws Exception {
// Given
String vendorTrasactionId = "XXXXXXXXXXXXXXXXXXXXX1223";
DeleteCustomerRequest req = new DeleteCustomerRequest(vendorTrasactionId);
EncryptRequestDTO dto = new EncryptRequestDTO();
when(zolozEncryptAndSignService.encryptService(any())).thenReturn(dto);
when(zolozEncryptAndSignService.generateSignature(any(), any())).thenReturn(dto);
when(zolozEncryptAndSignService.generateSignature(any(), any())).thenReturn(dto);
when(restTemplate.exchange(anyString(), any(), any(), any(Class.class)))
.thenThrow(new RestClientException("Unknown exception"));
Exception exception = assertThrows(Exception.class, () -> {
// When
zolozClient.deleteCustomerDataFromVendor(req);
});
// Then
assertEquals("Unknown exception", exception.getMessage());
}
private ZolozInitializeRequest buildZolozInitializeReq() {
ZolozInitializeRequest req = new ZolozInitializeRequest();
H5ModeConfig config = new H5ModeConfig();
config.setCompleteCallbackUrl("http://xxx.html");
config.setIsIframe("http://xxx.html");
req.setBizId("TXNREF1001");
req.setH5ModeConfig(config);
return req;
}
private ZolozCheckResultRequest buildReq() {
ZolozCheckResultRequest req = new ZolozCheckResultRequest();
req.setBizId("BIZID");
req.setTransactionId("TXN1001");
return req;
}
}
I am getting the below error.
[ERROR] [1;31mFailures: [m
[ERROR] [1;31m ZolozClientTest.testDeleteCustomerDataFromVendorEndpointNotFoundException:389 expected: but was: <Cannot invoke "com.sc.we.idv.config.SharedConfig.isShouldStubInitialize()" because "this.sharedConfig" is null>[m
[ERROR] [1;31m ZolozClientTest.testDeleteCustomerDataFromVendorNoData:328 expected: but was: <Cannot invoke "com.sc.we.idv.config.SharedConfig.isShouldStubInitialize()" because "this.sharedConfig" is null>[m
[ERROR] [1;31m ZolozClientTest.testDeleteCustomerDataFromVendorOtherException:409 expected: but was: <Cannot invoke "com.sc.we.idv.config.SharedConfig.isShouldStubInitialize()" because "this.sharedConfig" is null>[m
[ERROR] [1;31m ZolozClientTest.testDeleteCustomerDataFromVendorServiceUnavailalbleException:369 expected: but was: <Cannot invoke "com.sc.we.idv.config.SharedConfig.isShouldStubInitialize()" because "this.sharedConfig" is null>[m
[ERROR] [1;31m ZolozClientTest.testDeleteCustomerDataFromVendorTimeoutException:349 expected: but was: <Cannot invoke "com.sc.we.idv.config.SharedConfig.isShouldStubInitialize()" because "this.sharedConfig" is null>[m
[ERROR] [1;31m ZolozClientTest.testGetDemographicsEndpointNotFoundException:155 expected: but was: <Cannot invoke "com.sc.we.idv.config.SharedConfig.isShouldStubInitialize()" because "this.sharedConfig" is null>[m
[ERROR] [1;31m ZolozClientTest.testGetDemographicsNoData:97 expected: but was: <Cannot invoke "com.sc.we.idv.config.SharedConfig.isShouldStubInitialize()" because "this.sharedConfig" is null>[m
[ERROR] [1;31m ZolozClientTest.testGetDemographicsOtherException:174 expected: but was: <Cannot invoke "com.sc.we.idv.config.SharedConfig.isShouldStubInitialize()" because "this.sharedConfig" is null>[m
[ERROR] [1;31m ZolozClientTest.testGetDemographicsServiceUnavailalbleException:136 expected: but was: <Cannot invoke "com.sc.we.idv.config.SharedConfig.isShouldStubInitialize()" because "this.sharedConfig" is null>[m
[ERROR] [1;31m ZolozClientTest.testGetDemographicsTimeoutException:117 expected: but was: <Cannot invoke "com.sc.we.idv.config.SharedConfig.isShouldStubInitialize()" because "this.sharedConfig" is null>[m
[ERROR] [1;31m ZolozClientTest.testInitializeEndpointNotFoundException:268 expected: but was: <Cannot invoke "com.sc.we.idv.config.SharedConfig.isShouldStubInitialize()" because "this.sharedConfig" is null>[m
[ERROR] [1;31m ZolozClientTest.testInitializeNoData:210 expected: but was: <Cannot invoke "com.sc.we.idv.config.SharedConfig.isShouldStubInitialize()" because "this.sharedConfig" is null>[m
[ERROR] [1;31m ZolozClientTest.testInitializeOtherException:287 expected: but was: <Cannot invoke "com.sc.we.idv.config.SharedConfig.isShouldStubInitialize()" because "this.sharedConfig" is null>[m
[ERROR] [1;31m ZolozClientTest.testInitializeServiceUnavailalbleException:249 expected: but was: <Cannot invoke "com.sc.we.idv.config.SharedConfig.isShouldStubInitialize()" because "this.sharedConfig" is null>[m
[ERROR] [1;31m ZolozClientTest.testInitializeTimeoutException:224 Unexpected exception type thrown, expected: <com.sc.we.idv.exceptions.TimeoutErrorException> but was: <com.sc.we.idv.common.exceptions.DownstreamSystemErrorException>[m
[ERROR] [1;31mErrors: [m
[ERROR] [1;31m ZolozClientTest.testDeleteCustomerDataFromVendor:304 » DownstreamSystemError Cannot invoke "com.sc.we.idv.config.SharedConfig.isShouldStubInitialize()" because "this.sharedConfig" is null[m
[ERROR] [1;31m ZolozClientTest.testGetDemographics:78 » DownstreamSystemError Cannot invoke "com.sc.we.idv.config.SharedConfig.isShouldStubInitialize()" because "this.sharedConfig" is null[m
[ERROR] [1;31m ZolozClientTest.testInitialize:191 » DownstreamSystemError Cannot invoke "com.sc.we.idv.config.SharedConfig.isShouldStubInitialize()" because "this.sharedConfig" is null
How to resolve it?
ZolozClient
isShouldStubInitialize()
sharedConfig
To resolve this issue, you need to add a mock for the SharedConfig class in your test class and make sure it is injected properly into the ZolozClient instance.
Here are the steps to fix the problem:
Add a mock for the SharedConfig class in your test class.
Ensure that the mock is initialized properly.
Here’s how you can modify your ZolozClientTest class:
java
import com.sc.we.idv.config.SharedConfig;
// other imports...
public class ZolozClientTest extends IDVBaseTest {
@InjectMocks
private ZolozClient zolozClient;
@Mock
private RestTemplate restTemplate;
@Mock
private ZolozEncryptAndGenSignService zolozEncryptAndSignService;
@Mock
private ZolozCheckSignAndDecryptService checkSignatureAndDecryptService;
@Mock
private SharedConfig sharedConfig; // Add this line for SharedConfig
@Value("${zoloz.url}")
private String ZOLOZ_URL;
@BeforeEach
public void setup() {
MockitoAnnotations.openMocks(this);
ReflectionTestUtils.setField(zolozClient, "ZOLOZ_URL", ZOLOZ_URL);
ReflectionTestUtils.setField(zolozClient, "clientId", "2188438842658122");
// Optionally mock behaviors for sharedConfig if needed
when(sharedConfig.isShouldStubInitialize()).thenReturn(false); // or true if necessary
}
// ... (rest of your test cases)
}
Key Changes Made:
Added a mock for SharedConfig in the test class.
Used when(sharedConfig.isShouldStubInitialize()).thenReturn(...) to provide behavior for the mock, returning either true or false based on what your test requires.
Additional Note:
Make sure to initialize sharedConfig properly and set expected behaviors for all mocked dependencies that your ZolozClient might interact with. This setup ensures that during your tests, when methods are called on the ZolozClient, it will function correctly without encountering null pointer exceptions.
Now, rerun your test cases after making these changes, and the null reference issue should be resolved.