zoukankan      html  css  js  c++  java
  • 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>  

            

  • 相关阅读:
    让一个 csproj 项目指定多个开发框架
    推荐近乎免费的调试神器——OzCode
    再也不用克隆多个仓库啦!git worktree 一个 git 仓库可以连接多个工作目录
    .NET Core 和 .NET Framework 中的 MEF2
    将 WPF、UWP 以及其他各种类型的旧样式的 csproj 文件迁移成新样式的 csproj 文件
    .NET 中的轻量级线程安全
    卡诺模型(KANO Model)
    C#/.NET 匿名函数会捕获变量,并延长对象的生命周期
    迫不及待地体验了一把 C#8.0 中的可空引用类型(Nullable Reference)
    异步任务中的重新进入(Reentrancy)
  • 原文地址:https://www.cnblogs.com/mobile/p/2277534.html
Copyright © 2011-2022 走看看