Spring Boot 单元测试

本文介绍了如何在Spring Boot项目中进行单元测试。首先需要在pom.xml添加测试依赖,接着展示目标代码,并讲解测试步骤:利用SpringApplication.run初始化上下文,通过getBean获取Controller并调用相关方法进行测试。虽然没有明确的AfterClass方法销毁Spring Boot程序,但在测试用例结束后通常会自动销毁。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

         Spring boot 是一款快速的web开发框架。它包含了基本的Spring mvc, Spring security 还有ORM 框架Hibernate等组件。使用非常方便,一旦尝试,基本告别SSH框架。

话不多说,今天来看看怎么基于Spring boot做单元测试。


首先,我们需要在pom.xml中添加必要的依赖

<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-security</artifactId>
		</dependency>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
			<scope>test</scope>
		</dependency>
</dependencies>

然后看下我们需要进行单元测试的目标代码

@Controller
public class HomeController {

	@ResponseBody
	@RequestMapping("/hello")
	public String hello() {
		return "world";
	}
}


这里是一个controller,我们需要测试hello()是否会返回给我"world"。


测试代码

package com.scm.dashboard.controller;

import static org.junit.Assert.*;

import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.mock.web.MockServletContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.context.ConfigurableApplicationContext;

import com.scm.dashboard.Application;


@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = MockServletContext.class)
@WebAppConfiguration
public class HomeControllerTest {
	private static ConfigurableApplicationContext context;
	private static HomeController controller;

	
	@BeforeClass
	public static void setUp() {
		context = SpringApplication.run(Application.class, "");
		controller = (HomeController) context.getBean("homeController");
	}

	@Test
	public void testHello() {
		assertEquals(controller.hello(), "world");
		
	}

}

注意一下几点:

1.  在测试用例前必须调用context = SpringApplication.run(Application.class, "");

     用过Spring的应该清楚,以前我们是通过一个application.xml来生成这个context,现在用的是全注解方式。

2. 在这个上下文context中拿到已经注入的controller,调用context.getBean()方法,这是一个潜在的规则,Spring 在注入代码的时候变量名是按照驼峰命名的。

3.  跑hello()方法


是比较简单,当然,按道理应该还要写一个AfterClass的方法来销毁Spring boot程序,但是研究半天没找到解决办法,而且好像测试用例结束后就自行销毁了,应该不是大问题。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值