Cool
Cool
Published on 2021-07-07 / 24 Visits
0
0

SpringBoot整合Junit

添加Junit的起步依赖

<!--测试的起步依赖-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>

{message type="success" content="有时候需要注释掉scope"/}

  <!--测试的起步依赖-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
<!--<scope>test</scope>-->
</dependency>
package com.chinasoft.test;

import com.chinasoft.MySpringBootApplication;
import com.chinasoft.domain.User;
import com.chinasoft.mapper.UserMapper;
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 java.util.List;
/*@runWith注解作用:
@RunWith就是一个运行器
@RunWith(JUnit4.class)就是指用JUnit4来运行
@RunWith(SpringJUnit4ClassRunner.class),让测试运行于Spring测试环 境,以便在测试开始的时候自动创建Spring的应用上下文
@RunWith(Suite.class)的话就是一套测试集合*/
//这里推荐下面这种
@RunWith(SpringRunner.class)
  //主程序启动类  一定要加
@SpringBootTest(classes = MySpringBootApplication.class)
public class MapperTest {

    @Autowired
    private UserMapper userMapper;

    @Test
    public void test() {
        List<User> users = userMapper.queryUserList();
        System.out.println(users);
    }

}

Comment