整合JUnit
在pom.xml引入依赖
xml
<!--整合JUnit-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>通过在测试类中添加@SpringBootTest注解可以实现整合JUnit测试
java
package testHello;
import com.atguigu.boot.Hello;
import com.atguigu.boot.MainApplication;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import javax.annotation.Resource;
@SpringBootTest(classes = MainApplication.class) // 当测试类处于引导类的包和子包下时,就不需要指定引导类(指定的配置类中存在此bean也可以(@Bean))
public class test {
@Autowired
private ApplicationContext applicationContext; // 获取IOC容器,通过
@Resource
private Hello hello;
@Test
public void testHello(){
hello.sayHello();
}
}也可以使用
@ContextConfiguration(locations = "classpath:xxx.xml" / classes = yyy.class),用于测试类上,用于引入指定的配置类
