Spring测试代码讲解示例

2024-06-22 17:18

本文主要是介绍Spring测试代码讲解示例,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

  • 1 Spring测试简介
  • 2 Spring运行项目测试
    • 2.1 pom.xml
    • 2.2 测试实例bean
    • 2.3 测试配置
    • 2.4 测试运行类
  • 3 不运行项目测试
    • 3.1 ClassPathXmlApplicationContext
    • 3.2 AnnotationConfigApplicationContext

1 Spring测试简介

测试时开发工作中不可缺少的部分,单元测试只针对当前开放的类和方法进行测试,可以简单通过模拟依赖来实现,对运行环境没有依赖,但是仅仅进行单元测试是不够的,它只能验证当前类或方法能否正常工作
Spring通过Spring TestContext Framework对基础测试提供顶级支持,它不依赖于特定的测试框架,可以使用junit,或者TestNG
Spring提供了一个SpringJUnit4ClassRunner类,它提供了Spring TestContext Framework的功能。通过@ContextConfiguration来配置Application Context,通过@ActiveProfiles确定活动的profile

2 Spring运行项目测试

2.1 pom.xml

		<dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.2.9.RELEASE</version><scope>compile</scope></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>5.2.9.RELEASE</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version></dependency>

2.2 测试实例bean

package cn.jzh.test;public class TestBean {private String msg;public TestBean(String msg){this.msg= msg;}public String getMsg() {return msg;}public void setMsg(String msg) {this.msg = msg;}
}

2.3 测试配置

package cn.jzh.test;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.test.context.ActiveProfiles;@Configurationpublic class TestConfig {@Bean@Profile("dev")public TestBean devTestBean(){return new TestBean("from dev profile");}@Bean@Profile("prod")public TestBean prodTestBean(){return new TestBean("from prod profile");}
}

2.4 测试运行类

package cn.jzh.test;import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;@RunWith(SpringJUnit4ClassRunner.class)//SpringJUnit4ClassRunner在junit环境下提供Spring TestContext Framework的功能
@ContextConfiguration(classes = {TestConfig.class})//用来加载配置applicationContext,其中classes属性用来加载配置类
@ActiveProfiles("dev")//用来声明活动的profile
public class TestMain {@Autowiredpublic TestBean bean;@Testpublic void pordBean(){String msg = bean.getMsg();System.out.println(msg);}
}

3 不运行项目测试

配置文件使用xml的话,就用ClassPathXmlApplicationContext读取配置文件来测试,如果用注解的话,就用AnnotationConfigApplicationContext

3.1 ClassPathXmlApplicationContext

在src下新建配置文件applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><bean id="userinfoJdbcDao" class="cn.test.dao.impl.UserinfoJdbcDaoImpl"></bean>    <bean id="userinfoHibDao" class="cn.test.dao.impl.UserinfoHibDaoImpl"></bean>    <bean id="userinfoService" class="cn.test.service.impl.UserinfoServiceImpl"><property name="userinfoDao" ref="userinfoHibDao"></property></bean>
</beans>

测试

public class Test {public static void main(String[] args) {ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");UserinfoService userinfoService = applicationContext.getBean(UserinfoService.class,"userinfoService");Userinfo user = new Userinfo();user.setUserId(1);user.setUserName("test");user.setUserPass("test");userinfoService.save(user);}
}

3.2 AnnotationConfigApplicationContext

@Configuration
@ComponentScan("cn.jzh.aop")
@EnableAspectJAutoProxy//开启spring对aspectj代理的支持
public class AopConfig {
}
@Service
public class DemoMethodService {public void add(){System.out.println("service................");}
}
public class AopMain {public static void main(String[] args) {AnnotationConfigApplicationContext acac = new AnnotationConfigApplicationContext(AopConfig.class);DemoMethodService bean = acac.getBean(DemoMethodService.class);bean.add();acac.close();}
}

这篇关于Spring测试代码讲解示例的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1084918

相关文章

Spring Security常见问题及解决方案

《SpringSecurity常见问题及解决方案》SpringSecurity是Spring生态的安全框架,提供认证、授权及攻击防护,支持JWT、OAuth2集成,适用于保护Spring应用,需配置... 目录Spring Security 简介Spring Security 核心概念1. ​Securit

MySQL 8 中的一个强大功能 JSON_TABLE示例详解

《MySQL8中的一个强大功能JSON_TABLE示例详解》JSON_TABLE是MySQL8中引入的一个强大功能,它允许用户将JSON数据转换为关系表格式,从而可以更方便地在SQL查询中处理J... 目录基本语法示例示例查询解释应用场景不适用场景1. ‌jsON 数据结构过于复杂或动态变化‌2. ‌性能要

SpringBoot+EasyPOI轻松实现Excel和Word导出PDF

《SpringBoot+EasyPOI轻松实现Excel和Word导出PDF》在企业级开发中,将Excel和Word文档导出为PDF是常见需求,本文将结合​​EasyPOI和​​Aspose系列工具实... 目录一、环境准备与依赖配置1.1 方案选型1.2 依赖配置(商业库方案)二、Excel 导出 PDF

Python实现MQTT通信的示例代码

《Python实现MQTT通信的示例代码》本文主要介绍了Python实现MQTT通信的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录1. 安装paho-mqtt库‌2. 搭建MQTT代理服务器(Broker)‌‌3. pytho

SpringBoot改造MCP服务器的详细说明(StreamableHTTP 类型)

《SpringBoot改造MCP服务器的详细说明(StreamableHTTP类型)》本文介绍了SpringBoot如何实现MCPStreamableHTTP服务器,并且使用CherryStudio... 目录SpringBoot改造MCP服务器(StreamableHTTP)1 项目说明2 使用说明2.1

spring中的@MapperScan注解属性解析

《spring中的@MapperScan注解属性解析》@MapperScan是Spring集成MyBatis时自动扫描Mapper接口的注解,简化配置并支持多数据源,通过属性控制扫描路径和过滤条件,利... 目录一、核心功能与作用二、注解属性解析三、底层实现原理四、使用场景与最佳实践五、注意事项与常见问题六

Spring的RedisTemplate的json反序列泛型丢失问题解决

《Spring的RedisTemplate的json反序列泛型丢失问题解决》本文主要介绍了SpringRedisTemplate中使用JSON序列化时泛型信息丢失的问题及其提出三种解决方案,可以根据性... 目录背景解决方案方案一方案二方案三总结背景在使用RedisTemplate操作redis时我们针对

Java中Arrays类和Collections类常用方法示例详解

《Java中Arrays类和Collections类常用方法示例详解》本文总结了Java中Arrays和Collections类的常用方法,涵盖数组填充、排序、搜索、复制、列表转换等操作,帮助开发者高... 目录Arrays.fill()相关用法Arrays.toString()Arrays.sort()A

Spring Boot Maven 插件如何构建可执行 JAR 的核心配置

《SpringBootMaven插件如何构建可执行JAR的核心配置》SpringBoot核心Maven插件,用于生成可执行JAR/WAR,内置服务器简化部署,支持热部署、多环境配置及依赖管理... 目录前言一、插件的核心功能与目标1.1 插件的定位1.2 插件的 Goals(目标)1.3 插件定位1.4 核

如何使用Lombok进行spring 注入

《如何使用Lombok进行spring注入》本文介绍如何用Lombok简化Spring注入,推荐优先使用setter注入,通过注解自动生成getter/setter及构造器,减少冗余代码,提升开发效... Lombok为了开发环境简化代码,好处不用多说。spring 注入方式为2种,构造器注入和setter