Spring框架集成单元测试

Spring框架集成单元测试

1、添加jar包坐标

https://mvnrepository.com/artifact/org.springframework/spring-test
https://mvnrepository.com/artifact/junit/junit/4.12

<!-- spring单元测试 -->
<dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-test</artifactId>
     <version>5.2.12.RELEASE</version>
     <scope>test</scope>
 </dependency>
 <dependency>
     <groupId>junit</groupId>
     <artifactId>junit</artifactId>
     <version>4.12</version>
     <scope>test</scope>
 </dependency>

注:有时候会出现jar包冲突,所以版本不一定最高,合适就行。
jar包版本冲突:
java.lang.IllegalStateException: Could not load TestContextBootstrapper [null]. Specify @BootstrapWith’s ‘value’ attribute or make the default bootstrapper class available.
mapper接口和mapper.xml文件没有映射起来:
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):

2、创建测试类并添加注解

package edu.pro.wenhua;

import edu.pro.wenhua.bean.Login;
import edu.pro.wenhua.service.LoginService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 * @author wenhua
 * @date 2022年04月11日 21:34
 */
@RunWith(SpringJUnit4ClassRunner.class)// junit整合spring的测试开启spring的注解
@ContextConfiguration(locations = "classpath:application.xml")// 加载核心配置文件,自动构建spring容器
public class TestDemo {

    @Autowired
    LoginService loginService;// 自动注入LoginService对象

    @Test
    public void TestLogin(){

        Login login = loginService.SelectLogin();
        System.out.println(login);
    }
}

单元测试在开发中是十分重要的,所以必须掌握和熟练;愿与君共勉,不负韶华。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值