項目是有很多個功能塊組成的,我們開發的時候,當我們開發出來一個功能,想要測試這個功能是否正確,不可能等到前端和后端全部寫好了再進行測試,這樣太浪費時間,有沒有什么方法能直接測試后台的功能寫的是否正確(比如:service這個模塊的功能)?當然有,下面講解一個Junit單元測試。
我們是在Maven中進行測試的:
在maven中新建一個java EE工程:
(1)打開maven,在左欄邊,右鍵=>new=>Maven Project
(2)不要勾選create a simple project這一項,點擊“next”
(3)選擇后綴名為“webapp”這一欄,點擊“next”
(4)填寫“Group Id”和“Artifact Id”,點擊“finish”
這樣就可以創建一個javaEE工程了,但是一個完整的項目有四個目錄,如下圖:
但是它會缺少幾個source folder,但是項目已經默認有,我們重新建缺少的那幾個目錄
使用Junit單元測試,需要兩個依賴jar包 Junit和test
我們通過配置來導入這兩個jar包及其所依賴的jar包,(通過maven中央倉庫 https://mvnrepository.com/)
(1)在pom.xml文件里添加響應的jar依賴
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>oracle.com</groupId> <artifactId>JunitTest</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>JunitTest Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <!-- https://mvnrepository.com/artifact/junit/junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-test --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.1.2.RELEASE</version> <scope>test</scope> </dependency> </dependencies> <build> <finalName>JunitTest</finalName> </build> </project>
下面我們通過一個小例子來說明,直接上代碼:
在service層來測試,新建一個接口以及該接口的實現類
ISayHello.java的代碼是:
package com.service; public interface ISayHello { public void sayHello(); }
SayHelloImpl.java的代碼是:
package com.service.imple; import com.service.ISayHello; public class SayHelloImpl implements ISayHello{ @Override public void sayHello() { System.out.println("你好啊!!!程序員"); } }
spring.xml代碼是:
<?xml version="1.0" encoding="UTF-8" ?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="hello" class="com.service.imple.SayHelloImpl"></bean> </beans>
spring.xml是實例化的第三方,是spring中IoC容器幫我們實例化對象的。其中class屬性是實現類的全路徑,id是實例化對象的名稱。
pom.xml代碼是:(我們還要導入spring的核心jar包)
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>oracle.com</groupId> <artifactId>JunitTest</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>JunitTest Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <!-- https://mvnrepository.com/artifact/junit/junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-test --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.1.2.RELEASE</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-core --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.1.1.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.1.1.RELEASE</version> </dependency> </dependencies> <build> <finalName>JunitTest</finalName> </build> </project>
在src/test/java該目錄項目新建一個JunitTset類進行測試:
JunitTset.java代碼是:
package com.test; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.service.ISayHello; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations="classpath:spring.xml") public class JunitTest { @Autowired private ISayHello sayhello; @Test public void test() { sayhello.sayHello(); } }
其中:
@RunWith(SpringJUnit4ClassRunner.class)
通過注解的形式,意思是使用JUnit4進行測試
@ContextConfiguration(locations="classpath:spring.xml")
通過注解的形式,意思是加載spring.xml配置文件
@Autowired private ISayHello sayhello;
因為是引入了ISayHello抽象,這個抽象是一個接口,使用@Autowired注解形式來幫我們注入,@Autowired是根據抽象類型,自動去spring容器中,幫我們找到類型是ISayHello的實現類,然后把對象自動注入進來,我們就可以直接使用了該對象了,然后可以調用到該抽象里的方法了。
@Test public void test() { sayhello.sayHello(); }
其中:@Test是說明是單元測試,必須有的。
運行結果是: