4.Spring-Boot中基于Junit的单元测试

1.需要在pom.xml中引入spring-boot-starter-test

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

2.单元测试代码

两个关键的注解

@RunWith(SpringRunner.class)

@SpringBootTest

package com.niugang.test;

import java.util.List;

import javax.annotation.Resource;

import org.junit.Test;

import org.junit.runner.RunWith;

import org.springframework.boot.test.context.SpringBootTest;

import org.springframework.test.context.junit4.SpringRunner;

import com.niugang.entity.User;

import com.niugang.service.UserService;

@RunWith(SpringRunner.class)

@SpringBootTest

public class TestUser {

  @Resource

  private UserService userService;

  @Test

public void test1(){

  List<User> list = userService.queryList(null);

  System.out.println("集合大小:"+list.size());

}



}

 

注意不能不建包,直接写测试类

微信公众号

                          
posted @ 2020-01-15 15:42  盲目的拾荒者  阅读(420)  评论(0编辑  收藏  举报