spring学习12-使用junit4进行单元测试

简介: spring学习12-使用junit4进行单元测试

image.png

pom.xml
    <?xml version="1.0" encoding="UTF-8"?>
     <project xmlns="http://maven.apache.org/POM/4.0.0"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
         <modelVersion>4.0.0</modelVersion>
         <groupId>com.geyao</groupId>
         <artifactId>spring01geyao</artifactId>
         <version>1.0-SNAPSHOT</version>
         <dependencies>
             <dependency>
                 <groupId>org.springframework</groupId>
                 <artifactId>spring-context</artifactId>
                 <version>4.3.13.RELEASE</version>
             </dependency>
             <dependency>
                 <groupId>org.springframework</groupId>
                 <artifactId>spring-test</artifactId>
                 <version>4.3.13.RELEASE</version>
             </dependency>
             <dependency>
                 <groupId>log4j</groupId>
                 <artifactId>log4j</artifactId>
                 <version>1.2.17</version>
             </dependency>
             <dependency>
                 <groupId>junit</groupId>
                 <artifactId>junit</artifactId>
                 <version>4.12</version>
             </dependency>
         </dependencies>
     </project>CompactDisc类
    package soundSystem;
     import org.springframework.stereotype.Component;
     @Component
     public class CompactDisc {
         public CompactDisc() {
             super();
             System.out.println("compactdisc无参构造方法");
         }
         public void play(){
             System.out.println("正在播放音乐....");
         }
     }CDplay类
    package soundSystem;
     import org.springframework.beans.factory.annotation.Autowired;
     import org.springframework.stereotype.Component;
     @Component
     public class CDPlayer {
         private CompactDisc cd;
         public CDPlayer() {
             super();
             System.out.println("CDPlayer无参构造方法");
         }
     @Autowired
         public CDPlayer(CompactDisc cd) {
             this.cd = cd;
             System.out.println("CDPlayer有参构造方法");
         }
         public void play(){
             cd.play();
         }
     }appconfig类
    package soundSystem;
     import org.springframework.context.annotation.ComponentScan;
     //spring注解类
     @ComponentScan
     public class Appconfig {
     }apptest类
    package soundSystem;
     import org.junit.Test;
     import org.junit.runner.RunWith;
     import org.springframework.context.ApplicationContext;
     import org.springframework.context.annotation.AnnotationConfigApplicationContext;
     //@RunWith(SpringJunit4ClassRunner.class)
     //@ContextConfiguration(class=)
     public class AppTest {
         @Test
         public void testPlay(){
             ApplicationContext context=new AnnotationConfigApplicationContext(Appconfig.class);
             CDPlayer player=context.getBean(CDPlayer.class);
             player.play();
         }
     }运行结果
    [INFO ] 2019-10-29 21:19:35,531 method:org.springframework.context.support.AbstractApplicationContext.prepareRefresh(AbstractApplicationContext.java:583)
     Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@32709393: startup date [Tue Oct 29 21:19:35 CST 2019]; root of context hierarchy
     [DEBUG] 2019-10-29 21:19:35,565 method:org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:221)
     Creating shared instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
     [DEBUG] 2019-10-29 21:19:35,566 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:449)
     Creating instance of bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
     [DEBUG] 2019-10-29 21:19:35,602 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
     Eagerly caching bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor' to allow for resolving potential circular references
     [DEBUG] 2019-10-29 21:19:35,609 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:485)
     Finished creating instance of bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
     [DEBUG] 2019-10-29 21:19:35,717 method:org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:221)
     Creating shared instance of singleton bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
     [DEBUG] 2019-10-29 21:19:35,718 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:449)
     Creating instance of bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
     [DEBUG] 2019-10-29 21:19:35,720 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
     Eagerly caching bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor' to allow for resolving potential circular references
     [DEBUG] 2019-10-29 21:19:35,772 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:485)
     Finished creating instance of bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
     [DEBUG] 2019-10-29 21:19:35,773 method:org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:221)
     Creating shared instance of singleton bean 'org.springframework.context.annotation.internalRequiredAnnotationProcessor'
     [DEBUG] 2019-10-29 21:19:35,774 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:449)
     Creating instance of bean 'org.springframework.context.annotation.internalRequiredAnnotationProcessor'
     [DEBUG] 2019-10-29 21:19:35,775 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
     Eagerly caching bean 'org.springframework.context.annotation.internalRequiredAnnotationProcessor' to allow for resolving potential circular references
     [DEBUG] 2019-10-29 21:19:35,792 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:485)
     Finished creating instance of bean 'org.springframework.context.annotation.internalRequiredAnnotationProcessor'
     [DEBUG] 2019-10-29 21:19:35,806 method:org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:730)
     Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@124c278f: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.event.internalEventListenerProcessor,org.springframework.context.event.internalEventListenerFactory,appconfig,CDPlayer,compactDisc]; root of factory hierarchy
     [DEBUG] 2019-10-29 21:19:35,806 method:org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:251)
     Returning cached instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
     [DEBUG] 2019-10-29 21:19:35,807 method:org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:251)
     Returning cached instance of singleton bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
     [DEBUG] 2019-10-29 21:19:35,810 method:org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:251)
     Returning cached instance of singleton bean 'org.springframework.context.annotation.internalRequiredAnnotationProcessor'
     [DEBUG] 2019-10-29 21:19:35,813 method:org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:221)
     Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerProcessor'
     [DEBUG] 2019-10-29 21:19:35,815 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:449)
     Creating instance of bean 'org.springframework.context.event.internalEventListenerProcessor'
     [DEBUG] 2019-10-29 21:19:35,825 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
     Eagerly caching bean 'org.springframework.context.event.internalEventListenerProcessor' to allow for resolving potential circular references
     [DEBUG] 2019-10-29 21:19:35,834 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:485)
     Finished creating instance of bean 'org.springframework.context.event.internalEventListenerProcessor'
     [DEBUG] 2019-10-29 21:19:35,835 method:org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:221)
     Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerFactory'
     [DEBUG] 2019-10-29 21:19:35,836 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:449)
     Creating instance of bean 'org.springframework.context.event.internalEventListenerFactory'
     [DEBUG] 2019-10-29 21:19:35,836 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
     Eagerly caching bean 'org.springframework.context.event.internalEventListenerFactory' to allow for resolving potential circular references
     [DEBUG] 2019-10-29 21:19:35,844 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:485)
     Finished creating instance of bean 'org.springframework.context.event.internalEventListenerFactory'
     [DEBUG] 2019-10-29 21:19:35,846 method:org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:221)
     Creating shared instance of singleton bean 'appconfig'
     [DEBUG] 2019-10-29 21:19:35,846 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:449)
     Creating instance of bean 'appconfig'
     [DEBUG] 2019-10-29 21:19:35,848 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
     Eagerly caching bean 'appconfig' to allow for resolving potential circular references
     [DEBUG] 2019-10-29 21:19:35,855 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:485)
     Finished creating instance of bean 'appconfig'
     [DEBUG] 2019-10-29 21:19:35,856 method:org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:221)
     Creating shared instance of singleton bean 'CDPlayer'
     [DEBUG] 2019-10-29 21:19:35,857 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:449)
     Creating instance of bean 'CDPlayer'
     [DEBUG] 2019-10-29 21:19:35,876 method:org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:221)
     Creating shared instance of singleton bean 'compactDisc'
     [DEBUG] 2019-10-29 21:19:35,877 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:449)
     Creating instance of bean 'compactDisc'
     compactdisc无参构造方法
     [DEBUG] 2019-10-29 21:19:35,878 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
     Eagerly caching bean 'compactDisc' to allow for resolving potential circular references
     [DEBUG] 2019-10-29 21:19:35,885 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:485)
     Finished creating instance of bean 'compactDisc'
     [DEBUG] 2019-10-29 21:19:35,886 method:org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:757)
     Autowiring by type from bean name 'CDPlayer' via constructor to bean named 'compactDisc'
     CDPlayer有参构造方法
     [DEBUG] 2019-10-29 21:19:35,889 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
     Eagerly caching bean 'CDPlayer' to allow for resolving potential circular references
     [DEBUG] 2019-10-29 21:19:35,891 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:485)
     Finished creating instance of bean 'CDPlayer'
     [DEBUG] 2019-10-29 21:19:35,891 method:org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:251)
     Returning cached instance of singleton bean 'compactDisc'
     [DEBUG] 2019-10-29 21:19:35,892 method:org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:251)
     Returning cached instance of singleton bean 'org.springframework.context.event.internalEventListenerFactory'
     [DEBUG] 2019-10-29 21:19:35,970 method:org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:251)
     Returning cached instance of singleton bean 'lifecycleProcessor'
     [DEBUG] 2019-10-29 21:19:35,980 method:org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:251)
     Returning cached instance of singleton bean 'CDPlayer'
     正在播放音乐....
     Process finished with exit code 0
