惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

J
Java Code Geeks
F
Fortinet All Blogs
Martin Fowler
Martin Fowler
M
MIT News - Artificial intelligence
G
Google Developers Blog
P
Proofpoint News Feed
Recent Announcements
Recent Announcements
MyScale Blog
MyScale Blog
D
DataBreaches.Net
Stack Overflow Blog
Stack Overflow Blog
月光博客
月光博客
爱范儿
爱范儿
罗磊的独立博客
腾讯CDC
Hugging Face - Blog
Hugging Face - Blog
博客园 - 叶小钗
Vercel News
Vercel News
酷 壳 – CoolShell
酷 壳 – CoolShell
B
Blog
C
Check Point Blog
美团技术团队
宝玉的分享
宝玉的分享
Microsoft Security Blog
Microsoft Security Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻

博客园 - Liren

查找目录中同名的文件或者文件夹 获取设置一个字节某一个位的数值 基于JMS的数据交换既数据互操作平台的解决方案 Spring MVC 3中关于url-pattern设成"/"后,资源访问问题 博文阅读密码验证 - 博客园 NotificationManager - Liren - 博客园 发送短信 调用系统联系人列表 MorseCodeConverter 自用留存 - Liren - 博客园 Android 给自己的类加个事件 用代码旋转屏幕 - Liren - 博客园 Android SQLiteHelper Cassandra API60 Java 代码示例 Cassandra Java 使用TimeUUIDType 转:在 CLI 中練習 Data Model asp.net 实现一个简单CAS Server - Liren Ajax跨域访问代理类,支持GET和POST方法 Python 学习笔记:需要仔细阅读一个函数 Python 学习笔记: 备份工具
Spring MVC基于注解的Junit测试
Liren · 2011-12-06 · via 博客园 - Liren

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>