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

Spring boot 使用Mockito在Spring boot中进行单元测试,spring-boot,mockito,junit4,Spring Boot,Mockito,Junit4,在执行Juint4测试时。它显示空指针异常。在单元测试中使用save方法时,它返回null。这里我使用Mockito Juint4测试来模拟该方法。有人帮我解决这个问题 **Service Method.** public Result save(Map inputParams){ Result result = new Result(); logger.info("::::::::::::::: save ::::::::::::::::"+in

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

**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);



    }
**服务方法**
公共结果保存(映射输入参数){
结果=新结果();
logger.info(“::::保存:::::::”+inputParams);
试一试{
String name=inputParams.get(“name”).toString();
字符串类型=inputParams.get(“类型”).toString();
CoreIndustry CoreIndustry=coreIndustryDao.findByName(名称);
如果(coreIndustry!=null){
result.setStatusCode(HttpStatus.FOUND.value());
result.setMessage(Messages.NAME\u EXIST\u MESSAGE);
结果.设置成功(假);
}否则{
CoreIndustry coreIndustryNew=新的CoreIndustry();
coreIndustryNew.setName(名称);
coreIndustryNew.setType(类型);
coreIndustryNew.setInfo(新的Gson().toJson(inputParams.get(“info”));
System.out.println(“CoreIndustry信息为:…………:………:”+coreIndustryNew.getInfo());
coreIndustryData=coreIndustryDao.save(coreIndustryNew);
System.out.println(“保存的数据为…………::::::::”+coreIndustryData.getName()+“”+coreIndustryData.getType()+“”+coreIndustryData.getType());
结果:设置数据(coreIndustryData);
result.setStatusCode(HttpStatus.OK.value());
result.setMessage(Messages.CREATE_MESSAGE);
result.setSuccess(true);
}
}捕获(例外e){
logger.error(“::::异常:::::”+e.getMessage());
result.setStatusCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
结果.设置成功(假);
result.setMessage(例如getMessage());
}
返回结果;
}
**控制器**
@后期映射(路径=“/industry/save”)
公共结果保存(@RequestBody-Map-stringToParse){
logger.debug(“保存-----------------”+stringToParse);
结果=industryService.save(stringToParse);
返回结果;
}
**单元测试**
@RunWith(SpringRunner.class)
@春靴测试
公共类行业服务测试{
@蚕豆
私营核心产业道核心产业道;
私营工业服务业;
@试验
public void getAll(){
System.out.println(“::控制器的GetAll方法内部”);
//当(coreIndustryDao.findAll())。然后返回(Stream.of(
//新核心产业(“Dilip”、“Brik”、“Brik产业”))
//.collect(Collectors.toList());
//assertEquals(1,industryService.getAll().setData());
}
@试验
公共作废保存(){
ObjectMapper oMapper=新的ObjectMapper();
CoreIndustry CoreIndustry=新CoreIndustry();
coreIndustry.setId(2L);
coreIndustry.setName(“Dilip”);
coreIndustry.setType(“业务”);
HashMap=newHashMap();
地图放置(“名称”、“零售”);
地图。放置(“类型”、“开发”);
setInfo(新的Gson().toJson(map));
当(coreIndustryDao.save(any(CoreIndustry.class)),然后返回(new CoreIndustry());
映射实际值=oMapper.convertValue(coreIndustry,Map.class);
System.out.println(“CoreIndustry字段值为:“+CoreIndustry.getName()+”+CoreIndustry.getInfo());
创建的结果=industryService.save(实际值);
CoreIndustry coreIndustryValue=(CoreIndustry)created.getData();
Map expectedValues=oMapper.convertValue(coreIndustryValue,Map.class);
System.out.println(“从CoreIndustry获取保存的数据……”+预期值);
System.out.println(“从CoreIndustry获取保存的数据…”+coreIndustryValue.getName());
资产(实际值)。isSameAs(预期值);
}
我是这个春季开机技术的新手

请帮我一把。谢谢


您是否模拟了您的coreIndustryDao,例如@Mock coreIndustryDao coreIndustryDao;?请提供堆栈跟踪..哪一行正在抛出npe…很可能模拟没有像coreIndustryDao那样发生,请将包含堆栈跟踪的图像添加为文本。同时指出异常来自哪一行,以及类
IndustryService
CoreIndustryDao
之间的关系。