相关文章
|
1天前
|
移动开发 JavaScript 前端开发
学习vue3使用在线官方开发环境play.vuejs.org进行测试
学习vue3使用在线官方开发环境play.vuejs.org进行测试
|
2天前
|
Java 测试技术 API
Spring Boot 单元测试 0基础教程
Spring Boot 单元测试 0基础教程
8 0
|
4天前
|
测试技术
测试基础 Junit单元测试框架
测试基础 Junit单元测试框架
11 2
测试基础 Junit单元测试框架
|
4天前
|
运维 安全 测试技术
测试基础 学习测试你必须要知道的基础知识
测试基础 学习测试你必须要知道的基础知识
9 3
|
4天前
|
存储 前端开发 Java
Spring Boot自动装配的源码学习
【4月更文挑战第8天】Spring Boot自动装配是其核心机制之一,其设计目标是在应用程序启动时,自动配置所需的各种组件,使得应用程序的开发和部署变得更加简单和高效。下面是关于Spring Boot自动装配的源码学习知识点及实战。
13 1
|
12天前
|
Java 测试技术 Maven
Spring Boot单元测试报错java.lang.IllegalStateException: Could not load TestContextBootstrapper [null]
Spring Boot单元测试报错java.lang.IllegalStateException: Could not load TestContextBootstrapper [null]
|
13天前
|
XML Java 数据格式
Spring学习__一篇足矣
Spring学习__一篇足矣
Spring学习__一篇足矣
|
16天前
|
安全 Java 测试技术
Spring Boot 自动化单元测试类的编写过程
企业开发不仅要保障业务层与数据层的功能安全有效,也要保障表现层的功能正常。但是我们一般对表现层的测试都是通过postman手工测试的,并没有在打包过程中代码体现表现层功能被测试通过。那么能否在测试用例中对表现层进行功能测试呢?答案是可以的,我们可以使用MockMvc来实现它。
47 0
|
17天前
|
Java 数据安全/隐私保护 Sentinel
微服务学习 | Spring Cloud 中使用 Sentinel 实现服务限流
微服务学习 | Spring Cloud 中使用 Sentinel 实现服务限流
|
18天前
|
Java 测试技术 开发者
[AIGC] 使用Spring Boot进行单元测试:一份指南
[AIGC] 使用Spring Boot进行单元测试:一份指南

热门文章

最新文章