SpringBoot单元测试使用示例

    技术2022-07-11  106

    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进行模拟数据调用,可参考

     

    Processed: 0.011, SQL: 9