首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Junit test不还原数据

Junit test不还原数据
EN

Stack Overflow用户
提问于 2012-05-21 16:32:14
回答 1查看 374关注 0票数 4

我在junit中做了一些测试用例,在测试了这些方法之后,它没有从数据库恢复数据。为此,我在数据库中插入了一些数据。然后我测试完method.so后,测试数据必须被删除,但这并没有发生。以下是我的代码

代码语言:javascript
复制
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();
    }
}

}

EN

回答 1

Stack Overflow用户

发布于 2012-11-07 00:23:17

您应该使用dbunit而不是真正的数据库。您想要对其进行单元测试

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10681849

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档