Spring接口测试

本文介绍四种不同的Spring集成测试方法:使用SpringJUnit4ClassRunner进行单元测试、通过main方法加载ApplicationContext、静态初始化ApplicationContext以及结合Hibernate和Junit4进行测试。每种方法都提供了具体的代码实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

// 方法一:SpringJUnit4测试

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations =
{ "../spring/spring_core.xml", "../spring/spring_service.xml", "../spring/spring_qsecurity.xml", "../spring/spring_dao.xml" })
public class TestO
{
 @Test
 public void test()
 {
  System.out.println("---->"+OrganizationManagementService.findOrganizationalUnitsByOid("de03e075-cf87-4029-b5e5-82025ec10415").size());
 }
}

// 方法二:main测试

public class mytest
{
 public static void main(String[] args) throws IOException
 {
  ApplicationContext ac = new ClassPathXmlApplicationContext("../spring/*");
  @SuppressWarnings("unused")
  IDeviceManagerService vs = (IDeviceManagerService) ac.getBean("deviceManagerService");
 }
}


// 方法三:(ApplicationContext)main测试

public class TestClass
{
 private static IUserInfoService userInfoService;
 private static OrganizationService organizationService;
 
 static
 {
  ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:spring/*");
  userInfoService = (IUserInfoService) ac.getBean("userInfoService");
  organizationService = (OrganizationService) ac.getBean("organizationService");
 }

 public static void main(String[] args)
 {
   List<PersonInfo> list=
   ServiceUtil.getOrganizationService().queryPersonInfoByCondition("qs00001",
   "702c2893-ac39-4dff-8c77-33160f485ff1", "", "孙", "", "1", null);
   System.out.println(list.size()+"@@@@@@@@@@@@@@@@@");
 }

}

// 方法四:spring+hibernate+junit4 测试

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:/spring-hibernate.xml","classpath:/spring-mvc.xml","classpath:/spring.xml"})
@TransactionConfiguration(transactionManager="transactionManager",defaultRollback=false)
@Transactional
public class Test
{
 @Autowired
 private PersonServiceI personServiceDao;

 @org.junit.Test
 public void testSave()
 {
  try
  {
   System.out.println("----------"+personServiceDao);
   Person person = new Person();
   person.setName("王五");
   person.setAge(23);
   person.setUpdateTime(new SimpleDateFormat("YYYY-MM-dd HH:MM:ss").parse(new SimpleDateFormat("YYYY-MM-dd HH:MM:ss").format(new Date())));
   personServiceDao.save(person);
   List<Object[]> objList = personServiceDao.selectAll();
   for (int i = 0; i < objList.size(); i++)
   {
    System.out.println("--成功--"+objList.get(i)[2].toString());
   }
  } catch (ParseException e)
  {
   e.printStackTrace();
  }
 }

}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值