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 (){
}
}