日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

spring junit单元测试

發布時間:2023/12/10 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring junit单元测试 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

項目是有很多個功能塊組成的,我們開發的時候,當我們開發出來一個功能,想要測試這個功能是否正確,不可能等到前端和后端全部寫好了再進行測試,這樣太浪費時間,有沒有什么方法能直接測試后臺的功能寫的是否正確(比如: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{@Overridepublic 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 {@Autowiredprivate ISayHello sayhello;@Testpublic void test() {sayhello.sayHello();} }

其中:

@RunWith(SpringJUnit4ClassRunner.class)

通過注解的形式,意思是使用JUnit4進行測試

@ContextConfiguration(locations="classpath:spring.xml")

通過注解的形式,意思是加載spring.xml配置文件

@Autowiredprivate ISayHello sayhello;

因為是引入了ISayHello抽象,這個抽象是一個接口,使用@Autowired注解形式來幫我們注入,@Autowired是根據抽象類型,自動去spring容器中,幫我們找到類型是ISayHello的實現類,然后把對象自動注入進來,我們就可以直接使用了該對象了,然后可以調用到該抽象里的方法了。

@Testpublic void test() {sayhello.sayHello();}

其中:@Test是說明是單元測試,必須有的。

?

運行結果是:

?

轉載于:https://www.cnblogs.com/WQX-work24/p/9928110.html

總結

以上是生活随笔為你收集整理的spring junit单元测试的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。