您的位置:軟件測試 > 開源軟件測試 > 開源單元測試工具 > junit
Maven多模塊下使用JUnit進行單元測試
作者:siv8 發(fā)布時間:[ 2016/12/26 16:44:15 ] 推薦標簽:單元測試 Junit

  1、選中需要進行測試的service類,右鍵->new->other->JUnit Test Case,如下圖:
  2、編寫測試代碼如下:
  AppServiceTest.java
import static org.junit.Assert.assertEquals;
import java.io.IOException;
import javax.servlet.ServletException;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.context.ContextConfiguration;
import com.lenovo.moc.portal.service.AppService;
/**
* @author Administrator
*
*/
@RunWith(JUnit4ClassRunner.class)
// 由于本測試類位于src/test/java下,而app-context.xml處于src/main/java下,所以需要使用file來獲取,
// 否則使用@ContextConfiguration(locations={"classpath:WEB-INF/app-context.xml"})來獲取
@ContextConfiguration(locations={"file:src/main/webapp/WEB-INF/app-context.xml"})
public class AppServiceTest{
private MockHttpServletRequest request;
private MockHttpServletResponse response;
@Autowired
private AppService appService;
/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
request = new MockHttpServletRequest();
request.setCharacterEncoding("UTF-8");
response = new MockHttpServletResponse();
}
/**
* @throws java.lang.Exception
*/
@After
public void tearDown() throws Exception {
}
/**
* Test method for {@link AppService#login(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)}.
*/
@Test
public void testAdminAppLogin() {
try {
request.setParameter("key", "value");
appService.login(request, response);
assertEquals(response.getStatus(), 200);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ServletException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
  JUnit4ClassRunner.java
import java.io.FileNotFoundException;
import org.junit.runners.model.InitializationError;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.util.Log4jConfigurer;
public class JUnit4ClassRunner extends SpringJUnit4ClassRunner {
static {
try {
// 控制臺打印日志
Log4jConfigurer.initLogging("file:src/main/webapp/WEB-INF/log4j.properties");
} catch (FileNotFoundException ex) {
System.err.println("Cannot Initialize log4j");
}
}
public JUnit4ClassRunner(Class<?> clazz) throws InitializationError {
super(clazz);
}
}
log4j.properties
log4j.rootLogger=INFO, stdout, logfile
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
#log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
log4j.appender.stdout.layout.ConversionPattern=- %m%n
log4j.appender.logfile=org.apache.log4j.RollingFileAppender
log4j.appender.logfile.File=${catalina.home}/logs/appTest.log
log4j.appender.logfile.MaxFileSize=512MB
# Keep three backup files.
log4j.appender.logfile.MaxBackupIndex=3
# Pattern to output: date priority [category] - message
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n
log4j.logger.com.opensymphony.xwork2=ERROR
# Control logging for other open source packages
log4j.logger.org.springframework=ERROR
log4j.logger.org.quartz=ERROR
log4j.logger.net.sf.ehcache=ERROR
log4j.logger.net.sf.navigator=ERROR
log4j.logger.org.apache.commons=ERROR
log4j.logger.org.apache.struts=ERROR
# Struts OgnlUtil issues unimportant warnings
log4j.logger.com.opensymphony.xwork2.util.OgnlUtil=error
log4j.logger.com.opensymphony.xwork2.ognl.OgnlValueStack=error
  3、maven包:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
  4、右鍵測試文件進行測試即可。
  PS: ApplicationContext applicationContext = new FileSystemXmlApplicationContext("src/main/webapp/WEB-INF/applicationContext.xml");
  等價于
  @RunWith(SpringJUnit4ClassRunner.class)   @ContextConfiguration(locations={"file:src/main/webapp/WEB-INF/applicationContext.xml"})
  ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
  等價于
  @RunWith(SpringJUnit4ClassRunner.class)   @ContextConfiguration(locations={"classpath:applicationContext.xml"})

軟件測試工具 | 聯(lián)系我們 | 投訴建議 | 誠聘英才 | 申請使用列表 | 網站地圖
滬ICP備07036474 2003-2017 版權所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd