You need to sign in to do that
Don't have an account?
Kyo
I have a problem Testclass on the Trigger
CodeCoverage 50%. I want more 75 %
trigger PopAccount on Case (before insert,before update) { for(Case ca : trigger.new){ if(ca.AccountID == null && ca.Account_Number__c != null){ Account acc = [select id,AccountNumber From Account Where AccountNumber =: ca.Account_Number__c]; ca.AccountID = acc.id; } } }
@isTest private class TestPopAccount{ static testMethod void myTest() { Account Acc = new Account(); Acc.Name = 'test'; insert Acc; Acc = [select id,AccountNumber,Type From Account Where id =: acc.id]; Contact con = new Contact(); Con.AccountID = Acc.id; Con.LastName = 'Test'; insert con; Case ca = new Case(); ca.Account_Number__c = '000001'; ca.AccountID = Acc.id; ca.ContactID = con.id; ca.Origin = 'Phone'; ca.Status = 'Closed'; insert ca; update ca;
Thank you so much.
In your method you are using this
You are checking ca.AccountID == null but it is not as you have set this in test method
So you will never enter the condition, After fixing this you code coverage will increase but test might fail as you have not set AccountNumber for inserted account in test method so
This will not return any record, plesse fix this also by setting the accountnumber in test method when you insert account.
All Answers
In your method you are using this
You are checking ca.AccountID == null but it is not as you have set this in test method
So you will never enter the condition, After fixing this you code coverage will increase but test might fail as you have not set AccountNumber for inserted account in test method so
This will not return any record, plesse fix this also by setting the accountnumber in test method when you insert account.
I can edit it, then Code Coverage 75%, but Error
System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, PopAccount: execution of BeforeInsert caused by: System.QueryException: List has no rows for assignment to SObject Trigger.PopAccount: line 4, column 23: []
Change this
to
Thank! Perfect!
Your welcome , If you want to know structure of test method please check this
http://forceschool.blogspot.com/2011/06/testing-apex-structure-of-test-class.html