SpringBoot单元测试RunWith注解无法解析

关于SpringBoot单元测试RunWith注解没有提示,无法解析,没有导包提示的问题,笔者在此说明一下。
首先说明一下项目的环境
IDE是IDEA
创建项目使用的是Spring Initializr工具
在这里插入图片描述
JDK环境是1.8
SpringBoot版本是

<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>2.4.0</version>
	<relativePath/>
</parent>

测试相关的依赖

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-test</artifactId>
	<scope>test</scope>
</dependency>

笔者的测试类代码

package com.atguigu.delete;
import com.atguigu.delete.domain.Student;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
public class DeleteApplicationTests {
	@Autowired
	private Student student;
	@Test
	public void contextLoads() {
		System.out.println(student);
	}

}

直接运行测试方法
在这里插入图片描述
测试完成,输出正确。
明明没有写

@RunWith(SpringRunner.class)

注解为什么依然测试通过了呢
这是因为SpringBoot的高级版本使用junit5进行单元测试,无需RunWith注解即可进行测试。
应该是2.0及以上版本都是如此,至少2.4.0是如此。以后测试是不是更简单了呢
另外说下一些小问题
首先,使用Spring Initializr创建的项目
在这里插入图片描述
蓝色标注的文件或者文件夹可以删除,没什么用。
附上实体类和配置文件的代码

student.id=1
student.name=tomcat
student.gender=1
student.birth=2020/11/26

注意,如果没有配置日期格式化,日期的格式请按笔者的来
实体类代码

package com.atguigu.delete.domain;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.Date;
@ConfigurationProperties(prefix = "student")
@Component
public class Student {
	private Integer id;
	private String name;
	private Integer gender;
	private Date birth;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getGender() {
		return gender;
	}
	public void setGender(Integer gender) {
		this.gender = gender;
	}
	public Date getBirth() {
		return birth;
	}
	public void setBirth(Date birth) {
		this.birth = birth;
	}

	@Override
	public String toString() {
		return "Student[" +
				"id=" + id +
				", name=" + name +
				", gender=" + gender +
				", birth=" + birth +
				']';
	}
}

实体类代码没啥好说的,不要忘了注解
以上就是笔者对SpringBoot单元测试的说明,写的不好,请见谅

  • 6
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值