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

给若依添加单元测试

方案一(简单)

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

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

S1.在 src 目录下创建 test.java.MapperTests 文件
在这里插入图片描述
S2.将以下内容复制进去

import com.ruoyi.activity.ActivityApplication;
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.ArrayList;
import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = ActivityApplication.class)
@MapperScan(basePackages = "com.ruoyi.activity.mapper")
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 中

		<!-- Mysql驱动包 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!-- 阿里数据库连接池 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
        </dependency>
        <!-- SpringBoot Web容器 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- 测试所需 -->
        <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 注解
在这里插入图片描述
S5.在 activity 包下创建 ActivityApplication
在这里插入图片描述
S6.添加如下内容

package com.ruoyi.activity;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;

/**
 * 启动程序
 *
 * @author qiheng
 */
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
public class ActivityApplication
{
    public static void main(String[] args)
    {
        // System.setProperty("spring.devtools.restart.enabled", "false");
        SpringApplication.run(ActivityApplication.class, args);
        System.out.println("(♥◠‿◠)ノ゙  若依启动成功   ლ(´ڡ`ლ)゙  \n" +
                " .-------.       ____     __        \n" +
                " |  _ _   \\      \\   \\   /  /    \n" +
                " | ( ' )  |       \\  _. /  '       \n" +
                " |(_ o _) /        _( )_ .'         \n" +
                " | (_,_).' __  ___(_ o _)'          \n" +
                " |  |\\ \\  |  ||   |(_,_)'         \n" +
                " |  | \\ `'   /|   `-'  /           \n" +
                " |  |  \\    /  \\      /           \n" +
                " ''-'   `'-'    `-..-'              ");
    }
}


S7.分别从 admin 和 framework 模块中将相关配置文件复制过来
在这里插入图片描述
终于成功了!
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值