function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
adrisseladrissel 

Test class just not working!

I have a VF page I created for users to quickly create common Cases.  It has a controller for the actual Case-creation portion, et al.  I am trying to write a test class to get code coverage and for some reason it is just failing over and over.  No matter what modifications I have attempted I get the exact same error.  Here are the relevant code snippets with my question following:

VF Page:
<apex:page standardcontroller="Case" extensions="SIMSQuickCreate">

    <script type="text/javascript">

        function createCase(cancel) {

            var supplier = document.getElementById('{!$Component.form.block.section1.supplierInput}').value;
            var acc = document.getElementById('{!$Component.form.block.section1.accInput}').value;
            var conPerson = document.getElementById('{!$Component.form.block.section1.conPersonInput}').value;
            var con = document.getElementById('{!$Component.form.block.section1.conInput}').value;
            var conPersonPhone = document.getElementById('{!$Component.form.block.section1.conPersonPhoneInput}').value;
            var conPersonEmail = document.getElementById('{!$Component.form.block.section1.conPersonEmailInput}').value;
            var sub = document.getElementById('{!$Component.form.block.section2.subInput}').value;
            var descr = document.getElementById('{!$Component.form.block.section2.descrInput}').value;
            var cat = document.getElementById('{!$Component.form.block.section3.catInput}').value;
            var subCat = document.getElementById('{!$Component.form.block.section3.subCatInput}').value;
            
            if(cancel == 1) {
            
                cancelFunction();
            }

            else {
            
                if (supplier.toUpperCase() != 'UNKNOWN' && (conPerson == '' || conPersonPhone == '' || conPersonEmail == '')) {
                
                    alert("Known Suppliers require a Contact Person, Contact Person Phone, and Contact Person Email.  If the Supplier is unknown please enter 'Unknown'.");
                }
                
                else {
                
                    createFunction(supplier,acc,conPerson,con,conPersonPhone,conPersonEmail,sub,descr,cat,subCat);
                }
            }
            
            return false;
        }

    </script>
    <apex:form id="form">
    <apex:actionFunction name="createFunction" action="{!createCase}" rerender="form">    
        <apex:param id="supplier" assignTo="{!supplier}" name="supplier" value="" />  
        <apex:param id="acc" assignTo="{!acc}" name="acc" value="" />
        <apex:param id="conPerson" assignTo="{!conPerson}" name="conPerson" value="" />
        <apex:param id="con" assignTo="{!con}" name="con" value="" />
        <apex:param id="conPersonPhone" assignTo="{!conPersonPhone}" name="conPersonPhone" value="" />
        <apex:param id="conPersonEmail" assignTo="{!conPersonEmail}" name="conPersonEmail" value="" />
        <apex:param id="sub" assignTo="{!sub}" name="sub" value="" />
        <apex:param id="descr" assignTo="{!descr}" name="descr" value="" />
        <apex:param id="cat" assignTo="{!cat}" name="cat" value="" />
        <apex:param id="subCat" assignTo="{!subCat}" name="subCat" value="" />
    </apex:actionFunction>
    <apex:actionFunction name="cancelFunction" action="{!cancelCreate}" immediate="true"/>
    <apex:pageBlock id="block" title="SIMS Quick Create" mode="edit">
        <center><apex:commandButton value="Create" onclick="return createCase();"/><apex:commandButton value="Cancel" onclick="return createCase(1);"/></center>
        <apex:pageBlockSection id="section1" title="Supplier Information" columns="2">
            <apex:inputField id="supplierInput" value="{!Case.Supplier__c}"/>
            <apex:inputField id="accInput" value="{!Case.AccountId}" required="true"/>
            <apex:inputField id="conPersonInput" value="{!Case.Contact_Person__c}"/>
            <apex:inputField id="conInput" value="{!Case.ContactId}" required="true"/>
            <apex:inputField id="conPersonPhoneInput" value="{!Case.Contact_Person_Phone__c}"/>
            <apex:pageBlockSectionItem />
            <apex:inputField id="conPersonEmailInput" value="{!Case.Contact_Person_Email__c}"/>
            <apex:pageBlockSectionItem />
        </apex:pageBlockSection>
        <apex:pageBlockSection id="section2" title="Email-To-Case Information">
            <apex:inputField id="subInput" value="{!Case.Subject}" required="true"/>
            <apex:pageBlockSectionItem />
            <apex:inputField id="descrInput" value="{!Case.Description}"/>
        </apex:pageBlockSection>
        <apex:pageBlockSection id="section3" title="SIMS Detail">
            <apex:inputField id="catInput" value="{!Case.SIMS_Category__c}" required="true"/>
            <apex:pageBlockSectionItem />
            <apex:inputField id="subCatInput" value="{!Case.SIMS_Sub_category__c}" required="true"/>
        </apex:pageBlockSection>
        <center><apex:commandButton value="Create" onclick="return createCase();"/><apex:commandButton value="Cancel" onclick="return createCase(1);"/></center>
    </apex:pageBlock> 
    </apex:form>
