SpringBoot单元测试使用示例

IDEA开发工具,在待测试类,alt+enter,选择Create Test

 

生成 public class TrajectoryServiceImplTest extends TestCase

然后添加注解

@SpringBootTest(classes = StApplication.class)

@RunWith(SpringRunner.class)

@SpringBootTest(classes = StApplication.class)
@RunWith(SpringRunner.class)
@Slf4j
public class TrajectoryServiceImplTest extends TestCase

在待测试方法上添加@Test注解即可使用

TestCase提供了assertTrue();方法进行各种断言

Demo如下:

@Test
public void testTop10IntersectionWithCoordinate() {
    Integer  r=5; //距离容差
    Integer  t=3;//时间容差
    Integer  n=2;//N组轨迹点 (经纬度,时间)
    //轨迹点数组['x1', 'y1', 't1', 'x2', 'y2', 't2']
    String[] text={"106.22796", "35.146009999999997", "1577167735", "106.22796", "35.146009999999997", "1577167740"};
    Top10IntersectionWithCoordinateCondition testDomain=new Top10IntersectionWithCoordinateCondition();
    testDomain.setDeltaDistance(r);
    testDomain.setPointNum(n);
    testDomain.setDeltaTime(t);
    testDomain.setPointStr(text);
    List<IntersectionTraWithCoordinateVO> expectResult=new ArrayList<>();
    IntersectionTraWithCoordinateVO vo=new IntersectionTraWithCoordinateVO();
    vo.setTargetId("B8-37-65-9F-01-1F");
    vo.setPointCount(1);
    expectResult.add(vo);
    List<IntersectionTraWithCoordinateVO> result= trajectoryMapper.top10IntersectionWithCoordinate(testDomain);
    assertEquals("测试结果同预期结果不一致",expectResult,result);
    log.info(JSONObject.toJSONString(result));
}

除此之外,还有使用Mockito/EasyMock进行模拟数据调用,可参考

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

努力终会有回报

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值