Junit test does not return data

I do some test cells in junit and after testing the methods it does not return data from the database. To do this, I inserted some data into the database. and then I tested the method. After completing the test data, you must delete it, but this does not happen. Here is my code

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

}

+4
source share
1 answer

you should use dbunit instead of a real database. Do you want unit test it

0
source

Source: https://habr.com/ru/post/1413494/


All Articles