Spring unit testing and SQL data preparation

Source: Internet
Author: User

Tool code

During testing, the Mock of general database data is indispensable. Based on the previous article, we wrote a read script to write data to the database tool class.

 

[Java]
Import org. junit. After;
Import org. junit. Before;
Import org. junit. runner. RunWith;
Import org. slf4j. Logger;
Import org. slf4j. LoggerFactory;
Import org. springframework. beans. factory. annotation. Autowired;
Import org. springframework. jdbc. core. simple. SimpleJdbcTemplate;
Import org. springframework. test. context. ContextConfiguration;
Import org. springframework. test. context. junit4.SpringJUnit4ClassRunner;
 
Import javax. SQL. DataSource;
Import java. io. BufferedReader;
Import java. io. IOException;
Import java. io. InputStream;
Import java. io. InputStreamReader;
Import java. text. SimpleDateFormat;
Import java. util. Date;
Import java. util. HashMap;
 
@ RunWith (SpringJUnit4ClassRunner. class)
@ ContextConfiguration (locations = {"classpath: testApplicationContext. xml "})
Public class SqlDataPrepareUtil {
@ Autowired
DataSource;
SimpleJdbcTemplate template;
 
Private final Logger logger = LoggerFactory. getLogger (SqlDataPrepareUtil. class );
 
@ Before
Public void setUp () throws IOException {
 
Template = new SimpleJdbcTemplate (dataSource );
 
 
String content = sqlForThisTest ();
If (content. equals ("")){
Return;
}
 
String [] sqlLines = content. split (";");
For (int I = 0; I <sqlLines. length; I ++ ){
String SQL = sqlLines [I];
 
If (0 = SQL. trim (). length ()){
Continue;
}
Template. update (SQL );
If (logger. isDebugEnabled ()){
Logger. debug (SQL );
}
}
}
 
Private String sqlForThisTest () throws IOException {
String sqlName = getClass (). getSimpleName () + ". SQL ";
InputStream stream = getClass (). getResourceAsStream (sqlName );
If (stream = null ){
Return "";
}
BufferedReader reader = new BufferedReader (
New InputStreamReader (stream, "UTF-8 "));
StringBuilder buffer = new StringBuilder ();
Try {
String line = null;
While (line = reader. readLine ())! = Null ){
Buffer. append (line );
}
} Finally {
Reader. close ();
}
Return buffer. toString ();
}
 
}
// Www.heatpress123.net
 

Tool usage

The path where the script is stored is the same as that of the test class package. The test class is written as follows:

 

[Java]
Import org. jboss. resteasy. mock. MockHttpRequest;
Import org. jboss. resteasy. mock. MockHttpResponse;
Import org. junit. Assert;
Import org. junit. Ignore;
Import org. junit. Test;
Import org. junit. runner. RunWith;
Import org. springframework. beans. factory. annotation. Autowired;
Import org. springframework. stereotype. Component;
Import org. springframework. test. annotation. Rollback;
Import org. springframework. test. context. ContextConfiguration;
Import org. springframework. test. context. junit4.SpringJUnit4ClassRunner;
Import org. springframework. transaction. annotation. Transactional;
 
Import javax. ws. rs. core. MediaType;
Import javax. ws. rs. core. Response. Status;
Import java. io. UnsupportedEncodingException;
Import java.net. URISyntaxException;
Import java. util. ArrayList;
Import java. util. List;
 
Import static com. natpryce. makeiteasy. MakeItEasy .*;
 
@ Component
Public class LineSyncTest extends SqlDataPrepareUtil {
 
 
@ Test
@ Rollback (true)
Public void should_return_XXX_when_given_xx (){
 
}
 
}

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.