4 Stimmen

Junit-Test kehrt Daten nicht um

Ich habe einige Testfälle in Junit machen und nach dem Testen der Methoden ist es nicht zurückkehren Daten aus der Datenbank. Dafür habe ich einige Daten in die Datenbank eingefügt. und dann testete ich die method.so nach Beendigung der Testdaten gelöscht werden muss, aber es ist nicht geschehen. Hier ist mein 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();
    }
}

}

0voto

Sollten Sie dbunit anstelle einer echten Datenbank verwenden. Sie möchten Unit-Tests durchführen

CodeJaeger.com

CodeJaeger ist eine Gemeinschaft für Programmierer, die täglich Hilfe erhalten..
Wir haben viele Inhalte, und Sie können auch Ihre eigenen Fragen stellen oder die Fragen anderer Leute lösen.

Powered by:

X