思路:用test类加载spring配置文件来实现
1,jar包引入
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<properties>
<spring.version>4.2.2.RELEASE</spring.version>
</properties>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
2,创建测试父类
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath*:application-config.xml","classpath*:servlet-config.xml"})
public class ObjectTest {
protected final static Logger log = LoggerFactory.getLogger(ObjectTest.class);
}
@ContextConfiguration后面是spring配置文件位置
3,写具体测试子类
import com.uc.statge.mapper.CloudplatformDaoMapper;
import com.uc.statge.object.ObjectTest;
import com.uc.statge.pojo.Cloudplatform;
import com.uc.statge.pojo.User;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
import java.util.Map;
public class CloudplatformDaoMapperTest extends ObjectTest {
@Autowired
CloudplatformService cfs;
@Autowired
CloudplatformDaoMapper cdfMapper;
@Test
public void findById() {
User user =new User(3, "c@123.com" , "123123123" , " 2 5 " , 220000, 220100, 220105,
"12323232333", 0 , 0, 0);
try {
Map<String,Object> map = cfs.findById(user,1);
for(Map.Entry<String,Object> entry:map.entrySet()){
System.out.println(entry.getKey()+":"+entry.getValue());
}
} catch (Exception e) {
e.printStackTrace();
}
}
成功