[Spring Unit Testing] Spring Unit Testing with a Java Context
2023-09-14 09:00:48 时间
For example, we want to test against a implemataion:
package com.example.in28minutes.basic; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.config.ConfigurableBeanFactory; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; @Component @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) public class BinarySearchImpl { @Autowired @Qualifier("bubble") private SortAlgo sortAlgo; public int binarySearch(int [] numbers, int target) { // Sorting an array sortAlgo.sort(numbers); System.out.println(sortAlgo); // Quick sort // Return the result return 3; } }
We want to do the test with Java Context:
1. Bring in some dependencies: in pom.xml
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> <!-- <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> -->
Create a new test file:
package com.example.in28minutes.basic; import com.example.in28minutes.In28minutesBasicApplication; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringRunner; import static org.junit.Assert.assertEquals; // Load the context @RunWith(SpringRunner.class) @ContextConfiguration(classes = In28minutesBasicApplication.class) public class BinarySearchTest { // Get this bean from context @Autowired BinarySearchImpl binarySearchImpl; @Test public void testBasicScenario () { // call method on binarySearch int result = binarySearchImpl.binarySearch(new int[] {}, 5); // check if the value is correct assertEquals(3, result); } }
Two things we need to know:
1. Where can Java find 'BinarySearchImpl', which context it is:
@RunWith(SpringRunner.class) @ContextConfiguration(classes = In28minutesBasicApplication.class)
Basiclly we need to find out, where we register it:
@SpringBootApplication public class In28minutesBasicApplication { // What are the beans? --@Component // What are the dependencies of a bean? -- @AutoWired // Where to search for beans => NO NEED public static void main(String[] args) { // Application Context ApplicationContext applicationContext = SpringApplication.run(In28minutesBasicApplication.class, args); BinarySearchImpl binarySearch = applicationContext.getBean(BinarySearchImpl.class); .... } }
2. Once we load the context and run with spring runner, we can use '@Autowired' to bring in the bean from the context:
// Get this bean from context @Autowired BinarySearchImpl binarySearchImpl;
相关文章
- Java项目毕业设计:基于springboot+vue的电影视频网站系统「建议收藏」
- settings官方网站_java_home environment variable
- win7 java修复工具哪个好_DLL修复工具哪个好
- batch spring 重复执行_Spring Batch批处理
- Java课程设计
- java标识符与关键字_4、Java标识符和关键字
- java怎么用_如何使用Java编写程序
- XPath语法_java中path的作用
- 【说站】java方法引用是什么
- java基础教程 持续更新
- 十分钟搞定大厂java面试必问并发的基石CAS
- Java 代码审计基础知识 — java反射机制
- 整数划分问题(Java递归)
- Java的学习笔记(01)准备
- 基于Java+Spring+vue+element实现旅游信息管理平台系统
- 【愚公系列】2023年04月 Java教学课程 133-Spring框架的AOP
- Java操作MySQL数据库:实现看似不可能的梦想(java连接mysql数据库)
- Java ascii码值转为输出ascii码详解编程语言
- 数据库Java连接MySQL数据库实现数据添加(java添加mysql)
- Art of Illusion是一个用Java语言写成的基于GPL v2的开源三维建模和渲染软件,可以创建高质量的具有真实感的三维模型。它使用方便,可以对纹理和材料等效果进行编辑,可以产生具有真实感的图像。
- 为什么说Java程序员到了必须掌握 Spring Boot 的时候?
- Exploring the Power of Java with MongoDB for Enhanced Data Management(java与mongodb)
- Java Redis实例学习与应用(java redis实例)
- 让Java开发能力在Linux下得到更大发挥(java linux编程)
- Oracle与Java的结合图像处理的快速开发(java图片oracle)
- Java中字符串拼接的一些细节分析