最初の部分、SpringBootはjunitユニットテストを追加します
SpringBootはjunitを使用するのが非常に簡単です。まず見てみましょう。まず、これがspringboot2.0.4のバージョンです。
1つは、springbootテストパッケージを開くためのPom.xmlファイルです。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
2.テストクラスを作成する
1.単一ファイルテストの場合は、図に示すように、@ Testアノテーションを追加するだけです。
2. MVCフォーム呼び出し
テストクラス、2つの注釈を追加する必要がある
@RunWith(SpringRunner.class) @SpringBootTest(classes={App.class})
App.classはメインプログラムエントリクラスです。つまり、springbootのブートクラスです。
package com.qfx.test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import com.qfx.system.App;
import com.qfx.system.service.SysUserService;
(classes = {App.class})
パブリック クラス JunitTest {
<span class="hljs-meta">@Autowired</span>
SysUserService sysUserService;
<span class="hljs-meta">@Test</span>
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">printSysUserInfo</span><span class="hljs-params">()</span></span>{
String userStr = sysUserService.getSysUserAll();
System.out.println(userStr);
}
}
テストするとき、SpringbootはApp.classから開始されます。図に示すように、その効果を見てみましょう。
コントローラのテストに関しては、直接あなたが本当に使用JUnitテストしたい場合はあなたが使用することができ、ブラウザをテストするためにリクエストを送信するために、プロジェクトを開始することができますMockMvcこと
第二部、Spring Bootのテストライブラリ
Spring Bootは、主に次の2つのモジュールを含む、アプリケーションのテストに役立つ多くの実用的なツールと注釈を提供します。
-
spring-boot-test:テストをサポートするコアコンテンツ。
-
spring-boot-test-autoconfigure:テストの自動構成をサポートします。
限り使用するために開発さspring-boot-starter-test
イニシエータがテストモジュール春ブーツに導入することができるが、また、のようないくつか紹介JUnit, AssertJ, Hamcrest
以下に示すように、および他の有用なライブラリ。
- JUnit:Javaアプリケーションのユニットテスト用の標準クラスライブラリ。
- Spring Test&Spring Boot Test:Spring Bootアプリケーション機能の統合テストサポート。
- AssertJ:軽量のアサーションライブラリ。
- Hamcrest:オブジェクトマッチングライブラリ。
- Mockito:デフォルトの支払い1.xであるJava Mockテストフレームワークは2.xに変更できます。
- JSONassert:JSONのアサーションライブラリ。
- JsonPath:JSONオペレーションクラスライブラリ。
以下はMavenの依存関係図です。
これらは、Spring Bootが提供する、より一般的に使用されるテストライブラリの一部です。上記がニーズを満たさない場合は、上記で利用できない他のライブラリを追加することもできます。
Spring Bootアプリケーションをテストする
Maven依存関係を追加する
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>1.5.10.RELEASE</version>
<scope>test</scope>
</dependency>
1.共通クラスを単体テストクラスにするには、@ SpringBootTestおよび@RunWith(SpringRunner.class)の2つのアノテーションをクラス名に追加するだけです。
2.テストメソッドに@Testアノテーションを追加します。
テストでREST呼び出しを行う必要がある場合は、TestRestTemplateを@Autowireできます。
@RunWith(SpringRunner.class)
@SpringBootTest
public class BBTestAA {
@Autowired
プライベートTestRestTemplate testRestTemplate;
@Test
public void testDemo(){
…
}
}
GETリクエストテスト
@Test
public void get() throws Exception {
Map<String,String> multiValueMap = new HashMap<>();
multiValueMap.put("username","Java技术栈");
ActResult result = testRestTemplate.getForObject("/test/getUser?username={username}",ActResult.class,multiValueMap);
Assert.assertEquals(result.getCode(),0);
}
POSTリクエストテスト
@Test
public void post() throws Exception {
MultiValueMap multiValueMap = new LinkedMultiValueMap();
multiValueMap.add("username","Java技术栈");
ActResult result = testRestTemplate.postForObject("/test/post",multiValueMap,ActResult.class);
Assert.assertEquals(result.getCode(),0);
}
ファイルアップロードテスト
@Test
public void upload() throws Exception {
Resource resource = new FileSystemResource("/home/javastack/test.jar");
MultiValueMap multiValueMap = new LinkedMultiValueMap();
multiValueMap.add("username","Java技术栈");
multiValueMap.add("files",resource);
ActResult result = testRestTemplate.postForObject("/test/upload",multiValueMap,ActResult.class);
Assert.assertEquals(result.getCode(),0);
}
ファイルダウンロードテスト
@Test
public void download() throws Exception {
HttpHeaders headers = new HttpHeaders();
headers.set("token","javastack");
HttpEntity formEntity = new HttpEntity(headers);
String[] urlVariables = new String[]{"admin"};
ResponseEntity<byte[]> response = testRestTemplate.exchange("/test/download?username={1}",HttpMethod.GET,formEntity,byte[].class,urlVariables);
if (response.getStatusCode() == HttpStatus.OK) {
Files.write(response.getBody(),new File("/home/javastack/test.jar"));
}
}
上記の2番目の部分は、https://www.cnblogs.com/javastack/p/9150408.htmlから取得されます。