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