JUnit test for spring and struts

Source: Internet
Author: User

/**
* Copyright 2013 the original author or authors.
*
* May 23,201 3
*/

Import java. Io. file;

Import javax. servlet. servletexception;

Import org. Apache. Struts. Action. actionservlet;
Import org. JUnit. After;
Import org. JUnit. before;
Import org. JUnit. test;
Import org. springframework. Beans. Factory. Support. beandefinitionreader;
Import org. springframework. Beans. Factory. xml. xmlbeandefinitionreader;
Import org. springframework. Context. annotation. annotationconfigutils;
Import org. springframework. Web. Context. webapplicationcontext;
Import org. springframework. Web. Context. Support. genericwebapplicationcontext;

Import servletunit. Struts. mockstrutstestcase;

Import com. ystech. Struts. Action. mybaseactionservlet;

/**
* @ Author A5
*
*/
// Inherit mockstrutstestcase -- strutstest-2.1.3.jar test struts action
Public class sendemailactiontest extends mockstrutstestcase {

@ Before
Public void setup () throws exception {

Super. Setup ();
// Load the struts configuration file
This. setcontextdirectory (new file ("webcontent "));
This. setservletconfigfile ("/WEB-INF/Web. xml ");
This. setconfigfile ("/WEB-INF/struts-config.xml ");

// Create a spring Context
Genericwebapplicationcontext webcontext = new genericwebapplicationcontext ();
Beandefinitionreader beanreader = new xmlbeandefinitionreader (webcontext );

Beanreader. loadbeandefinitions (this. choosespringconfigs ());
Annotationconfigutils. registerannotationconfigprocessors (webcontext );
Webcontext. Refresh ();
Webcontext. registershutdownhook ();

// Set spring context to webcontext
This. Context. setattribute (
Webapplicationcontext. root_web_application_context_attribute,
Webcontext );
Webcontext. setservletcontext (this. context );

}

@ After
Public void teardown () throws exception {
Super. teardown ();
}

Public void testsendemailcs203pass (string badchar) throws servletexception {

// Load the custom Servlet
Mybaseactionservlet = new mybaseactionservlet ();
Mybaseactionservlet. INIT (this. config );
This. actionservlet = (actionservlet) mybaseactionservlet;

// Test the action
This. setrequestpathinfo ("/sendemail ");

This. addrequestparameter ("article_date", "pm et 05/22/13 ");
This. addrequestparameter ("article_publisher", "Reuters ");
This. addrequestparameter ("sender", "test@fmr.com ");
This. addrequestparameter ("recipient", "test@fmr.com ");
This. addrequestparameter ("message", badchar );
// Execute action
This. actionperform ();
// Verify the test result
This. verifynoactionerrors ();
}
Public void testsendemailcs203faild (string badchar ){

This. setrequestpathinfo ("/sendemail ");

This. addrequestparameter ("article_date", "pm et 05/22/13 ");
This. addrequestparameter ("article_publisher", "Reuters ");
This. addrequestparameter ("sender", "test@fmr.com ");
This. addrequestparameter ("recipient", "test@fmr.com ");
This. addrequestparameter ("message", badchar );

This. actionperform ();

String [] errors = {"errors. cs203.knowngood "};
This. verifyactionerrors (errors );
}

@ Test
Public void testemailgoodcharacter1 () throws servletexception {
This. testsendemailcs203pass ("\ t ");
}

@ Test
Public void testemailbadcharacter1 (){
This. testsendemailcs203faild ("&");
}

/**
* {@ Inheritdoc}
*/
Public String [] choosespringconfigs (){
Return New String [] {"file: src/config/spring/propertiescontext. xml ",
"File: src/config/spring/cs203context. xml ",
"File: src/config/spring/cachecontext. xml ",
"File: src/config/spring/jcacontext. xml ",
"File: src/config/spring/taskmanagercontext. xml ",
"File: src/config/spring/servicecontext. xml ",
"File: src/config/spring/modelcontext. xml ",
"File: src/config/spring/daocontext. xml ",};
}

}

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.