使用Mockito在Spring Boot中进行单元测试

时间:2019-11-19 05:40:00

标签: spring-boot mockito junit4

执行Juint4测试时。它显示空指针异常。在单元测试中使用save方法时,它返回null。在这里,我正在使用Mockito Juint4 Testing模拟该方法。有人帮我解决这个问题。

**Service Method.**


    public Result save(Map inputParams){

        Result result = new Result();

        logger.info("::::::::::::::: save ::::::::::::::::"+inputParams);
        try{
            String name = inputParams.get("name").toString();
            String type = inputParams.get("type").toString();

            CoreIndustry coreIndustry = coreIndustryDao.findByName(name);
            if(coreIndustry != null){
                result.setStatusCode(HttpStatus.FOUND.value());
                result.setMessage(Messages.NAME_EXIST_MESSAGE);
                result.setSuccess(false);
            }else{
                CoreIndustry coreIndustryNew = new CoreIndustry();
                coreIndustryNew.setName(name);
                coreIndustryNew.setType(type);
                coreIndustryNew.setInfo(new Gson().toJson(inputParams.get("info")));
                System.out.println("CoreIndustry Info is :............:.............:..............:"+coreIndustryNew.getInfo());
                CoreIndustry coreIndustryData = coreIndustryDao.save(coreIndustryNew);
                System.out.println("Saved Data Is.............::::::::::::::::::::................ "+coreIndustryData.getName()+"  "+coreIndustryData.getType()+"    "+coreIndustryData.getType());
                result.setData(coreIndustryData);
                result.setStatusCode(HttpStatus.OK.value());
                result.setMessage(Messages.CREATE_MESSAGE);
                result.setSuccess(true);
            }

        }catch (Exception e){
            logger.error("::::::::::::::: Exception ::::::::::::::::"+e.getMessage());

            result.setStatusCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
            result.setSuccess(false);
            result.setMessage(e.getMessage());
        }

        return result;

    }

    **Controller**   


    @PostMapping(path = "/industry/save")
            public Result save(@RequestBody  Map<String, Object> stringToParse)  {
                logger.debug("save---------------"+stringToParse);
                Result result = industryService.save(stringToParse);
                return result;
            }


**Unit Test**


@RunWith(SpringRunner.class)
@SpringBootTest
public class IndustryServiceTest {

    @MockBean
    private CoreIndustryDao coreIndustryDao;


    private IndustryService industryService;


    @Test
    public void getAll() {

        System.out.println(":::::::  Inside of GetAll Method of Controller.");
       // when(coreIndustryDao.findAll()).thenReturn(Stream.of(
         //       new CoreIndustry("Dilip","Brik","Brik Industry"))
           //     .collect(Collectors.toList()));
        //assertEquals(1,industryService.getAll().setData());
    }

    @Test
    public void save() {

        ObjectMapper oMapper = new ObjectMapper();

        CoreIndustry coreIndustry = new CoreIndustry();
        coreIndustry.setId(2L);
        coreIndustry.setName("Dilip");
        coreIndustry.setType("Business");
        HashMap<String,Object> map = new HashMap();
        map.put("name","Retail");
        map.put("type","Development");
        coreIndustry.setInfo(new Gson().toJson(map));

        when(coreIndustryDao.save(any(CoreIndustry.class))).thenReturn(new CoreIndustry());

        Map<String, Object> actualValues = oMapper.convertValue(coreIndustry,Map.class);


        System.out.println("CoreIndustry Filed values are........ : "+coreIndustry.getName()+"    "+coreIndustry.getInfo());
        Result created = industryService.save(actualValues);
        CoreIndustry coreIndustryValue = (CoreIndustry) created.getData();

        Map<String, Object> expectedValues = oMapper.convertValue(coreIndustryValue, Map.class);

        System.out.println(" Getting Saved data from CoreIndustry........"+expectedValues);

        System.out.println(" Getting Saved data from CoreIndustry........"+coreIndustryValue.getName());
        assertThat(actualValues).isSameAs(expectedValues);



    }

我是这个Spring Boot Technology的新手。

After Running the source code for save method. After Debugging my source code.

  

请帮帮我,太好了。谢谢。

0 个答案:

没有答案