我在junit中制作了一些测试用例,在测试方法后,它没有从数据库中恢复数据。 为此,我已将一些数据插入数据库。然后我测试了方法。所以在完成测试后必须删除数据,但是没有发生。 这是我的代码
public class TestAdminMethodsWebService extends AbstractTransactionalDataSourceSpringContextTests {
protected WebServiceTemplate admin;
Connection connection = null;
public TestAdminMethodsWebService(){
try {
Class.forName("org.postgresql.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
protected String[] getConfigLocations() {
return new String[] { "applicationContext-test.xml" };
}
public WebServiceTemplate getAdmin() {
return admin;
}
public void setAdmin(WebServiceTemplate admin) {
this.admin = admin;
}
@Override
protected void onSetUp() throws Exception{
String query1 = "INSERT INTO roles( id,is_active,name) VALUES ('1',true,'admin')";
String query2 = "INSERT INTO customers(id, is_active, name, lxg_username, lxg_password, lxg_classid) VALUES ('1', true,'cust1','naomi@mkinetic.com','5966nM','INFO1' )";
String query3 = "INSERT INTO users(id, is_active) VALUES ('1', true)";
String query4 = "INSERT INTO customers(id, is_active) VALUES ('1',true)";
String query5 = "INSERT INTO users( id, is_active,email, password, lxg_username, lxg_password, lxg_classid, role_id, customer_id) VALUES ('1',true,'gfe@gfe.com','gfe','naomi@mkinetic.com','5966nM','INFO1','1','1')";
String query6 = "INSERT INTO roles( id,is_active,name) VALUES ('2',true,'User') ";
try {
connection = DriverManager.getConnection("jdbc:postgresql://localhost/g","g", "root");
Statement st = connection.createStatement();
st.execute(query1);
st.execute(query2);
st.execute(query3);
st.execute(query4);
st.execute(query5);
st.execute(query6);
} catch (Exception e) {
e.printStackTrace();
}
finally{
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
@Override
protected void onTearDown() throws Exception{
String query1 = "delete from users where id='1'";
String query2 = "delete from roles where id='1'";
String query3 = "delete from customers where id='1'";
String query4 = "delete from users where id='1'";
String query5 = "delete from customers where id='1'";
String query6 = "delete from roles where id ='2'";
try {
connection = DriverManager.getConnection("jdbc:postgresql://localhost/g","g", "root");
Statement st = connection.createStatement();
st.execute(query1);
st.execute(query2);
st.execute(query3);
st.execute(query4);
st.execute(query5);
st.execute(query6);
} catch (Exception e) {
e.printStackTrace();
}
finally{
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
@SuppressWarnings("unckecked")
@Test
public void test_AddCompany(){
try{
AddCompany addCompany = new ObjectFactory().createAddCompany();
com.gfe.services.soap.admin.AddCompany.AdminCredentials adminCredentials = new com.gfe.services.soap.admin.AddCompany.AdminCredentials();
adminCredentials.setPassword("gfe");
adminCredentials.setUsername("gfe@gfe.com");
com.gfe.services.soap.admin.AddCompany.CompanyInfo companyInfo = new com.gfe.services.soap.admin.AddCompany.CompanyInfo();
companyInfo.setCompanyKey("company1");
companyInfo.setLxgPassword("5966nM");
companyInfo.setLxgUsername("naomi@mkinetic.com");
companyInfo.setName("gfe1");
companyInfo.setUrl("http://www.google.com");
addCompany.setAdminCredentials(adminCredentials);
addCompany.setCompanyInfo(companyInfo);
JAXBElement<AddCompanyResponse> xmlResponse = (JAXBElement<AddCompanyResponse>) admin.marshalSendAndReceive(addCompany);
AddCompanyResponse response = xmlResponse.getValue();
CompanyResponse companyResponse =response.getReturn();
BaseErrorResponseBean baseErrorResponseBean = companyResponse.getError();
if (baseErrorResponseBean!= null) {
String errorCdoe = baseErrorResponseBean.getErrorCode();
String errorMessage = baseErrorResponseBean.getErrorMessage();
System.out.println("Error code = " +errorCdoe);
System.out.println("Error Message = " + errorMessage);
}
String transactionId = companyResponse.getTransactionId();
Boolean success = companyResponse.isSuccess();
if (success) {
System.out.println("The test addCompany runned successfully");
}
else{
System.out.println("Some error occoured in test addCompany ");
}
}
catch(Exception e){
System.out.println("ERROR IN TRY BLOCK");
e.printStackTrace();
}
}
}
答案 0 :(得分:0)
您应该使用dbunit而不是真正的数据库。你想进行单元测试