文章目录
springboot测试类常出现的bug
1、junit版本太低的问题
2、spring-test和spring-boot-test和springboot版本不搭的问题
报错
Could not load TestContextBootstrapper [null]. Specify
@BootstrapWith's 'value' attribute or make the default
bootstrapper class available.
解决方式
3、保证测试类和启动类的包名一样,不然需要在@SpringBootTest指明启动类的位置
4、我的测试类文件
package com.books;
import com.books.entity.Book;
import com.books.service.impl.BookServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
//不加这两个注解,所有自动注入的对象将会失败(null)
@RunWith(SpringRunner.class)
@SpringBootTest(classes = com.books.BooksApplication.class)
//@Slf4j
public class SampleTest {
@Autowired
private BookServiceImpl bookService;
@Test
public void testSelect() {
System.out.println(("----- selectAll method test ------"));
List<Book> userList = bookService.list();
userList.forEach(System.out::println);
}
}