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)建單元測試的方法,然后點(diǎn)擊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類加載異常:java.lang.ClassNotFoundException解決方法
這篇文章主要給大家介紹了關(guān)于Java類加載異常:java.lang.ClassNotFoundException的解決方法,異常是Java編程語言中的一個標(biāo)準(zhǔn)異常類,它繼承自類,當(dāng)在運(yùn)行時嘗試加載類時,如果系統(tǒng)找不到指定的類文件就會拋出該異常,需要的朋友可以參考下2023-11-11
Java利用DelayQueue實現(xiàn)延遲任務(wù)代碼實例
這篇文章主要介紹了Java利用DelayQueue實現(xiàn)延遲任務(wù)代碼實例,DelayQueue?是一個支持延時獲取元素的阻塞隊列,?內(nèi)部采用優(yōu)先隊列?PriorityQueue?存儲元素,同時元素必須實現(xiàn)?Delayed?接口,需要的朋友可以參考下2023-12-12
JSON--List集合轉(zhuǎn)換成JSON對象詳解
這篇文章主要介紹了List集合轉(zhuǎn)換成JSON對象,小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。2017-01-01
SpringBoot之controller參數(shù)校驗詳解
介紹了Java中使用@Validated和@Valid進(jìn)行參數(shù)校驗的方法,包括不同標(biāo)簽的使用場景、基本屬性和一些常用的注解類型,同時,還討論了如何在控制器中使用這些校驗標(biāo)簽,以及如何處理校驗結(jié)果和自定義錯誤消息,最后,還介紹了如何實現(xiàn)分組校驗和嵌套校驗,并提供了一些示例代碼2024-11-11

