We are integrating our application with JIRA, to create an issue with attachment.
Method 1:
public JSONObject createIssue() {
IssueRestClient issueClient = restClient.getIssueClient();
MetadataRestClient metadataClient = restClient.getMetadataClient();
Map<String, Long> priorityMap = new HashMap<>();
Map<String, Long> issueMap = new HashMap<>();
Map<String, String> fieldsMap = new HashMap<>();
// To get the list of all types of priorities
metadataClient.getPriorities().claim().forEach(priorityType -> priorityMap.put(priorityType.getName(), priorityType.getId()));
// To get the list of all issue types
metadataClient.getIssueTypes().claim().forEach(issueType -> issueMap.put(issueType.getName(), issueType.getId()));
// To get the list of all fields, which includes the custom fields
metadataClient.getFields().claim().forEach(fieldType -> fieldsMap.put(fieldType.getName(), fieldType.getId()));
// skipped the remaining code
}
Method 2:
public void addAttachmentToIssue(String issueId, InputStream inputStream, String fileNameWithExtension) {
Issue issue = null;
IssueRestClient issueClient = restClient.getIssueClient();
Promise<Issue> promiseIssue = issueClient.getIssue(issueId);
issue = promiseIssue.claim();
issueClient.addAttachment(issue.getAttachmentsUri(), inputStream, fileNameWithExtension);
}
I'm writing unit tests using Mockito.
public class JIRATest{
#InjectMocks
JIRAService mockJiraService;
JiraRestClient mockJiraRestClient;
IssueRestClient mockIssueRestClient;
Promise mockPromise;
Issue mockIssue;
#BeforeEach
public void setup() {
mockJiraService = new JIRAService();
mockJiraRestClient = mock(JiraRestClient.class);
mockIssueRestClient = mock(IssueRestClient.class);
mockJiraService.setRestClient(mockJiraRestClient);
mockPromise = mock(Promise.class);
mockIssue = mock(Issue.class);
}
#Test
public void addAttachmentTest(){
Mockito.when(mockJiraRestClient.getIssueClient()).thenReturn(mockIssueRestClient);
Mockito.when(mockIssueRestClient.getIssue("DCU")).thenReturn(mockPromise);
Mockito.when(mockPromise.claim()).thenReturn(mockIssue);
Mockito.when(mockIssueRestClient.addAttachment(any(), any(), any()));
String webSite = "https://www.google.com";
URL url = new URL(webSite);
InputStream stream = url.openStream();
mockJiraService.addAttachmentToIssue("issueId", stream, "lowes-webpage.html");
}
}
How to mock these below objects, so that i wanted to use it in thenReturn() of Mockito?
1. Promise<Issue>
2. metadataClient.getPriorities() return Promise<Iterable<Priority>>
3. metadataClient.getPriorities().claim() return Iterable<Priority>
4. metadataClient.getIssueTypes() return Promise<Iterable<IssueType>>
5. metadataClient.getPriorities().claim() return Iterable<IssueType>
6. metadataClient.getFields() return Promise<Iterable<Field>>
7. metadataClient.getFields().claim() return Iterable<Field>
Related
I have a method that collects data from multiple factories using CompletableFuture.supplyAsync like below
#RunWith(MockitoJUnitRunner.StrictStubs.class)
public class TestThenApply {
#Mock private Factory1 factory1;
#Mock private Factory2 factory2;
#Mock private Factory3 factory3;
#Test
public void case1() throws Exception {
when(factory1.create()).thenReturn("factory1");
when(factory2.create()).thenReturn("factory2");
when(factory3.create()).thenReturn("factory3");
Map<String, Object> expected = new HashMap<>();
expected.put("factory1", "factory1");
expected.put("factory2", "factory2");
expected.put("factory3", "factory3");
assertEquals(expected, getModel());
}
#Test
public void case2() throws Exception {
when(factory1.create()).thenThrow(new RuntimeException("Test!!"));
when(factory2.create()).thenReturn("factory2");
when(factory3.create()).thenReturn("factory3");
Map<String, Object> expected = new HashMap<>();
expected.put("factory1", null);
expected.put("factory2", "factory2");
expected.put("factory3", "factory3");
assertEquals(expected, getModel());
}
#Test
public void case3() throws Exception {
when(factory1.create()).thenReturn("factory1");
when(factory2.create()).thenThrow(new RuntimeException("Test!!"));
when(factory3.create()).thenReturn("factory3");
Map<String, Object> expected = new HashMap<>();
expected.put("factory1", "factory1");
expected.put("factory2", null);
expected.put("factory3", "factory3");
assertEquals(expected, getModel());
}
#Test
public void case4() throws Exception {
when(factory1.create()).thenReturn("factory1");
when(factory2.create()).thenReturn("factory2");
when(factory3.create()).thenThrow(new RuntimeException("Test!!"));
Map<String, Object> expected = new HashMap<>();
expected.put("factory1", "factory1");
expected.put("factory2", "factory2");
expected.put("factory3", null);
assertEquals(expected, getModel());
}
private Map<String, Object> getModel() {
Map<String, Object> map = new HashMap<>();
CompletableFuture<Void> factory1Future = supplyAsyncWithCallable("factory1", () -> factory1.create())
.thenAccept(result -> map.put("factory1", result));
CompletableFuture<Void> factory2Future = supplyAsyncWithCallable("factory2", () -> factory2.create())
.thenAccept(result -> map.put("factory2", result));
CompletableFuture<Void> factory3Future = supplyAsyncWithCallable("factory3", () -> factory3.create())
.thenAccept(result -> map.put("factory3", result));
CompletableFuture.allOf(factory1Future, factory2Future, factory3Future).join();
return map;
}
private <T> CompletableFuture<T> supplyAsyncWithCallable(String widgetName, Callable<T> widgetModelBuilder) {
return CompletableFuture.supplyAsync(() -> {
try {
return widgetModelBuilder.call();
} catch (Exception e) {
return null;
}
});
}
}
I have 4 unit tests which tests below scenarios
When all 3 factories succeeds // Happy case
When factory1 fails but other 2 factories succeed
When factory2 fails but other 2 factories succeed
When factory3 fails but other 2 factories succeed
90% of the time all these test cases pass however one test case fails (assertion error) randomly sometimes.
I suspect this might be because thenAccept does not run before the main thread ends. I was able to get it working all the time by moving all the thenAccept logic after the join(). However I don't want to do that unless I have to. Any help is appreciated.
Thanks to #ernest_k
Using HashMap was the issue here. Replaced it with thread safe map and now the test cases pass consistently.
I am new to Junit 5 . There are two functions in the class under test , The first function calls the second function and second function returns a value which is used in the first function for processing .
So I have created a mock for this class but not able to mock the second function call When I am testing the first function .
First function --exportOpportunityListing()
Second function -- entityToCsvReport()
public class OpportunityReportServiceImpl extends BaseService implements OpportunityReportService {
#Value("${nfs.mountPath}")
private String fileMountPath;
#Value("${take1.url.host}")
private String take1HostURL;
#Autowired
UsersRepository usersRepository;
#Autowired
MailUtil mailUtil;
#Autowired
OpportunityJDBCRepository ojdbc;
#Override
#Async
public void exportOpportunityListing(Map<String, Object> paramMap, List<OpportunityCriteria> lfvo,
String xRemoteUser) {
try {
List<OpportunityJDBCDTO> lo = ojdbc.getOppListWithoutPagination(paramMap, lfvo);
List<OpportunityReport> exportData = lo.parallelStream().map(this::entityToCsvReport)
.collect(Collectors.toList());
CsvCustomMappingStrategy<OpportunityReport> mappingStrategy = new CsvCustomMappingStrategy<>();
mappingStrategy.setType(OpportunityReport.class);
String dirPath = fileMountPath + REPORT_PATH;
File fileDir = new File(dirPath);
if (!fileDir.exists()) {
FileUtils.forceMkdir(fileDir);
}
String pathWithoutExtension = dirPath + "opportunity_data_"
+ LocalDateTime.now().format(DateTimeFormatter.ofPattern(YYYYMMDDHHMMSS));
File reportFile = new File(pathWithoutExtension + EXTENSION_CSV);
Writer writer = new PrintWriter(reportFile);
StatefulBeanToCsv<OpportunityReport> beanToCsv = new StatefulBeanToCsvBuilder<OpportunityReport>(writer)
.withMappingStrategy(mappingStrategy).build();
beanToCsv.write(exportData);
writer.close();
String zipFilePath = pathWithoutExtension + EXTENSION_ZIP;
ZipUtil.zip(reportFile, zipFilePath);
Users remoteUser = usersRepository.findByUsername(xRemoteUser)
.orElseThrow(() -> new Take1Exception(ErrorMessage.USER_NOT_FOUND_WITH_USERNAME, xRemoteUser));
Mail mail = Mail.builder().to(new String[] { remoteUser.getEmail() })
.model(MailModel.builder().name(remoteUser.getName())
.body("Please find attached the opportunity report you requested.").build())
.subject("Opportunity Report").attachments(Arrays.asList(new File(zipFilePath))).build();
mailUtil.sendMail(mail);
Files.delete(reportFile.toPath());
} catch (IOException | CsvDataTypeMismatchException | CsvRequiredFieldEmptyException e) {
throw new Take1Exception(ErrorMessage.INTERNAL_SERVER_EXCEPTION, e);
}
}
public OpportunityReport entityToCsvReport(OpportunityJDBCDTO o) {
OpportunityReport or = modelMapper.map(o, OpportunityReport.class);
or.setCurrency("USD");
or.setOnline(Boolean.TRUE.equals(o.getIsOnline()) ? "YES" : "NO");
return or;
}
}
Here is my JUnit Test case .
class OpportunityReportServiceImplTest {
#InjectMocks
OpportunityReportServiceImpl opportunityReportServiceImpl;
#Autowired
OpportunityReportServiceImpl ors;
#Mock
OpportunityJDBCRepository ojdbc;
#Mock
UsersRepository usersRepository;
#Mock
MailUtil mailUtil;
#Mock
ModelMapper mp;
String username = "anandabhishe";
String nfusername = "ananda";
Mail mail;
List<OpportunityJDBCDTO> lo = new ArrayList<OpportunityJDBCDTO>();
List<OpportunityReport> lor = new ArrayList<OpportunityReport>();
#BeforeEach
void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
ReflectionTestUtils.setField(opportunityReportServiceImpl, "fileMountPath", ".");
ReflectionTestUtils.setField(opportunityReportServiceImpl, "take1HostURL", "");
lo.add(new OpportunityJDBCDTO());
lor.add(new OpportunityReport());
}
#Test
void testExportOpportunityListing() throws IOException {
OpportunityReport or = new OpportunityReport();
or.setCurrency("USD");
or.setOnline("Yes");
when(ojdbc.getOppListWithoutPagination(getParamMap(), getOppCriteria())).thenReturn(lo);
when(usersRepository.findByUsername(username)).thenReturn(Optional.of(getUser()));
doNothing().when(mailUtil).sendMail(mail);
// doNothing().when(opportunityReportServiceImpl).entityToCsvReport(oj);
when(opportunityReportServiceImpl.entityToCsvReport(getOpportunityJDBCDTO())).thenReturn(or);
opportunityReportServiceImpl.exportOpportunityListing(getParamMap(), getOppCriteria(), username);
assertTrue(true);
FileUtils.forceDelete(new File("." + REPORT_PATH));
}
private Map<String, Object> getParamMap() {
return new HashMap<String, Object>();
}
private List<OpportunityCriteria> getOppCriteria() {
List<OpportunityCriteria> loc = new ArrayList<>();
loc.add(new OpportunityCriteria());
return loc;
}
private OpportunityJDBCDTO getOpportunityJDBCDTO() {
OpportunityJDBCDTO oj = new OpportunityJDBCDTO();
oj.setIsOnline(true);
oj.setApplicationCount(2);
oj.setCost(200);
oj.setCountryCode("in");
oj.setCreationDate(LocalDateTime.now());
oj.setEndDate(LocalDate.now());
oj.setLocation("test");
oj.setOpportunityId(123);
oj.setOpportunityStatus("test");
oj.setOpportunityStatusId(1);
oj.setOpportunityTitle("test");
oj.setOpportunityType("test");
oj.setOpportunityTypeColor("test");
oj.setOpportunityTypeId(1);
oj.setPublishedAt(LocalDateTime.now());
oj.setPublishedBy("test");
oj.setPublishedByUserName("test");
oj.setRegistrationUrl("test");
oj.setStartDate(LocalDate.now());
oj.setSummary("test");
oj.setUserEmail("test");
oj.setUserFullName("test");
oj.setUserId(1);
oj.setUserName("test");
oj.setVendorName("test");
return oj;
}
private Users getUser() {
Users user = new Users();
return user;
}
}
I am getting Null Pointer Exception when the line in Test class is called :
when(opportunityReportServiceImpl.entityToCsvReport(getOpportunityJDBCDTO())).thenReturn(or);
I was missing mocking the modelmapper stub which is being used in second function , after I added that , the test passed .
OpportunityReport or = new OpportunityReport();
OpportunityJDBCDTO oj = new OpportunityJDBCDTO();
when(ojdbc.getOppListWithoutPagination(any(HashMap.class), anyList())).thenReturn(lo);
when(usersRepository.findByUsername(anyString())).thenReturn(Optional.of(getUser()));
doNothing().when(mailUtil).sendMail(mail);
doReturn(or).when(mp).map(oj, OpportunityReport.class);
opportunityReportServiceImpl.exportOpportunityListing(getParamMap(), getOppCriteria(), username);
assertTrue(true);
That's happening because opportunityReportServiceImpl is not a mock - it's the object that you're trying to test, but you're trying to stub a method of it as if it were a mock.
I would recommend that you don't try to stub the methods of the object that you're trying to test. But if you have to, you'll need to declare it as a #Spy. Then to stub it, you'll need the doReturn/when syntax instead of when/thenReturn. This might look like
doReturn(lo).when(ojdbc).getOppListWithoutPagination(getParamMap(), getOppCriteria());
i am writing test cases for repository classes were i am not able to cover some of lines in repository classes. i need to achieve 85% of code coverage and its mandatory in my case,Please suggest me something
My actual method
public Map<String, String> getProductFamily(List<String> itmNms) {
Map<String, String> productFamilyMap=new HashMap<String, String>();
try {
NamedParameterJdbcTemplate namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(jdbcTemplate);
String sql = "some query";
MapSqlParameterSource namedParameters = new MapSqlParameterSource();
namedParameters.addValue("itmNms", itmNms);
productFamilyMap = namedParameterJdbcTemplate.query(sql, namedParameters, (ResultSet rs) -> {
Map<String, String> productFamily = new HashMap<>();
while (rs.next()) {
productFamily.put(rs.getString("ITEMNAME"), rs.getString("PRODUCTFAMILY"));
}
return productFamily;
});
}catch (Exception e) {
LOGGER.error("Exception in OracleRespository.getProductFamily : {}", e);
}
return productFamilyMap;
}
Test case for above method
#Test
public void getProductFamily() {
List<String> itmNms = new ArrayList<String>();
itmNms.add("A-SPK-NAMED-USER");
oracleRepo.getProductFamily(itmNms);
Map<String, String> mp = new HashMap<String, String>();
Assert.assertNull(mp);
}
By writing above test cases i am able to cover code coverage till line no 6 below lines i am not able to cover due to below statements
productFamilyMap = namedParameterJdbcTemplate.query(sql, namedParameters, (ResultSet rs) ->{}
Can some one suggest how can i achieve code coverage for above method as 100%.
In cases like that, you need to "manually invoke" the code in lambda. This can be performed with Mockito.doAnswer(...) functionality of Mockito framework. The example (suitable for Mockito 2+):
Mockito.doAnswer(invocationOnMock -> {
ResultSet resultSet = Mockito.mock(ResultSet.class);
Mockito.when(resultSet.next()).thenReturn(true).thenReturn(false);
Mockito.when(resultSet.getString("ITEMNAME")).thenReturn(...);
Mockito.when(resultSet.getString("PRODUCTFAMILY")).thenReturn(...);
ResultSetExtractor<Map<String, String>> resultSetExtractor =
invocationOnMock.getArgument(2);
return resultSetExtractor.extractData(resultSet);
}).when(namedParameterJdbcTemplate).query(
Mockito.anyString(),
Mockito.any(MapSqlParameterSource.class),
Mockito.any(ResultSetExtractor.class)
);
Then you can verify productFamilyMap for populated key-value pair.
If you'd still have troubles with it, you can share your code (e.g. via Github) and I'll try to help you with it.
EDIT: Initially, I didn't notice the thing that NamedParameterJdbcTemplate is created manually with new, and it's kinda hard to mock it. In this case, it's better to refactor your production code a bit - you can create NamedParameterJdbcTemplate object as bean (like you probably did with raw JdbcTemplate) and then inject it into your class (and ofc remove the line where you're creating it with new). Then the things become trivial.
#Component
public class OracleRepository {
private static final Logger LOGGER = LoggerFactory.getLogger(OracleRepository.class);
#Autowired
private NamedParameterJdbcTemplate namedParameterJdbcTemplate; //created as bean in configuration class
public Map<String, String> getProductFamily(List<String> itmNms) {
Map<String, String> productFamilyMap=new HashMap<String, String>();
try {
String sql = "some query";
MapSqlParameterSource namedParameters = new MapSqlParameterSource();
namedParameters.addValue("itmNms", itmNms);
productFamilyMap = namedParameterJdbcTemplate.query(sql, namedParameters, (ResultSet rs) -> {
Map<String, String> productFamily = new HashMap<>();
while (rs.next()) {
productFamily.put(rs.getString("ITEMNAME"), rs.getString("PRODUCTFAMILY"));
}
return productFamily;
});
}catch (Exception e) {
LOGGER.error("Exception in OracleRespository.getProductFamily : {}", e);
}
return productFamilyMap;
}
}
The test class remains unchanged:
#RunWith(MockitoJUnitRunner.class)
public class OracleRepositoryTest {
#InjectMocks
private OracleRepository oracleRepo;
#Mock
private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
#Test
public void getProductFamily() {
List<String> itmNms = new ArrayList<>();
itmNms.add("A-SPK-NAMED-USER");
Mockito.doAnswer(invocationOnMock ->{
ResultSet resultSet = Mockito.mock(ResultSet.class);
Mockito.when(resultSet.next()).thenReturn(true).thenReturn(false);
Mockito.when(resultSet.getString("ITEMNAME")).thenReturn("A-SPK-NAMED-USER");
Mockito.when(resultSet.getString("PRODUCTFAMILY")).thenReturn("SPKCLD");
ResultSetExtractor<Map<String, String>> resultSetExtractor =
invocationOnMock.getArgument(2);
return resultSetExtractor.extractData(resultSet);
}).when(namedParameterJdbcTemplate).query(
Mockito.anyString(),
Mockito.any(MapSqlParameterSource.class),
Mockito.any(ResultSetExtractor.class)
);
Map<String, String> productFamilyMap = oracleRepo.getProductFamily(itmNms);
Assert.assertEquals("SPKCLD", productFamilyMap.get("A-SPK-NAMED-USER"));
}
}
The above answer is accurate and is working. I tried in my project too.
Let me try to explain how this works.
We are saying when mocked JDBCTemplate query() method is called, then we want to invoke our own lambda expression with some mocking done, like we first create a mocked result set, and mocks some of its getString methods. Next we capture the third argument of mocked invocation which is result set extractor. Now from here we simply return this extractor extract data method with our mocked result set which will now be invoked.
So in essence we are calling the original extract data method with our mocked result set.
Mockito.doAnswer(invocationOnMock -> {
ResultSet resultSet = Mockito.mock(ResultSet.class);
Mockito.when(resultSet.next()).thenReturn(true).thenReturn(false);
Mockito.when(resultSet.getString("ITEMNAME")).thenReturn(...);
Mockito.when(resultSet.getString("PRODUCTFAMILY")).thenReturn(...);
ResultSetExtractor<Map<String, String>> resultSetExtractor =
invocationOnMock.getArgument(2);
return resultSetExtractor.extractData(resultSet);
}).when(namedParameterJdbcTemplate).query(
Mockito.anyString(),
Mockito.any(MapSqlParameterSource.class),
Mockito.any(ResultSetExtractor.class)
);
After doing lots of research, I didn't find the answer for this question in JUnits in java.
What I want to do is: To call some method on object returned by when().thenReturn(object) call.
e.g.:
public boolean checkUpdate(String str, String endStr){
GetEndpointRequest geaReq = new
GetEndpointRequest().withEndpointArn(endpointArn);
GetEndpointResult geaRes = amazonSNS.getEndpointAttributes(geaReq);
return !geaRes.getAttributes().get("Token").equals(token) || !geaRes.getAttributes().get("Enabled").equalsIgnoreCase("true");
}
And here is the Test method:
import com.amazonaws.services.sns.model.GetEndpointAttributesRequest;
import com.amazonaws.services.sns.model.GetEndpointAttributesResult;
#Tested
AmazonSNSRegistrationService service= new AmazonSNSRegistrationService();
service.amazonSNS = mock(AmazonSNS.class);
#Test
public void checkUpdateTest(){
String pushToken = "dxbv1fwJYIo";
String strToken = "";
String strEnabled = "";
String endPointArn = "";
Map<String, String> jsonBody = new HashMap<String, String>();
jsonBody.put("Token", "");
jsonBody.put("enabled", "");
GetEndpointAttributesRequest getEndpointReq =mock(GetEndpointAttributesRequest.class);
GetEndpointAttributesResult getEndpointRes =mock(GetEndpointAttributesResult.class);
getEndpointRes.setAttributes(jsonBody);
when(service.amazonSNS.getEndpointAttributes(getEndpointReq)).thenReturn(getEndpointRes);
when(getEndpointRes.getAttributes()).thenReturn(jsonBody);
when(getEndpointRes.getAttributes().get(strToken)).thenReturn("");
when(getEndpointRes.getAttributes().get(strEnabled)).thenReturn("");
amazonSNSRegistrationService.checkUpdate(pushToken, endPointArn);
}
I'm getting NullPointerException in checkUpdate() method on line -- "return !geaRes.getAttributes().get("Token").equals(token)".
As, geaRes is null.
How to solve this?
Adding another simple example , where I face this issue:
AmazonSNSRegistrationSerice.java:
public boolean deletePlatformApplicationArn(String deviceId, String appId){
boolean isArnDeleted = false;
try {
DeleteEndpointRequest deleteEndpointReq = new DeleteEndpointRequest().withEndpointArn(appId);
DeleteEndpointResult result = amazonSNS.deleteEndpoint(deleteEndpointReq);
if (result.getSdkHttpMetadata().getHttpStatusCode() == HttpStatus.SC_OK) {
AWSUtil.deleteArnEndpoint(deviceId, appId);
isArnDeleted = true;
}
} catch (Exception e) {
ErrorLogEventHelper.logErrorEvent(this.getClass().getName(), "Exception while deleting AWS ARN (endpoint)" + e.getMessage(), "deletePlatformApplicationArn", e, ErrorLogEvent.ERROR_SEVERITY);
}
return isArnDeleted;
}
AmazonSNSRegistrationSericeTest.java:
#Test
public void deletePlatformApplicationArnTest(){
String appId = "arn:aws:sns";
String deviceId = "dev_1";
DeleteEndpointRequest deleteEndpointReq = mock(DeleteEndpointRequest.class);
DeleteEndpointResult result = mock(DeleteEndpointResult.class);
when(amazonSNSRegistrationService.amazonSNS.deleteEndpoint(deleteEndpointReq)).thenReturn(result);
SdkHttpMetadata metadata = mock(SdkHttpMetadata.class);
when(result.getSdkHttpMetadata()).thenReturn(metadata);
when(result.getSdkHttpMetadata().getHttpStatusCode()).thenReturn(HttpStatus.SC_OK);
amazonSNSRegistrationService.deletePlatformApplicationArn(deviceId, appId);
}
Again the result object in if(result.getSdkHttpMetadata()) is coming NULL.
So first of all, you're trying to get an empty value out of your JsonMap:
#Test
public void checkUpdateTest(){
String strToken = "";
.
.
.
when(getEndpointRes.getAttributes().get(strToken)).thenReturn("");
// so what you ask here is this:
when(getEndpointRes.getAttributes().get("")).thenReturn("");
// this should probably be "Token"
when(getEndpointRes.getAttributes().get("Token")).thenReturn("");
}
the next thing is, the JsonMap is a real object and not a mock so you don't need to call when(getEndpointRes.getAttributes().get(strToken)).thenReturn(""); as your jsonmap is going to return the right value.
then, this call is not needed as your getEndpointRes is a mock. That's why you use the when().thenX() syntax
getEndpointRes.setAttributes(jsonBody);
To resolve your NPE problem try the following:
// imports...
#Tested
AmazonSNSRegistrationService service= new AmazonSNSRegistrationService();
#Test
public void checkUpdateTest(){
// directly mock the amazonsns here
AmazonSNS amazonSNS = mock(AmazonSNS.class);
service.amazonSNS = amazonSNS;
String pushToken = "dxbv1fwJYIo";
String endPointArn = "";
// maybe mock them aswell
Map<String, String> jsonBody = new HashMap<String, String>();
jsonBody.put("Token", "");
jsonBody.put("enabled", "");
// deleted the mocked request. You don't need it in this case as your service contructs it by itself
GetEndpointAttributesResultgetEndpointRes getEndpointRes = mock(GetEndpointAttributesResult.class);
getEndpointRes.setAttributes(jsonBody);
// directly use the mocked object and react on any() as getEntpointReq never will be present in your service!
when(amazonSNS.getEndpointAttributes(any())).thenReturn(getEndpointRes);
when(getEndpointRes.getAttributes()).thenReturn(jsonBody);
amazonSNSRegistrationService.checkUpdate(pushToken, endPointArn);
// do some further assertments
}
It seems that you try to mock things that you don't need to mock and mock things that are generated in your service. If you ever encounter a NPE in the classes you are trying to test you should debug your test properly. In your case the mocking seems incorrect and therefore the real object calls result in NPEs
In your second example it seems to be the same issue. you try to return something from an mock, that is probably created inside your amazonSNSRegistrationService. How should your mocked deleteEndpointReq return any value if it is not part of the class? You simple create the mock, but it is not the same object that is created inside of you service!
And after all you should add some assertions. The tests I see here only fail if the tested class fails with an error like in your case. But some basic assertions are missing. I personally use the AAA pattern for unit tests.
Im trying to create a java post request to create a test run on test rail, however it doesn't seem to be working heres my code:
public class create_run {
public JSONObject AddTestRunTest() throws IOException, APIException {
JSONObject jsonobject = new JSONObject();
APIClient client = new APIClient("https://stdec.testrail.com/");
client.setUser("fea#sportdec.com");
client.setPassword("Fa1");
Map<String, Object> map = new HashMap<String, Object>();
map.put("suite_id", 829);
map.put("assignedto_id", 1);
map.put("name", "Regression Test");
map.put("include_all", true);
map.put({"17082","17085"});
client.sendPost("index.php?/api/v2/add_run/24", map);
return jsonobject;
}
}
The testrail documentation is here im looking for the add run http://docs.gurock.com/testrail-api2/reference-runs
Any help here how to make this actually work , is completing but nothing is happening. I'm a tester but struggling with this pat of the Java
Have sorted this problem by :
public static String TEST_RUN_ID = "27";
public static String TESTRAIL_USERNAME = "xxx#yahoo.com";
public static String TESTRAIL_PASSWORD = "jdNnNt0OKyNnVA0BW";
public static String RAILS_ENGINE_URL = "https://axulxharmx.testrail.io/";
public static final int TEST_CASE_PASSED_STATUS = 1;
public static final int TEST_CASE_FAILED_STATUS = 5;
public static void addResultForTestCase(String testCaseId, int status,
String error) throws IOException, APIException {
String testRunId = TEST_RUN_ID;
APIClient client = new APIClient(RAILS_ENGINE_URL);
client.setUser(TESTRAIL_USERNAME);
client.setPassword(TESTRAIL_PASSWORD);
HashMap data = new HashMaps();
data.put("status_id", status);
data.put("comment", "Test Executed- Status updated test automation framework.");
client.sendPost("add_result_for_case/"+testRunId+"/"+testCaseId+"",data );
}
**It's working fine in java code as well as POSTMAN and pushing the results to my TestRail instance.