Spring MVC基于注解的Junit测试

package com.liren.javadb2;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;

import java.sql.Connection;

import javax.sql.DataSource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations={
        "file:src/main/webapp/WEB-INF/spring/*.xml", 
        "file:src/main/webapp/WEB-INF/spring/appServlet/*.xml"})
public class One {
    private static final Logger logger = LoggerFactory.getLogger(One.class);
    
    @Autowired
    DataSource mainDataSource;
    
    @Test
    public void test() {
        try
        {
            Connection connection = mainDataSource.getConnection();
            assertNotNull(connection);
        }catch(Exception e){
            fail(e.toString());
        }
    }
}

Console输出:

INFO : org.springframework.test.context.TestContextManager - @TestExecutionListeners is not present for class [class com.liren.javadb2.One]: using defaults.

INFO : org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from file [E:\WorkSpaces\javaweb\javadb2\src\main\webapp\WEB-INF\spring\dbconfig.xml]
INFO : org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from file [E:\WorkSpaces\javaweb\javadb2\src\main\webapp\WEB-INF\spring\root-context.xml]
INFO : org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from file [E:\WorkSpaces\javaweb\javadb2\src\main\webapp\WEB-INF\spring\dbconfig.xml]
INFO : org.springframework.beans.factory.support.DefaultListableBeanFactory - Overriding bean definition for bean 'mainDataSource': replacing [Generic bean: class [com.jolbox.bonecp.BoneCPDataSource]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=close; defined in file [E:\WorkSpaces\javaweb\javadb2\src\main\webapp\WEB-INF\spring\dbconfig.xml]] with [Generic bean: class [com.jolbox.bonecp.BoneCPDataSource]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=close; defined in file [E:\WorkSpaces\javaweb\javadb2\src\main\webapp\WEB-INF\spring\dbconfig.xml]]
INFO : org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from file [E:\WorkSpaces\javaweb\javadb2\src\main\webapp\WEB-INF\spring\appServlet\servlet-context.xml]
INFO : org.springframework.context.annotation.ClassPathBeanDefinitionScanner - JSR-330 'javax.inject.Named' annotation found and supported for component scanning
INFO : org.springframework.context.support.GenericApplicationContext - Refreshing org.springframework.context.support.GenericApplicationContext@1431028: startup date [Tue Dec 06 09:38:45 CST 2011]; root of context hierarchy
INFO : org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor - JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
INFO : org.springframework.beans.factory.support.DefaultListableBeanFactory - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@14a0b74: defining beans [mainDataSource,org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping#0,org.springframework.format.support.FormattingConversionServiceFactoryBean#0,org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter#0,org.springframework.web.servlet.handler.MappedInterceptor#0,org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter,org.springframework.web.servlet.resource.ResourceHttpRequestHandler#0,org.springframework.web.servlet.handler.SimpleUrlHandlerMapping#0,org.springframework.web.servlet.view.InternalResourceViewResolver#0,DB,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor]; root of factory hierarchy
INFO : org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - Mapped URL path [/resources/**] onto handler 'org.springframework.web.servlet.resource.ResourceHttpRequestHandler#0'
INFO : org.springframework.context.support.GenericApplicationContext - Closing org.springframework.context.support.GenericApplicationContext@1431028: startup date [Tue Dec 06 09:38:45 CST 2011]; root of context hierarchy
INFO : org.springframework.beans.factory.support.DefaultListableBeanFactory - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@14a0b74: defining beans [mainDataSource,org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping#0,org.springframework.format.support.FormattingConversionServiceFactoryBean#0,org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter#0,org.springframework.web.servlet.handler.MappedInterceptor#0,org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter,org.springframework.web.servlet.resource.ResourceHttpRequestHandler#0,org.springframework.web.servlet.handler.SimpleUrlHandlerMapping#0,org.springframework.web.servlet.view.InternalResourceViewResolver#0,DB,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor]; root of factory hierarchy
INFO : com.jolbox.bonecp.BoneCP - Shutting down connection pool...
INFO : com.jolbox.bonecp.BoneCP - Connection pool has been shutdown.

注意MAVEN配置文件加上spring-test

<dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>${org.springframework-version}</version></dependency>

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏everhad

转载:Package by feature, not layer

The first question in building an application is "How do I divide it up into pac...

14700
来自专栏技术小黑屋

Build Android Packages From Command Line

A few months ago,I dealed with a task:To build a large amount of apk files. The...

13430
来自专栏ml

HDUOJ--Bone Collector

Bone Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/327...

37090
来自专栏杂烩

Eclipse下Hadoop的MapReduce开发之mapreduce打包

点击next,使用默认选择,再点击next,在最下面的Main class处选择项目里的MapReduceTest

22530
来自专栏Kubernetes

kube-controller-manager最佳配置

kubernetes version: 1.6.2 Kube-controller-manager Configuration kube-controller-...

54690
来自专栏李成熙heyli

webpack Performance: The Comprehensive Guide

Introduction For beginners, webpack building performance is sometimes annoying. ...

23870
来自专栏全栈架构

Spring Boot 与 Kotlin 上传文件

如果我们做一个小型的web站,而且刚好选择的kotlin 和Spring Boot技术栈,那么上传文件的必不可少了,当然,如果你做一个中大型的web站,那建议你...

13130
来自专栏运维

linux文件树

以前有意找这方面的资料,今天突然发现在系统中就有 linux系统用man hier solaris用man  filesystem 其结果如下     ...

13620
来自专栏FD的专栏

Effective Testing with RSpec 3 (英文版)(序言)

Early praise for Effective Testing with RSpec 3

22540
来自专栏Aloys的开发之路

使用CXF+Spring发布WebService,启动报错

使用CXF+Spring发布WebService,启动报错,日志如下: 五月 12, 2017 9:01:37 下午 org.apache.tomcat.uti...

738100

扫码关注云+社区

领取腾讯云代金券