mercredi 7 septembre 2016

Need suggestions for writing unit test cases using Mockito

I am newbie in writing unit test cases.

Things that I have to follow:

  1. I have to work using Mockito tool. And I read it is very useful tool.
  2. I have written services in spring mvc. It is having controller, business layer, pojos, dao's.
  3. I have both GET and POST services.
  4. For beginning I want to start with POST service.
  5. Response will be always in JSON and for POST Service request will be in JSON.
  6. Which steps I should follow to write good unit test cases.
  7. Please suggest some good tutorials.

Snippet of controller class:

@Controller
public class Controller
{
    @Autowired ManagerClass manager;

    @RequestMapping(value = "/app/data/send", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)

    public ResponseEntity<ResponseData> DataInquiry(@RequestBody RequestData requestData,
            @RequestHeader(value = Constants.HEADER_TRANS_ID, required = false) String transactionId) {

    ResponseData responseData = new ResponseData();
        try {
            responseData  = manager.retrieveData(requestData);
        } catch (Exception e) {
            logger.error("Exception " + e);
        }
        HttpHeaders headers = new HttpHeaders();
        headers.add(Constants.HEADER_TRANS_ID, transactionId);
        return new ResponseEntity<ResponseData>(responseData, headers, HttpStatus.OK);  

    }
}

public class ManagerClass
{

    @Override
    public ResponseData retrieveData(RequestData requestData) {
        Here Call will be made to dao layer by passing some parameters from requestdata in the method and in return response will be sent back to controller

    }
}

Aucun commentaire:

Enregistrer un commentaire