美文网首页
Springboot如何用测试类

Springboot如何用测试类

作者: 花开半時偏妍 | 来源:发表于2020-06-28 16:03 被阅读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);
    }
}

相关文章

网友评论

      本文标题:Springboot如何用测试类

      本文链接:https://www.haomeiwen.com/subject/zljwfktx.html