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
sumit dsumit d 

Exception in controllers test class

Hi All,
my controller is given below:-
public class SocialCasePushTopicController {
    public static void createPushTopic(){
        PushTopic pushTopic = new PushTopic();
        pushTopic.Name = 'CaseUpdates1';
        pushTopic.Query = 'SELECT Id, CaseNumber, Status FROM Case WHERE recordTypeId = \'01241000001USrEAAW\'';
        pushTopic.ApiVersion = 36.0;
        pushTopic.NotifyForOperationCreate = true;
        pushTopic.NotifyForOperationUpdate = true;
        pushTopic.NotifyForOperationUndelete = true;
        pushTopic.NotifyForOperationDelete = true;
        pushTopic.NotifyForFields = 'Referenced';
        insert pushTopic;
    }
    
}
when i write the test class its showing me this error:-System.DmlException: Insert failed. First exception on row 0; first error: DUPLICATE_VALUE, A PushTopic with this name already exists. Please choose a different name.: []
my test class is given below:-
@isTest
private class SocialCasePushTopicControllerTest {
   static Testmethod void createPushTopicTest(){
         SocialCasePushTopicController.createPushTopic();
    }
}
Aany suggestions?
Yaroslav ProYaroslav Pro
becouse you use the same name, change name
try to avoid hardcode id
for example
ObjectUtil.getRecordTypeIdsByDeveloperName(Account.sObjectType).get('Main');


   static Testmethod void createPushTopicTest(){
         SocialCasePushTopicController.createPushTopic();
        List<PushTopic> toAssert = [SELECT id,Name FROM PushTopic];
               system.assertEquals(1,toAssert.size());
            system.assertEquals('Name',toAssert.get(0).Name);
       
    }
sumit dsumit d
Hi ,
Still showing me the error.
Any suggestions?
Yaroslav ProYaroslav Pro
pushTopic.Name = 'CaseUpdates2';
Yaroslav ProYaroslav Pro
did you change it?
sumit dsumit d
yes,
i changes it still showing the error?
Can i use if (!Test.isRunningTest())?
is it good?
Any suggestions?
Shubham4462Shubham4462
Hi Sumit ,

i tried it in my org it is working fine...... with 100% coverage. i have tried your code in my org....

i think you should go to this link for your problem here you can find solution

https://salesforce.stackexchange.com/questions/197607/how-to-test-pushtopic-creation
sumit dsumit d
Hi shubham,
its giving me the 100% coverage but showing the exception.
And i dont think if (!Test.isRunningTest()) is good Approach?
Any suggestions?
Shubham4462Shubham4462
Do you try that test class which is in link???...

if you are not able to make condition true so you just have to use it because of their are some situation where you are stucked so you just have to use them :)
Shubham4462Shubham4462

Sometimes some actions you can't cover with unit tests.
For example, if you have some callouts, you can't execute callout in test execution context.
For such cases you might use Test.isRunningTest() method and determine if you are inside the Test execution context and do some fake logic inside of tests.
sumit dsumit d
Its not covering the full method after Test.isRunningTest() .
so how to do it with other Approach?
 
Raj VakatiRaj Vakati
Change you code as below 

 
public class SocialCasePushTopicController {
    public static void createPushTopic(String name){
        PushTopic pushTopic = new PushTopic();
        pushTopic.Name = name;
        pushTopic.Query = 'SELECT Id, CaseNumber, Status FROM Case WHERE recordTypeId = \'01241000001USrEAAW\'';
        pushTopic.ApiVersion = 36.0;
        pushTopic.NotifyForOperationCreate = true;
        pushTopic.NotifyForOperationUpdate = true;
        pushTopic.NotifyForOperationUndelete = true;
        pushTopic.NotifyForOperationDelete = true;
        pushTopic.NotifyForFields = 'Referenced';
        insert pushTopic;
    }
    
}



And Test Class

 
@isTest
private class SocialCasePushTopicControllerTest {
   static Testmethod void createPushTopicTest(){

//create case 
Case c = new Case();
 //enter details
c.RecordTypeId = '01241000001USrEAAW';
 c.Type = 'My Type';
 c.Origin = 'My Origin'; 
c.Status = 'My Status'; 
 insert c;

         SocialCasePushTopicController.createPushTopic('Testing');
    }
}

 
sumit dsumit d
Hi raj,
Still showing the same error.
Any another solution?
Raj VakatiRaj Vakati
did you made changes as i suggested ?
sumit dsumit d
yes i made the changes but still same error.