spring进行测试junit测试的时候,注入的bean为null,是因为bean是交给Spring来管理的,但是你只是单纯的启动了一个test方法,并没有注入bean,所以为空
解决:
需要增加注解
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:conf/spring-context.xml" })
locations :填写的路径是你的spring上下文控制的配置文件
测试代码:
import com.google.common.collect.Maps;
import com.entity.domain.ThingInfo;
import com.kpmg.te.core.domain.datasource.FormSearchCondition;
import com.kpmg.te.core.domain.datasource.Page;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.transaction.Transactional;
import java.util.Date;
import java.util.Map;
/**
* 批次管理测试类
*/
@ActiveProfiles({ "test" })
@SuppressWarnings("static-access")
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:conf/spring-context.xml" })
@Transactional
@Rollback(true)
public class BatchManageServiceTest{
@Autowired
public BatchManageService service;
/**
* 查询批次信息
*/
@Test
@Rollback(false)
public void testQueryList() {
FormSearchCondition formSearchCondition=new FormSearchCondition();
Page<Map<String,Object>> page=new Page<Map<String,Object>>();
page.setPageNo(1);
page.setPageSize(10);
Page<Map<String, Object>> list = this.service.queryList(formSearchCondition, page);
Assert.assertNotNull(list);
}
/**
* 修改物品信息
*/
@Test
@Rollback(true)
public void testUpdateBatchInfo() throws Exception {
Map<String,Object> batchInfo= Maps.newHashMap();
batchInfo.put("pkid","000318358F854158BDA3C81E0E57B310");
batchInfo.put("dispawnedReceiveDate",new Date());
batchInfo.put("sapPwnStatusCode","03");
ThingInfo thingInfo = this.service.updateBatchInfo(batchInfo);
Assert.assertNotNull(thingInfo);
}
}