+ Start a Discussion
Shruthi GM 4Shruthi GM 4 

I am facing this error in the test calss:-List has no rows for assignment to SObject

Controller:-

public with sharing class MyLoginController{


private ApexPages.StandardController controller;
public Login__c login{get;set;}
public id loginId{get;set;}


//constructor
public MyLoginController(ApexPages.StandardController controller) {
 this.controller=controller;
 this.login= (Login__c)Controller.getRecord();
 System.debug('@@@@@'+login.Id);


}



//Login method when user clicks on login button
public ApexPages.PageReference LoginMethod() {
system.debug('&&&&&&&&'+login);

loginId = [SELECT Id FROM Login__c 

                      WHERE Name =: login.Name].Id;
string password = [SELECT password__c FROM Login__c 

                      WHERE Name =: login.Name].password__c;
                      
             system.debug('!!!!!!!!'+login.Name);
           
       
        
        system.debug('@@@'+password);
        
        if(login.password__c==password) {
        system.debug('%%%%%%');
        PageReference redirectPage = new PageReference ('/apex/Nomination_msg');
        //redirectPage.setRedirect(true);
        //redirectPage.getParameters().put('id',controller.getId());
        
        return redirectPage;
        }
        else{

        login.Name.addError('Incorrect username or password.Please Enter correct value.');
        return null;
        }


}



//save method when user clicks on save button on nomination msg page
public pagereference save(){
login__c l= [select name,password__c,First_Name__c,Last_Name__c,age__c,Email__c,Country_of_user__c from login__c where id =: loginId ];
l.First_Name__c=login.First_Name__c;
l.Last_Name__c=login.Last_Name__c;
l.age__c=login.age__c;
l.Email__c=login.Email__c;
l.Country_of_user__c=login.Country_of_user__c;
update l;
try{
       system.debug('Entered into the Method');
    PageReference redirectPage = new PageReference ('/apex/successful_msg');
        return redirectPage;
             return null;
                }
    Catch(Exception dmle){
    ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Error'));
    //ApexPages.addMessages(dmle);
    //ApexPages.addMessage(myMsg);
    return null;
    } 
    }           




//signup method when the user clicks on signup button on login page
public pagereference signup() {
Pagereference pg=new Pagereference('https://ap2.salesforce.com/066280000027Mwq');
try{
system.debug('@@@###$$$$%%%%');
  PageReference redirectPage = Page.SignupPage;
               redirectPage.setRedirect(true);
                 return redirectPage;
              }
                 
      Catch(Exception dmle){
    ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Error'));
    //ApexPages.addMessages(dmle);
    //ApexPages.addMessage(myMsg);
    }          
return null;
}




//submit method when the user clicks on submit button on signup page
public pagereference Submit(){
login__c lp=new login__c();
lp.Name=login.Name;
lp.password__c=login.password__c;
lp.RePassword__c =login.RePassword__c ;
try{
 PageReference redirectPage = new PageReference ('/apex/Nomination_msg_a');
       system.debug('Entered into the Method');
          if(login.Password__c == login.Repassword__c) {
               Insert lp;
                return redirectPage;
                }
                else {
                login.Repassword__c.adderror('Please enter correct password');
                redirectPage.setRedirect(false);
                return null;
                }
                }
    Catch(Exception dmle){
    ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Error'));
    //ApexPages.addMessages(dmle);
    //ApexPages.addMessage(myMsg);
    return null;
    } 
}





//savesignup method when user clicks on save option on Nomination_msg_a page
public pageReference savesignup(){
loginId = [SELECT Id FROM Login__c WHERE Name =: login.Name].Id;
login__c log=[select name,password__c,First_Name__c,Last_Name__c,Age__c,Email__c,Country_of_user__c, id from login__c where id =: loginid];
log.First_Name__c=login.First_Name__c;
log.Last_Name__c=login.Last_Name__c;
log.Age__c=login.Age__c;
log.Email__c=login.Email__c;
log.Country_of_user__c=login.Country_of_user__c;
update log;
system.debug('&&&&&&&'+log);
try{

       system.debug('Entered into the Method');
       
            PageReference redirectPage = new PageReference ('/apex/successful_msg');
                return redirectPage;
                }
                
    Catch(Exception dmle){
    ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Error'));
    //ApexPages.addMessages(dmle);
    //ApexPages.addMessage(myMsg);
    return null;
    } 



}

test class:-

@isTest
public class MyLoginControllerTest{

static List<login__c > Lstlogs = new List<login__c >();

public static testMethod void oppExtensionTest(){
Test.startTest();
login__c login=new login__c();
login.Name=login.id;
login.Password__c='sss';
login.First_Name__c ='Shruthi GM';
login.Last_Name__c ='GM';
login.age__c =12;
login.Email__c ='shruthi.gm@tcs.com';
login.Country_of_user__c ='India'; 
insert login;

login__c login1=new login__c();
login1.Name=login1.id;
login1.Password__c='test';
login1.First_Name__c ='Shruthi GM';
login1.Last_Name__c ='GM';
login1.age__c =12;
login1.Email__c ='shruthi.gm@tcs.com';
login1.Country_of_user__c ='India'; 
insert login1;


ApexPages.StandardController controller;
controller = new ApexPages.StandardController(login);
MyLoginController mec = new MyLoginController(controller);
ApexPages.currentPage().getParameters().put('id',login.id);
PageReference testPage = mec.save();
PageReference testPage1 = mec.signup();
PageReference testPage2 = mec.submit();
PageReference testPage3 = mec.savesignup();

ApexPages.StandardController controller1;
controller1 = new ApexPages.StandardController(login1);
MyLoginController mec1 = new MyLoginController(controller1);
ApexPages.PageReference testing1 = mec1.LoginMethod();
PageReference testPage4 = mec1.save();
PageReference testPage5 = mec1.signup();
PageReference testPage6 = mec1.submit();
PageReference testPage7 = mec1.savesignup();
Test.stopTest();
}

Please help.

 
JethaJetha
Can you please point out, in which line you are getting aforesaid error. Would be easy for us in tracking.
Shruthi GM 4Shruthi GM 4
in the test class here am getting error:- PageReference testPage = mec.save();
Tarun SuriTarun Suri
Hi shruthi,
login.Name=login.id;
You are trying to get login.id before inserting login. Instead try this login.Name='Testname';
JethaJetha
Please try below test class, Hope it would resolve our issue,
 
public class MyLoginControllerTest{

static List<login__c > Lstlogs = new List<login__c >();

public static testMethod void oppExtensionTest(){
Test.startTest();
login__c login=new login__c();
login.Name= 'Testname';
login.Password__c='sss';
login.First_Name__c ='Shruthi GM';
login.Last_Name__c ='GM';
login.age__c =12;
login.Email__c ='shruthi.gm@tcs.com';
login.Country_of_user__c ='India'; 
insert login;

login__c login1=new login__c();
login1.Name='Test';
login1.Password__c='test';
login1.First_Name__c ='Shruthi GM';
login1.Last_Name__c ='GM';
login1.age__c =12;
login1.Email__c ='shruthi.gm@tcs.com';
login1.Country_of_user__c ='India'; 
insert login1;


ApexPages.StandardController controller;
controller = new ApexPages.StandardController(login);
MyLoginController mec = new MyLoginController(controller);
ApexPages.currentPage().getParameters().put('id',login.id);
mec.login = login1;
PageReference testPage = mec.save();
PageReference testPage1 = mec.signup();
PageReference testPage2 = mec.submit();
PageReference testPage3 = mec.savesignup();

ApexPages.StandardController controller1;
controller1 = new ApexPages.StandardController(login1);
MyLoginController mec1 = new MyLoginController(controller1);
ApexPages.PageReference testing1 = mec1.LoginMethod();
PageReference testPage4 = mec1.save();
PageReference testPage5 = mec1.signup();
PageReference testPage6 = mec1.submit();
PageReference testPage7 = mec1.savesignup();
Test.stopTest();
}

 
Shruthi GM 4Shruthi GM 4
again same error am facing:- System.QueryException: List has no rows for assignment to SObject

I have changed the code like this:-

@isTest
public class MyLoginControllerTest{

static List<login__c > Lstlogs = new List<login__c >();

public static testMethod void oppExtensionTest(){
Test.startTest();
login__c login=new login__c();
login.Name='Testing';
login.Password__c='sss';
login.First_Name__c ='Shruthi GM';
login.Last_Name__c ='GM';
login.age__c =12;
login.Email__c ='shruthi.gm@gmail.com';
login.Country_of_user__c ='India'; 
insert login;

login__c login1=new login__c();
login1.Name='testing1';
login1.Password__c='test';
login1.First_Name__c ='Shruthi GM';
login1.Last_Name__c ='GM';
login1.age__c =12;
login1.Email__c ='shruthi.gm@gmail.com';
login1.Country_of_user__c ='India'; 
insert login1;


ApexPages.StandardController controller;
controller = new ApexPages.StandardController(login);
MyLoginController mec = new MyLoginController(controller);
ApexPages.currentPage().getParameters().put('id',login.id);
mec.login = login1;
PageReference testPage = mec.save();
PageReference testPage1 = mec.signup();
PageReference testPage2 = mec.submit();
PageReference testPage3 = mec.savesignup();

ApexPages.StandardController controller1;
controller1 = new ApexPages.StandardController(login1);
MyLoginController mec1 = new MyLoginController(controller1);
ApexPages.PageReference testing1 = mec1.LoginMethod();
PageReference testPage4 = mec1.save();
PageReference testPage5 = mec1.signup();
PageReference testPage6 = mec1.submit();
PageReference testPage7 = mec1.savesignup();
Test.stopTest();
}

Here am getting that error!
PageReference testPage = mec.save();