SpringBoot测试失败并报错: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration
1 整合JUnit
一、src/main/java下创建包com.example
包下创建启动类MyApplication
二、src/test/java下创建包com.example
包下创建测试类MyApplicationTests
1.1 pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.5</version>
<relativePath/>
</parent>
<dependencies>
<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>
</dependencies>
1.2 基本应用
1.2.1 启动类MyApplication.java
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
1.2.2 测试类MyApplicationTests
package com.example;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
public class MyApplicationTests {
@Test
void contextLoads(){
System.out.println("afsf");
}
}
1.3 测试对象
1、注入要测试的对象@Autowired
2、执行要测试的对象对应的方法
2 整合MyBatis
核心配置:数据库连接相关信息(连什么?连谁?什么权限?)
映射配置:SQL映射(XML/注解)
2.1 pom.xml
1、导入对应starter
2、配置相关信息文章来源:https://www.toymoban.com/news/detail-403826.html
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.5</version>
<relativePath/>
</parent>
<dependencies>
<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>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
文章来源地址https://www.toymoban.com/news/detail-403826.html
2.2 application.yml
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/test
username: root
password: bigdata
2.3 数据库
一、数据库test
二、建表语句
CREATE TABLE `user` (
`id` int NOT NULL AUTO_INCREMENT,
`username` varchar(50) DEFAULT NULL,
`password` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`)
);
三、插入数据
id,username,password
1,lily,1111
2,lucy,2222
3,lilei,3333
2.4 代码文件
2.4.1 实体类User
package com.example.domain;
public class User {
private Integer id;
private String username;
private String password;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
//有参构造方法
public User(Integer id,String username,String password) {
this.id=id;
this.username=username;
this.password=password;
}
// 无参构造方法
public User() {
}
@Override
public String toString() {
return "User{id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
2.4.2 接口类UserDao
package com.example.dao;
import com.example.domain.User;
import org.apache.ibatis.annotations.*;
import java.util.List;
@Mapper
public interface UserDao {
//查询全部数据
@Select("select * from user")
public List<User> findAll();
}
2.4.3 启动类MyApplication
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
2.4.4 测试类MyApplicationTests
package com.example;
import com.example.dao.UserDao;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
public class MyApplicationTests {
@Autowired
private UserDao userDao;
@Test
void contextLoads(){
System.out.println(userDao.findAll());
}
}
到了这里,关于JAVA-10-[SpringBoot]整合JUnit和MyBatis的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!