目录

点击下一步:

点击创建

如果你的项目使用Maven作为构建工具,那么pom.xml文件中会自动帮你注入依赖如下所示:

如果没有可以自行注入:
org.springframework.boot
spring-boot-starter-test
test

- package com.example.junit.Interface;
-
- public interface Product {
- public void save();
- }
- package com.example.junit.impl;
-
- import com.example.junit.Interface.Product;
- import org.springframework.stereotype.Repository;
-
- @Repository
- public class ProductImpl implements Product {
- @Override
- public void save() {
- System.out.println("product is running!!!");
- }
- }
- package com.example.junit;
-
- import com.example.junit.impl.ProductImpl;
- import org.junit.jupiter.api.Test;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.boot.test.context.SpringBootTest;
-
- //加载配置类
- @SpringBootTest(classes = JunitApplication.class)
- //@ContextConfiguration(classes = JunitApplication.class)
- class JunitApplicationTests {
-
- //1.注入要测试的对象
- @Autowired
- private ProductImpl productController;
-
-
- @Test
- void contextLoads() {
- //2.执行要测试的的对象对应的方法
- productController.save();
-
-
- }
-
- }
名称:@SpringBootTest
@SpringBootTest(classes = JunitApplication.class)
class JunitApplicationTests{}
classes:设置SpringBoot启动类
注意事项:
如果测试类在SpringBoot启动类的包或子包中,可以省略启动类的设置,也就是省略classes的设定