</apex:page>

Controller:
public with sharing class SIMSQuickCreate{

    Apexpages.StandardController setCon;
    
    // SET VARIABLES
   
    public String supplier {get; set;}
    public String acc {get; set;}
    public String conPerson {get; set;}
    public String con {get; set;}
    public String conPersonPhone {get; set;}
    public String conPersonEmail {get; set;}
    public String sub {get; set;}
    public String descr {get; set;}
    public String cat {get; set;}
    public String subCat {get; set;}
    

    // CREATE NEW CASE VARIABLE    
    
    public Case c = new Case();
    
    
    // INSTANTIATE CONTROLLER
   
    public SIMSQuickCreate(Apexpages.StandardController controller) {

        setCon = controller;
    }
    
    public Pagereference createCase(){
        
        system.debug('--------------------------------------------------------------> Here is the value: '+acc+'  '+con);
        
        
        // GET ACCOUNTID FROM ACCOUNT NAME ENTERED
        
        List<Account> accList = new List<Account>{[SELECT Name,Id FROM Account WHERE Name =: acc]};
        
        Id accId = accList[0].Id;
        
        
        // GET CONTACTID FROM CONTACT NAME ENTERED
    
        List<Contact> conList = new List<Contact>{[SELECT Id FROM Contact WHERE Name =: con]};
        
        Id conId = conList[0].Id;
        
        
        // SET DEFAULT FIELD VALUES
        
        c.Status = 'Active in Support';
        c.Origin = 'Phone';
        c.Site_Outage__c = 'No';
        c.Severity_Level__c = 'Severity 4';
        c.Plan_Sent__c = date.today();
        c.Date_Issue_Originated__c = date.today();
        c.Product__c = 'Total Supplier Manager';
        c.SIMS_Root__c = 'Question/Training';
        c.Reported_by__c = 'No';
        
    
        // SET FIELD VALUES FROM USER INPUT
        
        c.AccountId = accId;        
        c.ContactId = conId;    
        c.Supplier__c = supplier;        
        c.Contact_Person__c = conPerson;        
        c.Contact_Person_Phone__c = conPersonPhone;        
        c.Contact_Person_Email__c = conPersonEmail;        
        c.Subject = sub;        
        c.Description = descr;        
        c.SIMS_Category__c = cat;        
        c.SIMS_Sub_category__c = subCat;
                
                
        // INSERT NEWLY CREATED CASE
        
        insert c;
        
        
        // SET CASE ID FOR REDIRECT URL
        
        Id caseId = c.Id;        
        
        Pagereference casePage = new Pagereference('/'+caseId);
        casePage.setRedirect(true);
        
        return casePage;
    }
    
    public Pagereference cancelCreate(){
    
        return new Pagereference('/'+Case.getSObjectType().getDescribe().getKeyPrefix()+'?fcf='+ApexPages.currentPage().getParameters().get('retUrl'));
    }
}


Test Class:
@isTest
private class SIMSQuickCreateTest {
  
    static testMethod void testSIMSQuickCreate() {
                
        // GET System Administrator PROFILE FOR TESTING
        
        Profile pro = [SELECT Id FROM Profile WHERE Name = 'System Administrator' LIMIT 1];
    
        PageReference pageRef = Page.SIMS_Quick_Create;
        
        List<Case> caseList = new List<Case>();
        
        
        // CREATE TEST USER FOR TESTING
        
        User u = new User(
            alias = 'u2', 
            email = 'test@test.com',
            emailencodingkey = 'UTF-8',
            FirstName = 'test',
            lastname='U',
            languagelocalekey = 'en_US',
            localesidkey = 'en_US',
            profileid = pro.Id,
            timezonesidkey = 'America/Los_Angeles',
            CommunityNickname = 'u2',
            isActive = true,
            username = 'testSciquest@test.com'
        );
        
        insert u;
        
                
        System.RunAs(u){
            
            Apexpages.StandardController controller;
            
            SIMSQuickCreate sqc = new SIMSQuickCreate(controller);
        
            String acc = 'Generic Supplier';
            String con = 'Supplier Issue';         

            sqc.createCase();
            sqc.cancelCreate();
        }
    }
}

