博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
mockIto
阅读量:4970 次
发布时间:2019-06-12

本文共 7070 字,大约阅读时间需要 23 分钟。

package com.carbond.landmark.converter.gb2016;import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.JSONObject;import com.changan.carbond.landmark.converter.gb2016.VehicleLoginConverter;import com.changan.carbond.landmark.dto.VehicleLoginDto;import com.changan.carbond.landmark.handler.gb2016.VehicleLoginSerialNumHandler;import com.changan.carbond.landmark.model.gb2016.GBBaseModel;import com.changan.carbond.landmark.util.GBVehicleHelper;import com.changan.carbond.landmark.util.ProtocolTool;import org.junit.Before;import org.junit.Test;import org.junit.runner.RunWith;import org.mockito.InjectMocks;import org.mockito.Mock;import org.mockito.Mockito;import org.mockito.MockitoAnnotations;import org.mockito.invocation.InvocationOnMock;import org.mockito.stubbing.Answer;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import org.springframework.test.context.web.WebAppConfiguration;import static org.assertj.core.api.Assertions.*;import static org.mockito.Mockito.when;import java.util.Date;/** * Created by Bonker on 2018/6/5. */@RunWith(SpringJUnit4ClassRunner.class)@WebAppConfiguration@ContextConfiguration(locations = {"classpath:applicationContext.xml"})public class VehicleLoginConverterTest {    private Logger logger = LoggerFactory.getLogger(this.getClass());    @InjectMocks    private VehicleLoginConverter target = new VehicleLoginConverter();    @Mock    GBVehicleHelper gbVehicleHelper;    @Mock    VehicleLoginSerialNumHandler vehicleLoginSerialNumHandler;    @Before    public void steup() {        MockitoAnnotations.initMocks(this);        when(gbVehicleHelper.newSerialNum()).thenReturn(1);        //无返回值        Mockito.doAnswer(new Answer() {            @Override            public Object answer(InvocationOnMock invocationOnMock) throws Throwable {                Object[] args = invocationOnMock.getArguments();                return "called with arguments: " + args;            }        }).when(vehicleLoginSerialNumHandler).putLoginSerialNum("LFBJDBB43WJ000118", 1);    }    @Test    public void convert() {        VehicleLoginDto vehicleLoginDto = new VehicleLoginDto();        vehicleLoginDto.setBatteriesCode(new String[]{"001", "002"});        vehicleLoginDto.setIccid("1234567890123");        vehicleLoginDto.setDate(new Date());        vehicleLoginDto.setVin("LFBJDBB43WJ000118");        //转换对象        GBBaseModel gbBaseModel = target.convert(vehicleLoginDto);        logger.info(JSON.toJSONString(gbBaseModel));        try {            byte[] data = gbBaseModel.encode();            logger.info(ProtocolTool.getBytesAsHexStr(data));            assertThat(data).isNotEmpty();        }catch (Exception e){            logger.info("error",e);        }    }    @Test    public void converter(){        String jsonStr="{\"carId\":\"925184500671971328\",\"tuid\":\"50002001170911990000000007045755\",\"vin\":\"LS5A3AJCXJB008944\",\"iccid\":\"898602B32216C0476992\",\"date\":1528353792586,\"batteriesCode\":[],\"targetType\":\"VehicleLoginDto\"}";        JSONObject jsonObj = JSON.parseObject(jsonStr);         VehicleLoginDto vehicleLoginDto=jsonObj.toJavaObject(VehicleLoginDto.class);        //转换对象        GBBaseModel gbBaseModel = target.convert(vehicleLoginDto);        logger.info(JSON.toJSONString(gbBaseModel));        try {            byte[] data = gbBaseModel.encode();            logger.info(ProtocolTool.getBytesAsHexStr(data));            assertThat(data).isNotEmpty();        }catch (Exception e){            logger.info("error",e);        }    }}
package com.carbond.landmark.converter.gb2016;import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.JSONObject;import com.changan.carbond.landmark.converter.gb2016.VehicleLogoutConverter;import com.changan.carbond.landmark.dto.VehicleLoginDto;import com.changan.carbond.landmark.dto.VehicleLogoutDto;import com.changan.carbond.landmark.handler.gb2016.VehicleLoginSerialNumHandler;import com.changan.carbond.landmark.model.gb2016.GBBaseModel;import com.changan.carbond.landmark.util.ProtocolTool;import org.junit.Before;import org.junit.Test;import org.junit.runner.RunWith;import org.mockito.InjectMocks;import org.mockito.Mock;import org.mockito.MockitoAnnotations;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import org.springframework.test.context.web.WebAppConfiguration;import java.util.Date;import static org.assertj.core.api.Assertions.assertThat;import static org.mockito.Mockito.when;/** * Created by Bonker on 2018/6/5. */@RunWith(SpringJUnit4ClassRunner.class)@WebAppConfiguration@ContextConfiguration(locations = {"classpath:applicationContext.xml"})public class VehicleLogoutConverterTest {    private Logger logger = LoggerFactory.getLogger(this.getClass());    @InjectMocks    private VehicleLogoutConverter target = new VehicleLogoutConverter();    @Mock    VehicleLoginSerialNumHandler vehicleLoginSerialNumHandler;    @Before    public void steup() {        MockitoAnnotations.initMocks(this);        when(vehicleLoginSerialNumHandler.getLoginSerialNum("LFBJDBB43WJ000118")).thenReturn(1);    }    @Test    public void convert() {        VehicleLogoutDto vehicleLogoutDto=new VehicleLogoutDto();        vehicleLogoutDto.setLogoutDate(new Date());        vehicleLogoutDto.setVin("LFBJDBB43WJ000118");        //转换对象        GBBaseModel gbBaseModel = target.convert(vehicleLogoutDto);        logger.info(JSON.toJSONString(gbBaseModel));        try {            byte[] data = gbBaseModel.encode();            logger.info(ProtocolTool.getBytesAsHexStr(data));            assertThat(data).isNotEmpty();        }catch (Exception e){            logger.info("error",e);        }    }    @Test    public void converter(){        String jsonStr=" {\"vin\":\"LS5A3CJC3JF810045\",\"logoutDate\":1528363761736,\"targetType\":\"VehicleLogoutDto\"}";        JSONObject jsonObj = JSON.parseObject(jsonStr);        VehicleLogoutDto vehicleLoginDto=jsonObj.toJavaObject(VehicleLogoutDto.class);        //转换对象        GBBaseModel gbBaseModel = target.convert(vehicleLoginDto);        logger.info(JSON.toJSONString(gbBaseModel));        try {            byte[] data = gbBaseModel.encode();            logger.info(ProtocolTool.getBytesAsHexStr(data));            assertThat(data).isNotEmpty();        }catch (Exception e){            logger.info("error",e);        }    }}
org.mockito
mockito-all
1.10.19
test
org.assertj
assertj-core
2.4.1
test
io.springfox
springfox-swagger-ui
2.8.0

转载于:https://www.cnblogs.com/Bonker/p/9214949.html

你可能感兴趣的文章
[Locked] Wiggle Sort
查看>>
deque
查看>>
c#中从string数组转换到int数组
查看>>
数据模型(LP32 ILP32 LP64 LLP64 ILP64 )
查看>>
java小技巧
查看>>
POJ 3204 Ikki's Story I - Road Reconstruction
查看>>
iOS 加载图片选择imageNamed 方法还是 imageWithContentsOfFile?
查看>>
toad for oracle中文显示乱码
查看>>
SQL中Group By的使用
查看>>
错误org/aopalliance/intercept/MethodInterceptor解决方法
查看>>
两个表格中数据不用是一一对应关系--来筛选不同数据,或者相同数据
查看>>
hiho_offer收割18_题解报告_差第四题
查看>>
AngularJs表单验证
查看>>
静态方法是否属于线程安全
查看>>
02号团队-团队任务3:每日立会(2018-12-05)
查看>>
SQLite移植手记1
查看>>
js05-DOM对象二
查看>>
mariadb BINLOG_FORMAT = STATEMENT 异常
查看>>
C3P0 WARN: Establishing SSL connection without server's identity verification is not recommended
查看>>
iPhone在日本最牛,在中国输得最慘
查看>>