如何在Spring MVC工程中进行单元测试
发布网友
发布时间:2022-04-24 07:46
我来回答
共1个回答
热心网友
时间:2022-04-24 09:16
直接以代码方式演示如何在spring MVC工程中进行单元测试:
package net.chinae.projects.bhe;
import java.util.List;
import net.chinae.projects.bhe.publisher.IPublisherService;
import net.chinae.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());
}
}
}