给若依添加单元测试(一)

给若依添加单元测试

方案二(异常困难但企业开发一般用这个)

方案一(简单)

在 admin 模块中添加单元测试

S1.在 src 目录下创建 test.java.MapperTests 文件
在这里插入图片描述

S2.将以下内容复制进去

import com.ruoyi.RuoYiApplication;
import com.ruoyi.activity.domain.Activity;
import com.ruoyi.activity.mapper.ActivityMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = RuoYiApplication.class)
public class MapperTests {

    @Autowired
    private ActivityMapper activityMapper;

    @Test
    public void testSelectActivityById() {
        Activity activity = activityMapper.selectActivityById(1);
        System.out.println(activity);
    }

    @Test
    public void testSelectActivityList() {
        List<Activity> activityList = activityMapper.selectActivityList(new Activity());
        System.out.println(activityList);
    }
    

}


S3.将以下内容添加到 admin 的 pom.xml 中

        <!-- 测试所需 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>

S4.在 ActivityMapper 前添加 Mapper 注解
在这里插入图片描述
成功:
在这里插入图片描述

方案二(异常复杂但实用)

在 activity 子模块中添加单元测试

  • 7
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
若依Spring Boot提供了方便的工具来进行单元测试。首先,你需要在需要进行单元测试的服务模块中引入依赖spring-boot-starter-test。这可以通过在pom.xml文件中添加以下代码来实现: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> ``` 接下来,你需要创建一个测试类。注意,测试类的位置应该与Spring Boot的启动类对应。如果你的测试类位置改变了,可能会找不到启动类。你可以通过在测试类上添加`@SpringBootTest`注解来告诉Spring Boot这是一个测试类,并且它需要启动整个Spring应用程序。 以下是一个简单的单元测试案例的示例代码: ```java import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @SpringBootTest public class MyServiceTest { @Autowired private MyService myService; @Test public void testMyService() { // 测试代码 } } ``` 在这个例子中,我们使用了`@Autowired`注解来自动注入一个`MyService`实例,然后我们可以在`testMyService`方法中编写具体的测试代码。你可以根据需要添加更多的测试方法来测试不同的功能。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [若依微服务版(SpringBoot/SpringCloudAlibaba)中在单个服务模块中进行单元测试](https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/128900845)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [若依框架如何进行单元测试](https://blog.csdn.net/qq_19309473/article/details/122147522)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值