We have two method to test, in both method we use TestNG DataProvider ( @DataProvider(name = "name-data-provider") ) to pass the parameters in the test function, because we want to run the same test with multiple values (DDT)
Login
We need two parameters to test above method: user and pass
DB
Test case:
Test loginUser with valid value
We use assertNotNull() to check method checkLogin()
Expected: checkLogin() return value that not null, that mean user is existed
Actual: as we can see in DB two user is existed
Result: We have green
Test loginUser with invalid value
We use assertNull() to check method checkLogin()
Expected: checkLogin() return value that null, that mean user is not existed
Actual: in DB we can see two user is not existed
Result:
In conclusion, the purpose of checkLogin() is to authenticate who the user is allowed to access to the system, after 2 test cases we have already tested 7 values that may be a user can enter, thus the behavior of checkLogin() method is matched with the expected
We need 5 parameters to test above method: userId, fullName, password, roleId, status
In this method, we use DataProviders with method as a Parameter
Import Required: import java.lang.reflect.Method
Test case:
Test insertUser with valid value:
We use assertEquals() to check method Insert()
Expected: Insert() return true
Actual: true
Result:
Test insertUser with invalid value:
In this test:
Insert very long userId that break data type nvarchar(50) in DB
Insert existed user into DB
We use assertEquals() to check method Insert()
Expected: Insert() return false, because userId is too long and userId is existed
Actual: false, cant insert into DB
Result:
In conclusion, Insert () aims to insert a new user into the system. In this test, we already checked 4 values, 2 valid, 2 invalids, and see the behavior of Insert() method is matched with the expected
In comparison after testing two methods:
checkLogin() method we use two Dataprovider for two tests (1)
Insert() method we use just one Dataprovider for all tests (2) and if any method is added, we just need to add two lines:
case "method" :
return statement.
=> to optimize we recommend use approach (2) instead of (1).