Springboot如何用测试类

2020-06-28  本文已影响0人  花开半時偏妍

一.直接调用

1.pom.xml文件添加springboot测试包

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

2.创建一个测试类,直接添加@Test注解即可。

import org.junit.Test;
public class JunitTest{
    @Test
     public void sayHello(){
               System.out.println("Hello world");
       }
}

二.MVC形式调用

测试类,需要加上两个注解

 @RunWith(SpringRunner.class)
 @SpringBootTest(classes={App.class})

其中App.class是主程序入口类,即springboot的启动类

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import com.qfx.system.App;
import com.qfx.system.service.SysUserService;

@RunWith(SpringRunner.class)
@SpringBootTest(classes={App.class})
public class JunitTest {

    @Autowired
    SysUserService sysUserService;

    @Test
    public void printSysUserInfo(){
        String userStr = sysUserService.getSysUserAll();
        System.out.println(userStr);
    }
}
上一篇 下一篇

猜你喜欢

热点阅读