欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

架构师之maven(二)junit4.11+spring4.1的测试配置

程序员文章站 2022-03-01 21:44:39
...
1.前言.
   maven的junit测试是需要遵守一些规则的.
2.步骤.
  (1)src和test的文件和class目录最好是默认的.
  (2)spring4的包不要少了 .
  (3)BaseTest写法.
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath*:aa_dependence.xml" ,"classpath*:bb.xml"})
public class BaseTest {
	
  @Test
  public void test(){
	  System.out.println("ok");
  }
}




3.可能出现的问题.
(1)不能autowire,读不到配置文件
这个需要在pom.xml里的build加上resoures文件(此步在默认maven的class文件夹有文件的情况不要配,配置反而会出错!!!!!!!).如:
<resources>
		<resource>
			<directory>src/main/resources</directory>
				<includes>
					<include>**/*.properties</include>
					<include>**/*.xml</include>
					<include>**/*.ftl</include>
				</includes>
				<filtering>true</filtering>
			</resource>
			<resource>
				<directory>src/main/java</directory>
				<includes>
					<include>**/*.xml</include>
				</includes>
				<filtering>true</filtering>
			</resource>
		</resources>



(4)缺少类库。
pom.xml加上
	<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.11</version>
		</dependency>
  		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-expression</artifactId>
			<version>4.1.5.RELEASE</version>
		</dependency>
  	  		<dependency>
			<groupId>com.caucho</groupId>
			<artifactId>hessian</artifactId>
			<version>4.0.7</version>
		</dependency>
		  <dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-validator</artifactId>
			<version>5.0.1.Final</version>
		</dependency>
		<dependency>
			<groupId>org.jboss.logging</groupId>
			<artifactId>jboss-logging</artifactId>
			<version>3.1.0.GA</version>
		</dependency>
		<dependency>
			<groupId>com.fasterxml</groupId>
			<artifactId>classmate</artifactId>
			<version>0.8.0</version>
		</dependency>
	 <dependency>
	        <groupId>org.apache.maven.surefire</groupId>
	        <artifactId>surefire-junit4</artifactId>
	        <version>2.10</version>
	      </dependency>


并且pom的plugins节点加上:
			 <plugin>
	    <groupId>org.apache.maven.plugins</groupId>
	    <artifactId>maven-surefire-plugin</artifactId>
	    <version>2.10</version>
	    <dependencies>
	      <dependency>
	        <groupId>org.apache.maven.surefire</groupId>
	        <artifactId>surefire-junit4</artifactId>
	        <version>2.10</version>
	      </dependency>
	    </dependencies>
 	  </plugin>



(5)不能找到class,这是maven操作把class清了,maven clean后重新点击Project->clean,将class重新生成过来。