1

我有一个名为的函数userLogin(),它将登录页面的输入(用户名和密码)作为参数,并返回包含尝试登录的用户详细信息的 Json 对象的字符串表示形式。

我的功能:

public String userLogin(String strUser, String strPswrd) throws Exception

{

    String strResult = null;
    String strUserlevel = null;
    String strDate = null;
    String strId = null;
    int nUserID = 0;
    int nParam = 0;
    StringBuilder sbSql = null;
    JSONObject oJson = null;

    ResultSet oRs = null;
    Root oRoot = null;
    PreparedStatement oPrStmt = null;

    try{
        oRoot = Root.createDbConnection(null);
        oJson = new JSONObject();

        sbSql = new StringBuilder("SELECT * FROM members WHERE username =? AND Password = ? AND deleteflag =0 ");
        oPrStmt = oRoot.con.prepareStatement(sbSql.toString());
        nParam =0;
        oPrStmt.setString(++nParam, strUser);
        oPrStmt.setString(++nParam, strPswrd);
        oRs =oPrStmt.executeQuery();
        if(oRs.next()){
            nUserID = oRs.getInt("userlevel");

            strUserlevel = oRs.getString("users");
            strDate = oRs.getString("lastlogin");
            strId = oRs.getString("id");
            strUser = strUser.substring(0, 1).toUpperCase() + strUser.substring(1);
            oJson.put("status", "success");
            oJson.put("ID", strId);
            oJson.put("userID", nUserID);
            oJson.put("userlevel", strUserlevel);
            oJson.put("lastlogin", strDate);
            oJson.put("username", strUser);
        }
        strResult = oJson.toString();

    }
    catch(Exception ex){
        ex.printStackTrace();
    }
    return strResult;

}

目前,我已经从 Groovy/Grails Tool 套件中安装了 groovy-eclipse 插件。我看过一些 Youtube 视频并阅读了一些关于如何编写 groovy 测试用例的教程,但不知道如何为我的函数实现它。有人可以帮助我,因为我是使用这个工具的新手。

4

1 回答 1

0

您可能希望将 DB 连接移动到构造函数、属性或工厂方法,以便可以通过测试提供它。

class YourClass {
    PreparedStatement newPreparedStatement(String sql) {
        Root.createDbConnection(null).con.prepareStatement(sql)
    }
    String userLogin(String strUser, String strPswrd) throws Exception {
        def oPrStmt = newPreparedStatement("SELECT * FROM members WHERE username =? AND Password = ? AND deleteflag =0 ")
        // all the rest of your code from "nParam = 0" on
    }
}

有了这个,你可以做这样的事情:

@Grab('org.mockito:mockito-all:1.10.19')
import static org.mockito.Matchers.*
import static org.mockito.Mockito.*

import org.junit.Rule
import org.junit.Test
import org.mockito.Mock
import org.mockito.Spy
import org.mockito.junit.MockitoJUnit
import org.mockito.junit.MockitoRule

final class UserLoginTests {
    @Rule
    public final MockitoRule mockitoRule = MockitoJUnit.rule()
    @Mock
    private PreparedStatement statement
    @Mock
    private ResultSet results

    @InjectMocks @Spy
    private YourClass yours

    @Test
    void testUserLogin() {
        when(yours.newPreparedStatement(anyString())).thenReturn(statement)
        when(statement.executeQuery()).thenReturn(results)
        when(results.next()).thenReturn(...)

        String json = yours.userLogin('user', 'pass')
        assert json == 'literal json string'
      //or
        def map = new JsonSlurper().parseText(json)
        assert map.containsKey('something')
        // etc.
      //or
        // look into org.skyscreamer.JsonAssert
    }
}
于 2018-09-10T14:40:17.740 回答