不用浏览器,用@Test直接测试spring boot项目接口
按照 https://www.cnblogs.com/sunshine233/p/18829119 的步骤写好了一个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
@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)

浙公网安备 33010602011771号