@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>