3

I have the following class

public with sharing class templicenseemail {
    private ApexPages.standardController standardController;

    public Trainee__c trainee { get; set; }
    public templicenseemail(ApexPages.StandardController controller) {
        this.standardController = standardController;
    }
    public PageReference doSomething() {
        trainee  = [SELECT Email__c,First_Name__c,Institution__c,Last_Name__c FROM Trainee__c 
                WHERE Id = :ApexPages.currentPage().getParameters().get('id')];

        String whatIs = trainee.Volume_Serial_Number__c;
        integer strlen = whatIs.length();

        ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO, trainee.Email__c  ));
        return null;
    }   
}

With the following test code:

@isTest(seealldata=false)
private class Send_noti_emailsTestClass5 {
    static testMethod void validateSend_noti_emails5() {
        Trainee__c o = new Trainee__c(Email__c='email', First_Name__c ='Name',Institution__c= 'InstName',Last_Name__c ='Name' , Id = ApexPages.currentPage().getParameters().get('id'));
        insert o;
        Trainee__c myTestTrainee = [SELECT Id From Trainee__c LIMIT 1];

        PageReference myVfPage = Page.sendinstantemail;
        Test.setCurrentPage(myVfPage);
        ApexPages.StandardController sc = new ApexPages.StandardController(myTestTrainee);
        templicenseemail testController = new templicenseemail(sc);

        testController.trainee= o;

        testController.doSomething();
    }
}

I get an error 'system.queryexception list has no rows for assignment to sobject' in the class at line:

trainee  = [SELECT Email__c,First_Name__c,Institution__c,Last_Name__c FROM Trainee__c 
                WHERE Id = :ApexPages.currentPage().getParameters().get('id')];

This is because i am not setting the Id correctly in the test code,

Can someone tell me whats wrong?

5

Because of the way you are getting the ID, through the page parameters, you need to set the page parameters in the test code

Something like this will work

system.currentPageReference().getParameters().put('Id', myTestTrainee.Id);

I might suggest just using the getRecord() in your constructor, instead of taking it from the page parameters, unless you are using some sort of URL hacking.

public templicenseemail(ApexPages.StandardController controller) {
     this.standardController = standardController;
     this.trainee = (Trainee__c)standardController.getRecord();
}
  • So your solution works, but i have another [i am not showing here] class i call from the templicenseemail class, and it does not see the email address i am setting with o, could you tell me why? – user2333346 Jan 7 '14 at 19:54
  • In other words, im sending the email to the other class, but the email doesnt seem to work, because when i try to query records based on that email, i also get "system.queryexception list has no rows for assignment to sobject' error. – user2333346 Jan 7 '14 at 19:56
  • I would be happy to help, but I would suggest adding another question to increase visibility on the new issue – Chris Duncombe Jan 7 '14 at 20:02
2

In your test code, just add the ID to the page parameters

Test.setCurrentPage(myVfPage);   
ApexPages.currentPage().getParameters().put('id', myTestTrainee.Id);
  • you have to put myTestTrainee.Id as sfdc_ninja states, or you will get an error. – user2333346 Jan 7 '14 at 19:48
  • You're right. Didn't pay close enough attention to what I was typing. – Daniel Hoechst Jan 7 '14 at 20:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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