SpringBoot 單元測試

2021-10-08 04:54:47 字數 2582 閱讀 9387

}輸出以下結果:

handler:

type = com.jia.controller.hellocontroller

method = com.jia.controller.hellocontroller#hello(string)

async:

async started = false

async result = null

resolved exception:

type = null

modelandview:

view name = null

view = null

model = null

flashmap:

attributes = null

建立乙個實體類用於測試

import lombok.data;

@data

public class user

用 @service 來標註服務類,並例項化了乙個 user 物件

import com.jia.pojo.user;

import org.springframework.stereotype.service;

@service

public class userservice

}

比較例項化的實體 user 和測試的預期值是否一樣

import com.jia.pojo.user;

import org.junit.assert;

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 static org.hamcrest.corematchers.*;

//表明要在測試環境執行,底層使用的junit測試工具

@runwith(springrunner.class)

// springjunit支援,由此引入spring-test框架支援!

//啟動整個spring的工程

@springboottest

public class userservicetest

}

期望值和實際值不一樣時:

Spring Boot 單元測試

由於spring boot在啟動時通常會先行啟動一些內建的元件,比如tomcat。因此,spring boot的測試類一般需要加一些簡單的註解。org.springframework.boot spring boot starter test test runwith標記乙個執行期springrun...

springboot單元測試

springboot對單元測試的支援十分完善,需要引入的jar包 org.springframework.boot spring boot starter test test 使用方式 在測試類的類頭部需要新增 runwith springrunner.class 和 springboottest註...

Spring Boot 單元測試

在所有測試方法前執行一次,一般在其中寫上整體初始化的 beforeclass 在所有測試方法後執行一次,一般在其中寫上銷毀和釋放資源的 afterclass 在每個測試方法前執行,一般用來初始化方法 比如我們在測試別的方法時,類中與其他測試方法共享的值已經被改變,為了保證測試結果的有效性,我們會在 ...