欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

springboot項目或其他項目使用@Test測試項目接口配置

 更新時間:2024年07月10日 11:14:27   作者:小徐敲java  
這篇文章主要介紹了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中基于GeoTools的Shapefile讀取亂碼的問題

    本文主要討論了在使用Java編程語言進行地理信息數(shù)據(jù)解析時遇到的Shapefile屬性信息亂碼問題,以及根據(jù)不同的編碼設(shè)置進行屬性信息解析的方法,感興趣的朋友跟隨小編一起看看吧
    2025-03-03
  • Java實現(xiàn)度分秒坐標(biāo)轉(zhuǎn)十進制度

    Java實現(xiàn)度分秒坐標(biāo)轉(zhuǎn)十進制度

    隨著技術(shù)的發(fā)展,十進制度因其精確性和便捷性在現(xiàn)代應(yīng)用中越來越受到青睞,下面我們就來看看如何使用Java實現(xiàn)度分秒坐標(biāo)轉(zhuǎn)十進制度吧
    2024-12-12
  • java學(xué)習(xí)之JasperReport踩坑

    java學(xué)習(xí)之JasperReport踩坑

    本篇文章介紹的是在JAVA學(xué)習(xí)中JasperReport遇到的坑以及解決辦法,有需要的朋友參考下吧。
    2018-01-01
  • SpringCloud Gateway路由核心原理解析

    SpringCloud Gateway路由核心原理解析

    本文主要介紹了SpringCloudGateway的基礎(chǔ)構(gòu)建塊、工作原理以及核心原理解析,SpringCloudGateway是Spring官方基于SpringSpringBoot和ProjectReactor等技術(shù)開發(fā)的網(wǎng)關(guān),旨在為微服務(wù)架構(gòu)提供一種簡單而有效的統(tǒng)一的API路由管理方式
    2024-10-10
  • Windows10 Java環(huán)境變量配置過程圖解

    Windows10 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)生的問題及解決

    這篇文章主要介紹了關(guān)于spring中單例Bean引用原型Bean產(chǎn)生的問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • 基于Redis實現(xiàn)分布式應(yīng)用限流的方法

    基于Redis實現(xiàn)分布式應(yīng)用限流的方法

    本篇文章主要介紹了基于 Redis 實現(xiàn)分布式應(yīng)用限流的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-12-12
  • Caused by: java.lang.ClassNotFoundException: org.objectweb.asm.Type異常

    Caused 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-07
  • Springboot如何添加server.servlet.context-path相關(guān)使用

    Springboot如何添加server.servlet.context-path相關(guān)使用

    這篇文章主要介紹了Springboot如何添加server.servlet.context-path相關(guān)使用問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • Java中的@builder建造者模式詳細(xì)解析

    Java中的@builder建造者模式詳細(xì)解析

    這篇文章主要介紹了Java中的@builder建造者模式詳細(xì)解析,使用 @Builder 注解可以簡化手動編寫建造者模式的代碼,使代碼更加簡潔易讀,它可以自動生成鏈?zhǔn)秸{(diào)用的方法來設(shè)置對象的屬性,并且可以在需要時進行可選屬性的設(shè)置,需要的朋友可以參考下
    2024-01-01

最新評論