last day (15 days later) » 

9:47 AM
Hey
 
Hi
 
thanx for your time
I want to show you some thing i think you will understand what i want
 
No problem. Seems you still have some problem with the JSONArray?
 
public int getTransactionApprovalManagementNumber() {
connection = getConnectionFromAppsPro();
String query =
"SELECT NVL(MAX(TRANSACTION_NUM),0) AS max_transaction_approval FROM " +
" " + getSchema_Name() + ".XXX_APROVAL_MANAGEMENT";
try {
ps = connection.prepareStatement(query);
rs = ps.executeQuery();

while (rs.next()) {
max_transaction = rs.getInt("max_transaction_approval");
}

} catch (Exception e) {
//("Error: ");
e.printStackTrace();
} finally {
closeResources(connection, ps, rs);
}

max_transaction++;
can you see this method
 
yes
 
9:50 AM
i do test for it
@Test
public void getTransactionApprovalManagementNumberTest() throws SQLException {
    approvalManagementDAO.getTransactionApprovalManagementNumber();
    verify(mockConn, times(1)).prepareStatement(anyString());
    verify(mockPreparedStmnt, times(1)).executeQuery();
    verify(mockResultSet, times(2)).next();
    verify(mockResultSet, times(1)).getInt("max_transaction_approval");
}
and its work fine
so i try to did the same thing for the method with jsonarray
then the error appear to me
 
post what you tried
 
i don't understand
post what please
 
what did you do to get the error with the jsonarray?
 
@Test
public void insertOrUpdateApprovalManagement() throws IOException,
JsonParseException,
JsonMappingException,
SQLException {
String body = "[test,test,test,test,test,test,test]";
String transactionType = "ADD";
JSONObject mock = Mockito.mock(JSONObject.class);
approvalManagementDAO.insertOrUpdateApprovalManagement(body,
transactionType);
verify(mockConn, times(1)).prepareStatement(anyString());

verify(mockPreparedStmnt, times(8)).setString(anyInt(),anyString());
verify(mockPreparedStmnt, times(1)).setInt(anyInt(), anyInt());
i tried this
 
the body doesn`t look like valid json to me
 
9:58 AM
yes but the error not in the json
org.mockito.exceptions.verification.WantedButNotInvoked:
Wanted but not invoked:
mockPreparedStmnt.setString(<any>, <any>);
-> at Test.com.appspro.fusionsshr.test.doaTest.ApprovalManagementDAOTest.insertOrUpdateApprovalManagement(ApprovalManagementDAOTest.java:93)

However, there were other interactions with this mock:
-> at com.appspro.fusionsshr.dao.ApprovalManagementDAO.getTransactionApprovalManagementNumber(ApprovalManagementDAO.java:59)
-> at com.appspro.db.AppsproConnection.closeResources(AppsproConnection.java:174)
in this line
verify(mockPreparedStmnt, times(8)).setString(anyInt(),anyString());
 
did you try the code I posted in my answer?
 
yes
its give me error in json
 
and it worked for you?
 
jsonarray
 
for me that doesnt make sense, because I do not create a real`JSONArray in my code. So formatting should be an issue,
 
10:02 AM
how i can pass test body to see the result
 
you need to pass a valid json format that represents the object structure you require
 
most of my purpose to do test >>> if any body change the method he should edit the unit test or the project will not work
 
thats a given :)
 
do you have a valid json format or do you need help with that?
 
10:06 AM
i need help please to see if i will faced another problem or no
 
try [{"eitCode": "x", "segmentName": "y"}]
 
@Test
public void insertOrUpdateApprovalManagement() throws Exception {

String transactionType = "ADD";

JSONObject mockJsonObject = Mockito.mock(JSONObject.class);
Mockito.when(mockJsonObject.get(Mockito.anyString())).thenReturn("");

JSONArray mockJsonArray = Mockito.mock(JSONArray.class);
Mockito.when(mockJsonArray.length()).thenReturn(1);
Mockito.when(mockJsonArray.getJSONObject(0)).thenReturn(mockJsonObject);

approvalManagementDAO.insertOrUpdateApprovalManagement(mockJsonArray, transactionType);
in this line ?
 
@Test
public void insertOrUpdateApprovalManagement() throws Exception {

String transactionType = "ADD";

approvalManagementDAO.insertOrUpdateApprovalManagement("[{\"eitCode\": \"x\", \"segmentName\": \"y\"}]", transactionType);
Mockito.verify(mockConn, Mockito.times(1)).prepareStatement(Mockito.anyString());

Mockito.verify(mockPreparedStmnt, Mockito.times(2)).setString(Mockito.anyInt(), Mockito.anyString());
Mockito.verify(mockPreparedStmnt, Mockito.times(1)).executeUpdate();

}
 
yes its work
 
in your code you used segementName instead of segmentName, so it will be just tread as null, which might no suite the matchers ... adjust the json
 
10:11 AM
please the last question, how i can handle the if condition ?
String transactionType = "ADD";
this maybe be "EDIT"
 
that question i did not really understand? just pass a different transactionType in the test ... there is no maybe ;) ... its your test
 
try {
if (transactionType.equals("ADD")) {
int transactionNum = getTransactionApprovalManagementNumber();
connection = getConnectionFromAppsPro();
for (int i = 0; i < arr.length(); i++) {
jsonObjInput = arr.getJSONObject(i);
String query =
"INSERT INTO " + " " + getSchema_Name() + ".XXX_APROVAL_MANAGEMENT(EIT_CODE,SEGMENT_NAME,TYPE,POSITION_NAME,ENABLE,HIDE,TRANSACTION_NUM,CREATED_BY,CREATED_DATE,REQUIRED)\n" +
"VALUES(?,?,?,?,?,?,?,?,sysdate,?)";
ps = connection.prepareStatement(query);
ps.setString(1, jsonObjInput.get("eitCode").toString());
you see, if ADD i will do what you give me, but i didn't check the else if
 
you should have a test for your add-brach, and a different test for your edit branch
 
another method?
you mean
 
yes
 
10:14 AM
ahaa
thank you really thank you :)
 
k ... I think thats normal unit testing design
you test a specific branch per test
 
aha
that is fine right?
 
yes ... I`ll update my answer with the json part, feel free to accept it afterwards
 
i accepted from now hhhhhhhh
thank you :)
 
are you new to unit testing?
 
10:17 AM
yes i'm
 
then you should probaly invest some more time to read up about the basic concepts. I assume there are some links to be found if you search for similiar questions.
have a nice day
 
I will, thank you
 
at any moderator that comes across here, feel free to delete this room :)
 

last day (15 days later) »