Spring junit單元測試事務的使用


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class MyTransactualControllerTest extends AbstractTransactionalJUnit4SpringContextTests   {
	
	@Autowired
	IUnifiedResource unifiedResource;
	
	@Test
	public void addDeviceSucc() throws Exception{
		Map<String, Object> map=new HashMap<String, Object>();
		
		map.put("c_name", "testtttt");
		map.put("c_classname", "DeviceInfo");
		map.put("c_ip", "1.1.1.2");
		map.put("i_port", 2356);
		map.put("c_vag_index_code", "123456");
		map.put("i_org_id", "1");
		map.put("i_camera_count", "4");
		map.put("i_ip_chan_count", "4");
		map.put("i_alarm_in_count", "4");
		map.put("i_alarm_out_count", "4");
		map.put("i_talk_count", "4");
		map.put("i_net_domain_id", "1");
		map.put("c_user_name", "admin");
		map.put("c_user_pwd", "12345");
		
		map.put("i_register_type", 0);
		map.put("i_device_type", 1);
		map.put("i_manufacturer", 0);
		map.put("i_is_online", "4");
		map.put("c_treaty_type", "SDK");
		map.put("i_bandwidth", 0);
		map.put("i_res_type", 30000);
		
		map.put("i_enable", 1);
		
	
		ResourceContext context=unifiedResource.addResource("admin",30000, map, true);
		context.getTableLable();
		context.getTableName();
		context.setMessage("");
		context.getMessage();
		context.getResultCode();
		context.setResultCode(1);
		context.getAttributes();
		ResourceContext.getEmptyContext();
	}
	

}

  如上代碼所示,主要單元測試類需要繼承 AbstractTransactionalJUnit4SpringContextTests 類,在spring配置文件applicationContext.xml文件中加入transactionManager bean:

<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM