0

I am writing negative test case in which I am expecting internal server error but somehow I am not able to get it. The code i have tried and the output is posted below:

delete method:

/**
 * delete pricing rule by id
 * @param ruleId
 * @return
 */
@DeleteMapping(path = "/deleteById/{ruleId}")
public ResponseEntity<String> deletePricingRule(@PathVariable String ruleId) {
    logMethodStart(RulesCrudController.class, "deletePricingRule");
    try {
        log.debug( "deleting pricing rule with id: " + ruleId );
        String deletedId = pricingRuleServiceImpl.deletePricingRule(ruleId);
        if( deletedId != null ) {
            log.debug("deleted pricing rule with id: " + ruleId);
            return new ResponseEntity<>("The rule id : " + deletedId + " is deleted successfully", HttpStatus.OK);
        } else {
            return new ResponseEntity<>("No pricing rule with rule id: "+ ruleId +" is Found",HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
    catch(Exception e){
        log.error("deletePricingRule() got an exception: " + e.toString());
        return new ResponseEntity<>("No pricing rule with rule id: "+ ruleId +" is Found",HttpStatus.INTERNAL_SERVER_ERROR);
    }
}

delete test case

@Test
public void DeletePricingRule_withDeleteMethod_InternalServerError() throws Exception {

    when(mockService.deletePricingRule(dynamo.getId())).thenReturn( null );

    mockMVC.perform(MockMvcRequestBuilders.delete("/pricingRule/deleteById/{ruleId}", dynamo.getId()))
            .andExpect(status().isInternalServerError());


}

Object that I am using:

@Mock
private PricingRule dynamo, dynamo1;

@Before
public void setUp() {

    dynamo = new PricingRule();
    dynamo.setId("0653cf02-6b39-40ab-a008-6efea487eb8f");
    dynamo.setRuleDesc("dynamo test");

    dynamo1 = new PricingRule();
    dynamo1.setId("0653cf02-6b39-40ab-a008-6efea487eb8f");
    dynamo1.setRuleDesc("dynamo test");

}

Output:

java.lang.AssertionError: Status 
Expected :500
Actual   :200
Sikandar Sahab
  • 638
  • 3
  • 10
  • 27
  • [Why is my class not calling my mocked methods in unit test?](https://stackoverflow.com/q/74027324/112968) – you are using `@Mock` to have mock objects created, but then you overwrite those with real instances in the @Before method. If you use a debugger, what's the value of `ruleId` when inside your controller method? – knittl Dec 26 '22 at 18:21

0 Answers0