Reputation: 11
I have a function called userLogin()
that takes the input (username and password) of a login page as parameters and returns the string representation of a Json object that contains the details of the user who is trying to login.
My Function:
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;
}
Currently, I have installed the groovy-eclipse plugin from the Groovy/Grails Tool suite.I have watched some Youtube video's and read some tutorials on how to write groovy test cases but don't know how to implement it for my function.Can someone help me as I am new to using this tool .
Upvotes: 1
Views: 757
Reputation: 1179
You'll probably want to move the DB connection to a constructor, property or factory method so it can be supplied via the tests.
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
}
}
With that, you could do something like this:
@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
}
}
Upvotes: 0