1.添加依赖
<!--springboot程序测试依赖,如果是⾃动创建项⽬默认添加-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
2.添加注解:
@RunWith(SpringRunner.class) //底层用junit SpringJUnit4ClassRunner
@SpringBootTest(classes={xxxx.class})//启动整个springboot工程 xxxxx为自己工程的启动类
3.常用单元测试的注解
@before
@Test
@After
断言:判断程序结果是否符合预期 TestCase.assertXXX
注意:每一个test都是独立的都会加载 @before和@after
代码:
@RunWith(SpringRunner.class)//底层用junit
@SpringBootTest(classes = {SsmstudyApplication.class})//启动整个springboot工具
public class VideoTest {
@Before
public void testbefore(){
System.out.println("这是before");
}
@Test
public void test1(){
int i1=2;
int i2=3;
int i4=i1+i2;
System.out.println("这是test1");
//断言
TestCase.assertEquals(5,i4);
}
@Test
public void test2(){
System.out.println("这是test2");
}
@After
public void testAfter(){
System.out.println("这是After");
}
}