springboot項目或其他項目使用@Test測試項目接口配置
1. springboot項目在pom配置
添加此依賴
dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> /dependency>
2. 對要測試的class創(chuàng)建測試文件
2.1 按快捷鍵ctrl+shift+t
選擇創(chuàng)建New Test,勾選需要創(chuàng)建單元測試的方法,然后點擊OK就直接創(chuàng)建完成了,會在test目錄下生成相應(yīng)的測試類
在類上面加上注解
@RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest
如果測試的是service
demo如下:
package com.example.demo; 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.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest public class HelloServiceTest { @Autowired private HelloService helloService; @Test public void hello(){ helloService.hello(); } }
如果測試的是controller,需要加入@AutoConfigureMockMvc的注解
demo如下:
package com.example.demo; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import org.springframework.test.web.servlet.result.MockMvcResultMatchers; @RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest @AutoConfigureMockMvc public class HelloControllerTest { @Autowired private MockMvc mockMvc; @Test public void hello() throws Exception { mockMvc.perform(MockMvcRequestBuilders.get("/hello")) .andExpect(MockMvcResultMatchers.status().isOk()); } }
2.2 在test(沒有此目錄就需要創(chuàng)建)目錄的java類下
在類上添加注解@SpringBootTest和@RunWith(SpringRunner.class)
在方法上添加@Test
import javax.annotation.Resource; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import com.hujy.demo.service.impl.DbHistoryConfig; @RunWith(SpringRunner.class) @SpringBootTest public class ShardingJdbcDemoApplicationTests { @Resource private DbHistoryConfig dbHistoryConfig; @Test public void fileRenew(){ dbHistoryConfig.fileRenew("1"); } }
@SpringBootTest和@RunWith(SpringRunner.class)是為了啟動springboot項目,然后@Test注解中使用ioc容器
3.普通項目測試
也需要在test(沒有此目錄就需要創(chuàng)建)目錄的java類下
3.1 在方法上添加注解@Test
import com.hujy.demo.service.impl.DbHistoryConfig; import org.junit.Test; public class ShardingJdbcDemoApplicationTests { @Test public void fileRenew() { new DbHistoryConfig().fileRenew("1"); } }
3.2 在pom添加依賴
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency>
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
解決Java中基于GeoTools的Shapefile讀取亂碼的問題
本文主要討論了在使用Java編程語言進行地理信息數(shù)據(jù)解析時遇到的Shapefile屬性信息亂碼問題,以及根據(jù)不同的編碼設(shè)置進行屬性信息解析的方法,感興趣的朋友跟隨小編一起看看吧2025-03-03Java實現(xiàn)度分秒坐標(biāo)轉(zhuǎn)十進制度
隨著技術(shù)的發(fā)展,十進制度因其精確性和便捷性在現(xiàn)代應(yīng)用中越來越受到青睞,下面我們就來看看如何使用Java實現(xiàn)度分秒坐標(biāo)轉(zhuǎn)十進制度吧2024-12-12java學(xué)習(xí)之JasperReport踩坑
本篇文章介紹的是在JAVA學(xué)習(xí)中JasperReport遇到的坑以及解決辦法,有需要的朋友參考下吧。2018-01-01Windows10 Java環(huán)境變量配置過程圖解
這篇文章主要介紹了Windows10 Java環(huán)境變量配置過程圖解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-07-07關(guān)于spring中單例Bean引用原型Bean產(chǎn)生的問題及解決
這篇文章主要介紹了關(guān)于spring中單例Bean引用原型Bean產(chǎn)生的問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06基于Redis實現(xiàn)分布式應(yīng)用限流的方法
本篇文章主要介紹了基于 Redis 實現(xiàn)分布式應(yīng)用限流的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-12-12Caused by: java.lang.ClassNotFoundException: org.objectweb.a
這篇文章主要介紹了Caused by: java.lang.ClassNotFoundException: org.objectweb.asm.Type異常,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07Springboot如何添加server.servlet.context-path相關(guān)使用
這篇文章主要介紹了Springboot如何添加server.servlet.context-path相關(guān)使用問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03