2

I am using pattern matchers for the email filed in contact in my development. When writting the test i am getting error in that patter matches.Here i have method the error line.Kindly help me to resolve this and tell how to write test for pattern matcher. In Apex class:

email = contact.email;

If( Pattern.matches(('^[_A-Za-z0-9._-]+@test.com.au'), email))
{
         if(booking == 'Team1') {
                    setnon(False);
                    setbuy(True);
                    setRepl(False);
                }
                else if(booking == 'Team2') {

                    setnon(False);
                    setbuy(False);
                    setRepl(True);
                }
                 else if(booking == 'None') {
                    setnon(False);
                    setbuy(False);
                    setRepl(False);
                    }
                }

In test class:

    @isTest(seeAllData = true)
public class newcont_TestClass
{
    static testmethod void Test1()
    {
            PageReference pg = page.newcont;
            Test.setCurrentPage(pg);
            //To get Account Record Type id f
            Schema.DescribeSObjectResult cfrSchema = Schema.SObjectType.Account; 
            Map<String,Schema.RecordTypeInfo> AccountRecordTypeInfo = cfrSchema.getRecordTypeInfosByName();

            // To get contact record type
            RecordType rt = [SELECT id,Name FROM RecordType WHERE Name='PS'];

            Account acc = new Account(Name = '190 ', recordtypeid=AccountRecordTypeInfo .get('PS').getRecordTypeId());
            insert acc;
            Account account = [SELECT Id FROM Account LIMIT 1];

            Contact con = new Contact (LastName = 'test', Email = 'test@test.com.au', AccountId= account.id,recordtypeid=rt.id );
            insert con;
            Contact contact1 = [SELECT Id, LastName, Email FROM CONTACT WHERE LastName = 'test' LIMIT 1]; 

            Case case1 = new Case(S_Logged_By__c = 'test',Full_Name__c= 'test',Email_Address__c = 'test1@test.com.au',Contact_Name__c = contact1.id  );
            insert case1;        
            ApexPages.currentPage().getParameters().put('message', 'Record Created Successfully' );

            ApexPages.StandardController stdController = new ApexPages.StandardController (case1);   
            newcont controller = new newcont();
            controller.method1();
            Test.StopTest();

    }
}
7
  • Are you sure you're querying the same contact and you don't have SeeAllData=true for your test method? Also, is this your helper class for a Contact trigger or how does this work? Can you explain a bit more or maybe post the rest of the code? – Boris Bachovski Jul 26 '14 at 6:47
  • @Bachovski Thank for your comment. yes i am using SeeAllData=true.I a m apex class to insert the contact which has the email in the following formate. – Lavanya Sanathkumar Jul 26 '14 at 7:40
  • Can you pleas post the whole test method? – Boris Bachovski Jul 26 '14 at 7:41
  • @Bachovski I have updated my code. Inside method1() I have given the pattern macther for the email field. – Lavanya Sanathkumar Jul 26 '14 at 7:54
  • From what I can see you're referencing contact1 in your apex class, which is an existing contact in the system and not the one you're creating. Therefore I am not sure if contact1's email address matches that pattern. Also please check the field level security for the email field on the contact and ensure it's visible to the user's profile which is running the test method. – Boris Bachovski Jul 26 '14 at 7:57
1

Based on our discussion and looking at the whole (massive) controller, the issue was that your case variable was private and you were setting the case email field based on a URL parameter.

Solution:

Either make your case variable public and set the email field on it before calling the method that you're testing, or, specify the URL parameter in your test class before you call the test method:

PageReference pg = page.newcont;
Test.setCurrentPage(pg);
...
ApexPages.currentPage().getParameters().get('email', con.Email);
DC controller = new DC();
controller.method1();

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.