jeudi 23 juillet 2015

mockito test spring controller with return type JsonObject

I am New to Mockito and I am facing problem in writing test case for my controller. I have given detail source code of how my controller handler and service looks like and, I have also given the test case I have written which is failing

Can anyone please help me writing the correct test case for my controller.

Thank you

Following is my Test case

public class ControllerTest {

    Controller controller;


    @Before
    public void setUp() throws Exception {
        controller = new Controller();
    }


    @Test
    public void testControllerMethod() throws IOException {

        String x = "123456";
        String param1 = "328648";
        String param2 = "493734";


        ServiceClass mock = Mockito.mock(ServiceClass.class);


        Mockito.when(mock.ServiceClassMethod(x, param1, param2)).thenReturn("ok");

        controller.setServiceClass(mock);


        ReturnSomething actualResponse = controller.methodName(x, param1, param2);

        Assert.assertNotNull(actualResponse);
        Assert.assertEquals("ok",actualResponse);


     }
}

Following is my Controller Class:

@Controller

@RequestMapping("/a")

public class Controller {

    @Autowired
    private ServiceClass serviceClass;

    @RequestMapping(value = "/{x}/y", method = RequestMethod.GET)
    public @ResponseBody ReturnSomething methodName(
            @PathVariable String x,
            @RequestParam(required = false, value = "param1") String param1,
            @RequestParam(required = false, value = "param2") String param2) throws IOException{

            String result = serviceClass.serviceMethod(x, param1, param2);
            if(StringUtils.isNotEmpty(result)){
            String result1 = StringUtils.replace(result,  "/something/something/something", "/something/something/something");

            JsonObject result2 = (new JsonParser()).parse(result1).getAsJsonObject();
            return new ReturnSomething(status, message, result2);
            }else{
                return new ReturnSomething(status, message, null);
            }
    }

Following is My Service class:

@Service

public class ServiceClassImpl implements ServiceClass {

    @Autowired
    private Handler handler;


    @Override
    public String serviceMethod(String x, String param1, String param2) {
        String result = handler.handlerMethod(x, param1, param2);
        return result;




**Following is My Handler Class**


@Component

public class HandlerImpl implements Handler {


    private final static Logger log = LoggerFactory
            .getLogger(HandlerImpl.class.getName());


    Handler handler;
    private RestTemplate restTemplate = new RestTemplate();

    @Override
    public String handlerMethod(String x,String param1, String param2) {

        Map<String, String> requestParams = new HashMap<String, String>();
        requestParams.put("x", x);
        requestParams.put("param1", param1);
        requestParams.put("param2", param2);

        String result = restTemplate.getForObject(url, String.class, requestParams);
        log.trace("Json Recieved :" + result);
        return result;
    }

Aucun commentaire:

Enregistrer un commentaire