Service层的怎么写测试类

给出具体代码

 代码部分

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package com.java.service;
 
/**
 * @Description:
 * @Author: qiuxie
 * @Create: 2024/4/12 0:26
 */
public interface TestService {
 
    void test();
}
 
 
package com.java.service;
 
import org.springframework.stereotype.Service;
 
/**
 * @Description:
 * @Author: qiuxie
 * @Create: 2024/4/12 0:26
 */
@Service
public class TestServiceImpl implements TestService{
    @Override
    public void test() {
        System.out.println("service测试.....");
    }
}

  启动类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.java;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
/**
 * @Description:  设置年轻代和老年代的比例  1:4
 * E:S0:S1  8:1:1
 * @Author: Yourheart
 * @Create: 2023/4/21 11:27
 */
@SpringBootApplication()
public class MysqlServiceApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(MysqlServiceApplication.class, args);
    }
 
}

  无任何配置文件

测试类代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package com.java;
 
import com.java.service.TestService;
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;
 
/**
 * @Description:
 * @Author: qiuxie
 * @Create: 2024/4/12 0:27
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class Tests {
 
    /**
     * 这里做对象的声明
     */
    @Autowired
    private TestService testService;
 
    @Test
    public void test(){
        testService.test();
    }
 
}

  最后给出pom文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
 
    <groupId>org.example</groupId>
    <artifactId>service-test</artifactId>
    <version>1.0-SNAPSHOT</version>
 
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.14</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <properties>
        <java.version>1.8</java.version>
        <skipTests>true</skipTests>
    </properties>
 
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
 
 
</project>

  

 

posted @   不忘初心2021  阅读(7)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
历史上的今天:
2022-04-12 java.lang.IllegalStateException: Duplicate key ex-qixuie002
2022-04-12 javascript中select的获取选中项的索引
2022-04-12 Failed to bind properties under 'mybatis.configuration.mapped-statements[0]
点击右上角即可分享
微信分享提示