不用浏览器,用@Test直接测试spring boot项目接口
约 652 字大约 2 分钟
2025-07-11
按照 按照 CSMN 的层级写一个 spring boot 项目 的步骤写好了一个 spring boot 项目后,如果想测试接口写的对不对,需要打开浏览器配合。
如果不想用浏览器测试,而是想直接在 Test 里用 @Test
方法测试,需要使用一个注解 @Autowired
先看一下目录结构:
新建一个方法 test()
,上方加注解 @Test
表示这是一个测试方法。
然后需要调用 studentMapper.findAll();
@Test
public void test() {
studentMapper.findAll();
}
但是调用 studentMapper.findAll();
之前肯定要先声明 studentMapper
private StudentMapper studentMapper;
这个时候直接运行 test()
会报错
java.lang.NullPointerException: Cannot invoke "com.example.springbootmybatistest.Mapper.StudentMapper.findAll()" because "this.studentMapper" is null
因为 private StudentMapper studentMapper;
声明之后直接调用 studentMapper.findAll(); ``studentMapper
确实是 null
。
但前面也说了 StudentMapper
是 interface
,不能直接 new
。
如果是完整例子,在 controller
中调用 studentMapper.findAll();
Spring boot 会自动管理 bean 对象,
但这里我们也没有写 controller
,所以这时候需要另一个注解 @Autowired
。
@Autowired 对类成员变量、方法及构造函数进行标注,让 spring 完成 bean 自动装配的工作。
加了 @Autowired
之后再次运行 test()
package com.example.springbootmybatistest;
import com.example.springbootmybatistest.Mapper.StudentMapper;
import com.example.springbootmybatistest.Entity.Student;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
@SpringBootTest
class SpringbootMybatisTestApplicationTests {
@Autowired
// @Autowired对类成员变量、方法及构造函数进行标注,让 spring 完成 bean 自动装配的工作。
private StudentMapper studentMapper;
/**
* 这个例子不写controller
* 直接在test中测试mapper中的方法
*/
@Test
public void test() {
List<Student> students = studentMapper.findAll();
// 打印查询结果
for (Student student:students) {
System.out.println(student);
}
}
}
打印结果
Student(id=1, name=alice, age=26, sex=female, grade=1)
Student(id=2, name=Jane, age=28, sex=female, grade=2)
Student(id=3, name=Mary, age=65, sex=female, grade=1)
Student(id=4, name=lily, age=45, sex=gril, grade=2)
Student(id=5, name=yss, age=78, sex=female, grade=3)
其她方法也一样。
package com.example.springbootmybatistest.Mapper;
import com.example.springbootmybatistest.Entity.Student;
import org.apache.ibatis.annotations.Select;
import java.util.List;
public interface StudentMapper {
@Select("select * from student")
List<Student> findAll();
@Select("select count(*) from student")
int countStudent();
@Select("select * from student where id = #{id};")
List<Student> findStudentByColumn(int id);
}
package com.example.springbootmybatistest;
import com.example.springbootmybatistest.Mapper.StudentMapper;
import com.example.springbootmybatistest.Entity.Student;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
@SpringBootTest
class SpringbootMybatisTestApplicationTests {
@Autowired
private StudentMapper studentMapper;
@Test
public void test() {
List<Student> students = studentMapper.findAll();
for (Student student:students) {
System.out.println(student);
}
int countStudent = studentMapper.countStudent();
System.out.println(countStudent);
List<Student> studentss = studentMapper.findStudentByColumn(2);
for (Student student:studentss) {
System.out.println(student);
}
}
}
运行结果:
Student(id=1, name=alice, age=26, sex=female, grade=1)
Student(id=2, name=Jane, age=28, sex=female, grade=2)
Student(id=3, name=Mary, age=65, sex=female, grade=1)
Student(id=4, name=lily, age=45, sex=gril, grade=2)
Student(id=5, name=yss, age=78, sex=female, grade=3)
5
Student(id=2, name=Jane, age=28, sex=female, grade=2)