Spring MVC annotation-based JUnit Testing

Source: Internet
Author: User
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 = 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 output:

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 \ webapps \ 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. classpat?andefinition=- 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. inobject' 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.

 

Note: Add spring-test to the maven configuration file.

<Dependency>
<Groupid> org. springframework </groupid>
<Artifactid> spring-test </artifactid>
<Version >$ {org. springframework-version} </version>

</Dependency>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.