如何在Spring MVC工程中进行单元测试
1个回答
2016-11-18 · 知道合伙人软件行家
关注
展开全部
直接以代码方式演示如何在spring MVC工程中进行单元测试:
package net.chinaedu.projects.dubhe;
import java.util.List;
import net.chinaedu.projects.dubhe.publisher.IPublisherService;
import net.chinaedu.projects.venus.domain.Publisher;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@ActiveProfiles({"test"})
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:conf/spring/app-context.xml", "classpath:conf/spring/servlet-context.xml"})//指定工程中Spring配置xml文件
public class JUnit4Test extends AbstractJUnit4SpringContextTests {//AbstractTransactionalJUnit4SpringContextTests
@Autowired
private IPublisherService publisherService;
@Test
public void testFindAllPublisher(){
List<Publisher> publisherList = publisherService.findAll();
for (Publisher publisher:publisherList) {
System.out.println(publisher.getId() + " , " + publisher.getName());
}
}
}
package net.chinaedu.projects.dubhe;
import java.util.List;
import net.chinaedu.projects.dubhe.publisher.IPublisherService;
import net.chinaedu.projects.venus.domain.Publisher;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@ActiveProfiles({"test"})
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:conf/spring/app-context.xml", "classpath:conf/spring/servlet-context.xml"})//指定工程中Spring配置xml文件
public class JUnit4Test extends AbstractJUnit4SpringContextTests {//AbstractTransactionalJUnit4SpringContextTests
@Autowired
private IPublisherService publisherService;
@Test
public void testFindAllPublisher(){
List<Publisher> publisherList = publisherService.findAll();
for (Publisher publisher:publisherList) {
System.out.println(publisher.getId() + " , " + publisher.getName());
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询