This is my first attempt at Apex. I'm calling Apex code from a Custom Button. I've successfully run the code (largely inspired by Andrew Fawcett and the Salesforce Apex Approval Process Example)
but now I'm writing the unit test. When I run it, it's failing with error:
"System.QueryException: List has no rows for assignment to SObject"
Here's the class:
public with sharing class DetailButtonController
{
private ApexPages.StandardController standardController;
public DetailButtonController(ApexPages.StandardController standardController)
{
this.standardController = standardController;
}
public PageReference doSomething()
{
// Apex code for handling record from a Detail page goes here
Id recordId = standardController.getId();
Opportunity record = (Opportunity) standardController.getRecord();
// My code
User user1 = [SELECT Id FROM User WHERE Alias='mhall'];
// Create an approval request for the opportunity
Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
req1.setComments('Submitting request for approval.');
req1.setObjectId(recordId);
// Submit on behalf of a specific submitter
req1.setSubmitterId(user1.id);
// Submit the record to specific process and skip the criteria evaluation
req1.setProcessDefinitionNameOrId('Release_to_Partner');
req1.setSkipEntryCriteria(true);
req1.setNextApproverIds(new Id[] {user1.id});
// Submit the approval request for the opportunity
Approval.ProcessResult result = Approval.process(req1);
// End my code
return null;
}
}
And Here is the Unit Test
@istest
public class DetailButtonControllerTest
{
@istest
public static void TestDetailButtonController(){
Opportunity MyTestOppty = [SELECT id From Opportunity LIMIT 1];
Test.StartTest(); //indicate that the test is about to start
PageReference myVfPage = Page.opptopartnerbuttonpage;
Test.setCurrentPage(myVfPage);
ApexPAges.StandardController sc = new ApexPages.StandardController(MyTestOppty);
DetailButtonController bcPartnerRel = new DetailButtonController(sc); //instance of the class we’re testing
bcPartnerRel.doSomething(); //call actual function to perform the test
Test.StopTest(); // end the test
}
}
Is there anything obvious? I'm trying to set trace flags or view a debug log, but haven't figured those out yet. Any help is appreciated.