• Hank Holiday
  • NEWBIE
  • 5 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies

Scenario:

 

I have a REST service which returns the account of a portal user, and the subaccounts to that account. It works fine, but I need test code coverag, a problem likely not unfamiliar to many around these parts.

 

I know how to test REST services, well easy ones at least, but have a problem with generating required data, and relations between it in apex. My rest service uses the accoutnId field on a user, and then querries the account and its subaccounts, I would prefer to not rewrite my perfect working service because I don't know how to get test coverage, so I ask your help.

 

The accountId field on a user is not writeable, yet always filled in when creating a customer portal user based on a contact, i think this uses the Site.CreatePortal user, possible i'm wrong in this ? I tried doing it like that in code, and ran into the following error:

 

System.UnexpectedException: Method not yet implemented: createPortalUser(User, String)

 

Any advise or insights are highly appreciated !

 

@IsTest()
  	global static void testGET(){
  		
  		//add data
  		
  			//account 
	  			Account pa = new Account(name='test parent acc');
	  			insert pa;
  			
  			//subaccount
  				Account sa = new Account(name='test sub acc',ParentId =pa.id);
	  			insert sa;
  	
  			//user
  			
  				Profile p = [SELECT Id FROM Profile limit 1];
  				User u = new User(lastname='De Rycke',
  						  Alias='Samuel',
  						  email = 'zert@zer.com',
  						  username= 'zert@zer.com', 
  						  profileId= p.id,
  						  emailencodingkey='UTF-8',
  						  languagelocalekey='en_US',
  						  localesidkey='en_US',
  						  timezonesidkey='America/Los_Angeles');
  				
  				ID userid = Site.CreatePortalUser( u, pa.id );
  				
  		Test.startTest();
  		//rest service
  			RestRequest req = new RestRequest(); 
	  		req.addParameter('userid',userid);
			RestResponse res = new RestResponse();
	 
			req.requestURI = '/Account/MyAccounts/';  
			req.httpMethod = 'GET';
			RestContext.request = req;
			RestContext.response = res;
			List<Account> result = MyAccountsRestService.getMyAccounts();
	  		
  		//assert
  	
  			System.assertEquals(2, result.size());
  			System.assertEquals('test parent acc', result.get(0).name);
  			System.assertEquals('test sub acc', result.get(1).name);
  		Test.stopTest();
  	}