所以基本上,我试图为Java函数编写一个普通的测试用例(junit4)。 因此,我创建了一个新的groovy项目,然后在“ src”文件夹中的新包中创建了一个新的groovy类。然后,我将需要测试的java函数放入该groovy类中。此后,我创建了一个groovy测试用例。用于另一个新包中的java函数,该新包也在同一“ src”目录中创建。
常规类的代码:
先打包
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import org.json.JSONObject;
import catalog.Root;
public class FirstGroovy {
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;
String admin=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;
}
}
常规测试用例的代码:
package firsttest;
import static org.junit.Assert.*;
import first.FirstGroovy
import org.junit.Before;
import org.junit.Test;
public class FirstGroovyTest {
def FirstGroovy
@Before
void setup (){
FirstGroovy =new FirstGroovy ()
}
@Test
public void test() {
def obj1='{"userlevel":"0","userID":1,"username":"Admin","status":"success","lastlogin":"07-24-2018 04:11:45.546","ID":"1"}';
assertEquals(obj1,FirstGroovy.userLogin("admin","admin"));
assertEquals(obj1,FirstGroovy.userLogin("raj","raj"));
assertEquals(obj1,FirstGroovy.userLogin("roshan","vicky823"));
assertEquals(obj1,FirstGroovy.userLogin("xyz","xyx"));
}
}
注意:目前我只从eclipse marketplace安装了groovy / grails工具套件的groovy插件。当我运行测试用例时,出现如下未找到类的异常
找不到类firsttest.FirstGroovyTest java.lang.ClassNotFoundException:firsttest.FirstGroovyTest