0

I'm having a problem since my query get empty result and i'm not sure how to solve it, How can i write a test method for it? which objects i need to create?

webService static String processReject(String conId, String info){
    ID contactId = [Select contactid from User where id =: Userinfo.getUserid()].contactId;
    ID accId  = [Select AccountID from Contact where id =: contactid].AccountId;    
    doSomthing(accId,info);//this method is just for example
}
  • 1
    Did you try something? – Reshma Apr 3 '17 at 7:04
0

Based on the above code you mentioned, you need to create a Account, Contact and user.

account acc1=new account();
acc1.Name='testAcc';
insert acc1;

Contact c1=new contact();
c1.FirstName='fnameTest';
c1.LastName='lnameTest';
c1.AccountId=acc1.id;
insert c1;

the only reason you are getting null in the query is because you might be creating an user but didn't tag the contactId to the user record( assuming you are creating a portal user and have portal user license)

Use below code to create an user and your query will return results.

Note:I'm assuming you have below profile name in your org as it's one of standard profile for a portal user (refer this link)

 Profile p = [SELECT Id FROM Profile WHERE Name='Customer Portal User'];
 User u = new User(Alias = 'testUser', Email='stdusr@test.com', 
                          EmailEncodingKey='UTF-8', LastName='Test', LanguageLocaleKey='en_US', 
                          LocaleSidKey='en_US', ProfileId = p.Id, 
                          TimeZoneSidKey='America/Los_Angeles',contactid=c1.id, Username = 'testU@a.com' + System.currentTimeMillis());
 insert u;

your final test class code will look like this

  • I dont have a profile named 'Customer Portal User', how can i create in in the test class? – Eli Apr 3 '17 at 7:35
0

The class code I'm not sure what you want to do, since your are not using the conId param(I believe this is the Id for contact). If this param is something else you can prepare that value in setup method. Anyway the test class code can be as below -

@isTest
private class CommonTestSetup {    
    private static String infoTobePassed = 'someTestInfo';
    private static User testUser;
    //Prepare test data
    @testSetup static void setup() {
        // Create common test accounts
        List<Account> testAccts = new List<Account>();
        for(Integer i=0;i<1;i++) {
            testAccts.add(new Account(Name = 'TestAcct'+i));
        }
        insert testAccts;   

        // Create common test contacts
        List<Contact> testContacts = new List<Contact>();
        for(Integer i=0;i<1;i++) {
            testContacts.add(new Contact(Name = 'TestContact'+i, AccountId = testAccts.get(i)));
        }
        insert testContacts;

        testUser = new User(
            ProfileId = [SELECT Id FROM Profile WHERE Name = '<--YOUR PROFILE-->'].Id,
            LastName = 'last',
            Email = 'test123@testuser123.com',
            Username = 'est123@testuser123.com' + System.currentTimeMillis(),
            CompanyName = 'TEST',
            Title = 'title',
            Alias = 'alias',
            TimeZoneSidKey = 'America/Los_Angeles',
            EmailEncodingKey = 'UTF-8',
            LanguageLocaleKey = 'en_US',
            LocaleSidKey = 'en_US',
            contactID = testContacts.get(0)
        );
        insert testUser;
    }

    @isTest static void processRejectTestMethod() {               
        System.runAs(testUser) {
            // Get the first test contact by using a SOQL query
            Contact con = [SELECT Id FROM Contact WHERE Name='TestContact0' LIMIT 1];           
            <--ClassName-->.processReject(con.Id, infoTobePassed);            
            //Perform some assert based on your logic
        }
    }
}

For more info on test class - https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing.htm

0

You need to create a community user in test class. You could try below test class.

@isTest
public class TestProcessReject {
    static testMethod void testMethod1(){
        Test.startTest();
        Account a = new Account(
            Name = 'Test Account'
        );
        INSERT a;

        Contact c = new Contact(
            FirstName = 'Test',
            LastName = 'Contact',
            AccountId = a.Id
        );
        INSERT c;

        Profile p = [SELECT Id FROM Profile WHERE Name = 'Customer Community Login User' LIMIT 1]; //You can query any community profile
        UserRole ur = [SELECT Id FROM UserRole WHERE Name = 'TestRole' LIMIT 1]; //Replace TestRole with a role in your org
        User u = new User();
        u.ContactId = c.Id;
        u.ProfileId = p.Id;
        u.UserRoleId = ur.Id;
        u.Username = 'testuser0@test.com';
        u.FirstName = 'Test';
        u.LastName = 'User';
        u.LastName = c.LastName;
        u.Email = 'testuser0@test.com';
        u.Alias = 'tuser';
        u.Languagelocalekey = 'en_US';
        u.Localesidkey = 'en_US';
        u.Emailencodingkey = 'UTF-8';
        u.TimezoneSidKey = 'America/Chicago';
        INSERT u;

        System.runAs(u){
            YourClass.processReject(c.Id, 'Info'); //Replace Yourclass with actual class name
            //Put Assert statements
        }
        Test.stopTest();
    }
}

In your code one issue I could found was your method does not return any value but its declared as string return type. Please ensure.

Not the answer you're looking for? Browse other questions tagged or ask your own question.