用到的方法是junit。
在pom.xml中引入依赖:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
实现类中,右键可以快速添加测试类,如下:
===================================================================
如果要在测试类中,要注入依赖,需要添加几个注解,如下:
关于@WebAppConfiguration注解的使用可以看一下这个连接。
spring集成测试@WebAppConfiguration注解的使用
====================================================================
以下是关于JSON的转换:
@WebAppConfiguration
@SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class)
public class RmsPreemptFlowServiceImplTest extends TestCase {
@Autowired
RmsPreemptFlowService rmsPreemptFlowService;
@Test
public void testSelectByDelegationId() {
List<RmsPreemptFlow> list = rmsPreemptFlowService.selectList(new RmsPreemptFlow());
System.out.println("数组"+list);
//[com.erayt.rms.domain.RmsPreemptFlow@79de9f90, com.erayt.rms.domain.RmsPreemptFlow@15db4dc, com.erayt.rms.domain.RmsPreemptFlow@2e7a6dcd, com.erayt.rms.domain.RmsPreemptFlow@762261fd, com.erayt.rms.domain.RmsPreemptFlow@7b0d42b6, com.erayt.rms.domain.RmsPreemptFlow@179af25f, com.erayt.rms.domain.RmsPreemptFlow@6a73f164, com.erayt.rms.domain.RmsPreemptFlow@fbb585e, com.erayt.rms.domain.RmsPreemptFlow@1e32a3ed, com.erayt.rms.domain.RmsPreemptFlow@5e7aa27e, com.erayt.rms.domain.RmsPreemptFlow@2b778004, com.erayt.rms.domain.RmsPreemptFlow@7e5b449c, com.erayt.rms.domain.RmsPreemptFlow@75fdbd71, com.erayt.rms.domain.RmsPreemptFlow@7e1f88e2, com.erayt.rms.domain.RmsPreemptFlow@697f4085, com.erayt.rms.domain.RmsPreemptFlow@4d248692, com.erayt.rms.domain.RmsPreemptFlow@55aabc72, com.erayt.rms.domain.RmsPreemptFlow@57f076bd, com.erayt.rms.domain.RmsPreemptFlow@44986fae, com.erayt.rms.domain.RmsPreemptFlow@2d6161b9, com.erayt.rms.domain.RmsPreemptFlow@2d28114b, com.erayt.rms.domain.RmsPreemptFlow@79b9dfcd, com.erayt.rms.domain.RmsPreemptFlow@7edb2a4b, com.erayt.rms.domain.RmsPreemptFlow@5811f080, com.erayt.rms.domain.RmsPreemptFlow@355e1080, com.erayt.rms.domain.RmsPreemptFlow@2aef651]
RmsPreemptFlow rmsPreemptFlow =new RmsPreemptFlow();
rmsPreemptFlow.setDelegationId("DLG20231226002429");
List<RmsPreemptFlow> listOne = rmsPreemptFlowService.selectList(rmsPreemptFlow);
//[com.erayt.rms.domain.RmsPreemptFlow@72426ced]
System.out.println("数组one"+listOne);
//com.erayt.rms.domain.RmsPreemptFlow@72426ced
System.out.println("对象"+listOne.get(0));
RmsPreemptFlow record = list.get(0);
String recordStr = JSON.toJSONString(record);
//对象json:{"auditTimestamp":1702611592448,"beginDate":20231226,"createTime":1702611592448,"createUser":"admin","currency":"CNY","delegationId":"DLG20231226002447","endDate":20231226,"flowId":"03734EE66A3902A8B5B7AB96","id":0,"investType":"BondsDeals","issueCode":"MONETARY_ISSUE","operateType":"RELEASE","portfolioId":"P20231204000124","preemptDate":20231226,"preemptQuota":-2000.84249700,"preemptStatus":"invalid","preemptType":"FundAmount","remark":"委托审批不通过","secuCode":"019626","side":"BUY","stageType":"end","updateTime":1702611592448,"updateUser":"admin"}
System.out.println("对象json:"+recordStr);
RmsPreemptFlow o = JSON.parseObject(recordStr,RmsPreemptFlow.class);
//json转回对象:com.erayt.rms.domain.RmsPreemptFlow@412bb41f
System.out.println("json转回对象:"+o);
//通过javabean转成JSON格式
//{"endDate":20231226,"remark":"委托审批不通过","currency":"CNY","id":0,"flowId":"03734EE66A3902A8B5B7AB96","issueCode":"MONETARY_ISSUE","preemptDate":20231226,"investType":"BondsDeals","preemptType":"FundAmount","side":"BUY","auditTimestamp":1702611592448,"stageType":"end","operateType":"RELEASE","preemptQuota":-2000.84249700,"updateUser":"admin","updateTime":1702611592448,"secuCode":"019626","preemptStatus":"invalid","beginDate":20231226,"portfolioId":"P20231204000124","createTime":1702611592448,"createUser":"admin","delegationId":"DLG20231226002447"}
System.out.println("通过javabean转成JSON格式"+JSON.toJSON(o));
//** tip:可以把json存入到数据库字段中,使用的时候,再讲json转成对象,从对象中取出相应的字段。
//** tip:Java后端还可以使用JSON格式来接收前端或其他系统发送的数据,以便更好地处理和解析数据。
/**
前端将对象转成JSON格式。
将对象转为 json ,再进行传递(HTML不能直接传递对象);
js 对象转换成json对象
var json = JSON.stringify(jsObject)
console.log(json);
json对象转成js对象
var js = JSON.parse(jsonObject);
console.log(js);
*/
}
}