SpringBoot整合Junit

SpringBoot整合Junit

使用步骤

搭建SpringBoot工程

SpringBoot整合Junit
SpringBoot整合Junit
SpringBoot整合Junit

引入starter-test起步依赖

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

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

编写测试类

package com.wt.demo17.service;

import org.springframework.stereotype.Service;

@Service
public class UserService {

    public void add(){
        System.out.println("吃饭");
    }
}

package com.wt.demo17;
import com.wt.demo17.service.UserService;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;

public class Demo17ApplicationTests {

@Autowired
	private UserService userService;

	public void testJunit() {
		userService.add();

	}
	}

添加注解

在相同目录下(与UserService相同目录)不用加启动类的class文件

@RunWith(SpringRunner.class)
@SpringBootTest

在不相同目录下(与UserService不相同目录)加启动类的class文件

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Demo17Application.class)

最终

package com.wt.demo17;
import com.wt.demo17.service.UserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.boot.test.context.SpringBootTest;


@RunWith(SpringRunner.class)
@SpringBootTest
public class Demo17ApplicationTests {

@Autowired
	private UserService userService;

	@Test
	public void testJunit() {
		userService.add();

	}
	}
上一篇:salt-master配置文件详解


下一篇:Ribbon:结合restTemplet实现轮训调用(负载均衡)