I have a method in service file –>
<code>public FeedbackResponseDTO createFeedback(@Valid @NonNull FeedbackRequestDTO feedbackRequestDTO, @Valid @NonNull String channelName) {
// in v2: channelName is already set in feedbackRequestDTO
log.info("Class: {}, Method: {}, Object: {}", this.getClass().getName(), "createFeedback", "checking ssfqId");
ServicesStageFeedbackQuestionMappings servicesStageMappingsEntity = servicesStageQuestionMappingsService
.getMappingsById(feedbackRequestDTO.getServicesStageFeedbackQuestionMappingsId());
Feedback feedbackInput = convertFKeysToEntity (feedbackRequestDTO, servicesStageMappingsEntity);
log.info("Class: {}, Method: {}, Object: {}", this.getClass().getName(), "createFeedback", "saving feedback");
Feedback feedbackSaved = feedbackRepo.save(feedbackInput);
log.info("Class: {}, Method: {}, Object: {}", this.getClass().getName(), "createFeedback", "saved feedback with id "+ feedbackSaved.getId());
return FeedbackResponseDTO.builder().id(feedbackSaved.getId()).build();
}
</code>
<code>public FeedbackResponseDTO createFeedback(@Valid @NonNull FeedbackRequestDTO feedbackRequestDTO, @Valid @NonNull String channelName) {
// in v2: channelName is already set in feedbackRequestDTO
log.info("Class: {}, Method: {}, Object: {}", this.getClass().getName(), "createFeedback", "checking ssfqId");
ServicesStageFeedbackQuestionMappings servicesStageMappingsEntity = servicesStageQuestionMappingsService
.getMappingsById(feedbackRequestDTO.getServicesStageFeedbackQuestionMappingsId());
Feedback feedbackInput = convertFKeysToEntity (feedbackRequestDTO, servicesStageMappingsEntity);
log.info("Class: {}, Method: {}, Object: {}", this.getClass().getName(), "createFeedback", "saving feedback");
Feedback feedbackSaved = feedbackRepo.save(feedbackInput);
log.info("Class: {}, Method: {}, Object: {}", this.getClass().getName(), "createFeedback", "saved feedback with id "+ feedbackSaved.getId());
return FeedbackResponseDTO.builder().id(feedbackSaved.getId()).build();
}
</code>
public FeedbackResponseDTO createFeedback(@Valid @NonNull FeedbackRequestDTO feedbackRequestDTO, @Valid @NonNull String channelName) {
// in v2: channelName is already set in feedbackRequestDTO
log.info("Class: {}, Method: {}, Object: {}", this.getClass().getName(), "createFeedback", "checking ssfqId");
ServicesStageFeedbackQuestionMappings servicesStageMappingsEntity = servicesStageQuestionMappingsService
.getMappingsById(feedbackRequestDTO.getServicesStageFeedbackQuestionMappingsId());
Feedback feedbackInput = convertFKeysToEntity (feedbackRequestDTO, servicesStageMappingsEntity);
log.info("Class: {}, Method: {}, Object: {}", this.getClass().getName(), "createFeedback", "saving feedback");
Feedback feedbackSaved = feedbackRepo.save(feedbackInput);
log.info("Class: {}, Method: {}, Object: {}", this.getClass().getName(), "createFeedback", "saved feedback with id "+ feedbackSaved.getId());
return FeedbackResponseDTO.builder().id(feedbackSaved.getId()).build();
}
The coverage in intellij is 100% but mutation is failing with line
replaced return value with null for service/impl/FeedbackServiceV2::createFeedback → SURVIVED
I wrote a test case but didnt worked –>
<code>@Test
void testCreateFeedback_WhenFeedbackInputIsNull_ThrowsRestServiceException() {
FeedbackRequestDTOV2 feedbackRequestDTO = new FeedbackRequestDTOV2("Class: {}, Method: {}, Object: {}",
"Class: {}, Method: {}, Object: {}", "Class: {}, Method: {}, Object: {}", "Class: {}, Method: {}, Object: {}");
ServicesStageFeedbackQuestionMappings servicesStageMappingsMock = Mockito.mock(ServicesStageFeedbackQuestionMappings.class);
when(feedbackMapper.dtoToEntity(Mockito.any(FeedbackRequestDTOV2.class))).thenThrow(RestServiceException.class);
assertThrows(RestServiceException.class,
() -> feedbackServiceV2.createFeedback(feedbackRequestDTO, "channelName"),
"Expected createFeedback to throw RestServiceException when feedbackMapper returns null");
}
</code>
<code>@Test
void testCreateFeedback_WhenFeedbackInputIsNull_ThrowsRestServiceException() {
FeedbackRequestDTOV2 feedbackRequestDTO = new FeedbackRequestDTOV2("Class: {}, Method: {}, Object: {}",
"Class: {}, Method: {}, Object: {}", "Class: {}, Method: {}, Object: {}", "Class: {}, Method: {}, Object: {}");
ServicesStageFeedbackQuestionMappings servicesStageMappingsMock = Mockito.mock(ServicesStageFeedbackQuestionMappings.class);
when(feedbackMapper.dtoToEntity(Mockito.any(FeedbackRequestDTOV2.class))).thenThrow(RestServiceException.class);
assertThrows(RestServiceException.class,
() -> feedbackServiceV2.createFeedback(feedbackRequestDTO, "channelName"),
"Expected createFeedback to throw RestServiceException when feedbackMapper returns null");
}
</code>
@Test
void testCreateFeedback_WhenFeedbackInputIsNull_ThrowsRestServiceException() {
FeedbackRequestDTOV2 feedbackRequestDTO = new FeedbackRequestDTOV2("Class: {}, Method: {}, Object: {}",
"Class: {}, Method: {}, Object: {}", "Class: {}, Method: {}, Object: {}", "Class: {}, Method: {}, Object: {}");
ServicesStageFeedbackQuestionMappings servicesStageMappingsMock = Mockito.mock(ServicesStageFeedbackQuestionMappings.class);
when(feedbackMapper.dtoToEntity(Mockito.any(FeedbackRequestDTOV2.class))).thenThrow(RestServiceException.class);
assertThrows(RestServiceException.class,
() -> feedbackServiceV2.createFeedback(feedbackRequestDTO, "channelName"),
"Expected createFeedback to throw RestServiceException when feedbackMapper returns null");
}
Can someone help me with what is wrong in the test case , why its not passing the mutation test.
New contributor
HaHa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.