The following error is what is returned in the Apex Test Execution:
Error Message:	System.QueryException: List has no rows for assignment to SObject
Stack Trace:	
Class.SIMSQuickCreate.createCase: line 38, column 1
Class.SIMSQuickCreateTest.testSIMSQuickCreate: line 58, column 1

Long story short, despite the fact that the "acc" and "con" variables are being passed correctly in the test class to the "createCase()" function, the query I am pulling for the Account in the controller isn't finding any data.  (It returns data just fine when the EXACT SAME data is entered on the actual VF page).

Please help.

Thanks!!
 
Best Answer chosen by adrissel
Arunkumar RArunkumar R
Hi,

Can you try the below test class,

@isTest
private class SIMSQuickCreateTest {
  
    static testMethod void testSIMSQuickCreate() {
                
        // GET System Administrator PROFILE FOR TESTING
        
        Profile pro = [SELECT Id FROM Profile WHERE Name = 'System Administrator' LIMIT 1];
    
        
        List<Case> caseList = new List<Case>();
        
        
        // CREATE TEST USER FOR TESTING
        
        User u = new User(
            alias = 'u2', 
            email = 'test@test.com',
            emailencodingkey = 'UTF-8',
            FirstName = 'test',
            lastname='U',
            languagelocalekey = 'en_US',
            localesidkey = 'en_US',
            profileid = pro.Id,
            timezonesidkey = 'America/Los_Angeles',
            CommunityNickname = 'u2',
            isActive = true,
            username = 'testSciquest@test.com'
        );
        
        insert u;
        
                
        System.RunAs(u)
        {
            
            Apexpages.StandardController controller;
            
            SIMSQuickCreate sqc = new SIMSQuickCreate(controller);
            
            Account acc = new Account();
            acc.Name = 'Generic Supplier';
            insert acc;
            
            Contact cnt = new Contact();
            cnt.LastName = 'Supplier Issue';
            cnt.AccountId = acc.Id;
            insert cnt;
        
            sqc.acc = acc.Name;
            sqc.con = cnt.LastName;         

            sqc.createCase();
            sqc.cancelCreate();
        }
    }
}

Thanks...!

All Answers

Arunkumar RArunkumar R
Hi,

Can you try the below test class,

@isTest
private class SIMSQuickCreateTest {
  
    static testMethod void testSIMSQuickCreate() {
                
        // GET System Administrator PROFILE FOR TESTING
        
        Profile pro = [SELECT Id FROM Profile WHERE Name = 'System Administrator' LIMIT 1];
    
        
        List<Case> caseList = new List<Case>();
        
        
        // CREATE TEST USER FOR TESTING
        
        User u = new User(
            alias = 'u2', 
            email = 'test@test.com',
            emailencodingkey = 'UTF-8',
            FirstName = 'test',
            lastname='U',
            languagelocalekey = 'en_US',
            localesidkey = 'en_US',
            profileid = pro.Id,
            timezonesidkey = 'America/Los_Angeles',
            CommunityNickname = 'u2',
            isActive = true,
            username = 'testSciquest@test.com'
        );
        
        insert u;
        
                
        System.RunAs(u)
        {
            
            Apexpages.StandardController controller;
            
            SIMSQuickCreate sqc = new SIMSQuickCreate(controller);
            
            Account acc = new Account();
            acc.Name = 'Generic Supplier';
            insert acc;
            
            Contact cnt = new Contact();
            cnt.LastName = 'Supplier Issue';
            cnt.AccountId = acc.Id;
            insert cnt;
        
            sqc.acc = acc.Name;
            sqc.con = cnt.LastName;         

            sqc.createCase();
            sqc.cancelCreate();
        }
    }
}

Thanks...!
This was selected as the best answer
adrisseladrissel
That worked perfectly!!  Now I just need to understand what you did.  I'll look through it in detail and ask any questions that may come up!

Thanks!!!