在Springboot 项目中使用Junit单元测试

在编写程序的过程中我们需要进行很多的测试,Junit单元测试

一:导入依赖

使用idea 创建一个新的springBoot 项目时,一般会自动导入test 依赖,如果没有请手动引入

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>

二: 测试类基类

在springBoot 项目目录结构下有一个自动生成的test 包,和一个默认的xxxtest 类,因为所有的测试类上面的注解基本都一样,所以写一个测试基类,其他的测试类直接继承这个基类

@RunWith(SpringRunner.class)
//如果是web 项目@WebAppConfiguration
@SpringBootTest
public class Test {
     
    @Before
    public void init() {
     
        System.out.println("开始测试...");
    }

    @After
    public void after(){
     
        System.out.println("测试结束");
    }
}

三:测试类

public class Spring_AOP_Test extends Test {
     
    @Autowired
    CalculatorImp calculatorImp;
    @Test
    public void aspectTest(){
     
        int result =calculatorImp.add(4,5);
        System.out.println(result);
    }
}

运行@Test 方法就可以执行,进行测试

你可能感兴趣的:(Spring,Boot)