You need to sign in to do that
Don't have an account?
writing test methods
I can't for the life of me grasp the concept of test methods.
I have a very basic class that deactivates specific users but i don't know how to write a test method for it.
global class deactivateAccount {
webservice static String deactivateUserAccount(){
String myResult; // define the result that will be returned
Integer countUsers = 0; // count the updated users
List<User> myUsers = [
SELECT u.Profile.Name, u.isActive
FROM User u
WHERE u.Profile.Name = 'test' and u.IsActive = TRUE
];
for (User aUser: myUsers){
aUser.IsActive = FALSE;
countUsers++;
}
update myUsers;
return 'done: ' + countUsers;
}
}
How do I test this? Can someone EXPLAIN what i'd be testing.
Thank you!
it is my idea here, but i did not run it and i like to konw the result from your thanks:
public static testMethod void testdeactivateUserAccount()
{
deactivateUserAccount();
Profile p = [select id from profile where name='test' limit 1];
User testuser = new User(profileid = p.Id, IsActive = TRUE, alias = 'test', email='test@hotmail.com',
lastname='Testing', country='New Zealand', username='test@hotmail.com');
insert testuser;
String mes = deactivateUserAccount();
System.assertEquals(FALSE,[select u.ISACTIVE from User u WHERE Id=:testuser.Id].IsACTIVE);
SYstem.assertEquals('done: 1',mes);
}
All Answers
it is my idea here, but i did not run it and i like to konw the result from your thanks:
public static testMethod void testdeactivateUserAccount()
{
deactivateUserAccount();
Profile p = [select id from profile where name='test' limit 1];
User testuser = new User(profileid = p.Id, IsActive = TRUE, alias = 'test', email='test@hotmail.com',
lastname='Testing', country='New Zealand', username='test@hotmail.com');
insert testuser;
String mes = deactivateUserAccount();
System.assertEquals(FALSE,[select u.ISACTIVE from User u WHERE Id=:testuser.Id].IsACTIVE);
SYstem.assertEquals('done: 1',mes);
}
it all checked to 100% ... I had to change the profile name from test to the actual profile name I have in my org and then it jumped to 100% coverage. And I think I get the idea of testing as well:
1. prepare dummy data
2a. if you test a trigger then do the action that would trip the trigger.
2b. if you test a method/function then call that function
3. write system.assert to write to the log file
4? check results returned by function/trigger
My mistake, I should remind you to get profile name from your sandbox. I doubted about the 77%coverage,
:smileyvery-happy: 100% is acceptable one. few suggestions :
1 100% is not necessary.
2 you do not have to hard code the profile name if your konw how to use SystemRunAs(user) .(teststart(), teststop()...)
3 focus on user cases not coding lines coverage.
Hello,
Could you make a suggestion on how to test the following:
Dear NZ_Xue,
I have very similar class structure and web service, but instead of SOQL, it has a SOSL query.
This web service is called from a custom button on VF page, everyhting works good, but when I call the web service from a test class, SOSL retuns nothing .. any reason as to why this may be happnening?