SpringBoot整合Junit
创建新模块
什么都不勾选
创建成功
编辑pom文件
删除这些内容
运行测试类
模拟dao层
package com.taotao.dao;
/**
* create by 刘鸿涛
* 2022/5/1 12:43
*/
@SuppressWarnings({"all"})
public interface BookDao {
public void save();
}
package com.taotao.dao.impl;
import com.taotao.dao.BookDao;
import org.springframework.stereotype.Repository;
/**
* create by 刘鸿涛
* 2022/5/1 12:45
*/
//使他变成一个bean@Repository
@Repository
@SuppressWarnings({"all"})
public class BookDaoImpl implements BookDao {
@Override
public void save() {
System.out.println("book dao is running....");
}
}
测试dao层
package com.taotao.springboot_04_junit;
import com.taotao.dao.BookDao;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class Springboot04JunitApplicationTests {
//自动装配
@Autowired
private BookDao bookDao;
//执行要测试的对象的对应方法
@Test
void contextLoads() {
System.out.println("springboot text run...");
bookDao.save();
}
}
成功测试
小结
classes属性
遇到问题
更换Application文件路径后
解决方案1
SpringBootTest注解指定classes
测试成功
解决方案2
@ContextConfiguration注解指定classes