1.在pom文件中添加依赖
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>compile</scope> </dependency> </dependencies>
2.在src下的main的同级目录下创建test目录,test里面的目录最好同main下的目录,以便于识别
3.在测试类下加入注解
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"classpath:spring.xml", "classpath:spring-mvc.xml", "classpath:spring-hibernate.xml", "classpath:spring-redis.xml", "classpath:spring-schedule.xml", "classpath:spring-hibernate-dynamic.xml"}) @Transactional
4.主动注入
@Resource(name = "baseDao") private BaseDao baseDao;
5.在要测试的方法上标注
@Test
方法的返回值要为void
注:方法逻辑需要的参数自己给出对应的值
@Test public void getvoucherList() { List InvoiceDetail = new ArrayList(); String startDate = "2023-05-06"; String endDate = "2023-05-10"; List<Map> maps = baseDao.selectMapsBySQL("select id from cs_dzd_info where tax_num = ? and create_time>= ? and create_time <= ?", Arrays.asList("测试数据", startDate, endDate)); for (int i = 0; i < maps.size(); i++) { Integer id = (Integer) maps.get(i).get("id"); Map eInvoiceId = new HashMap(); eInvoiceId.put("eInvoiceId", "electronic_dzdz_" + String.valueOf(id)); InvoiceDetail.add(eInvoiceId); } System.out.println(InvoiceDetail); }