0

I'm testing a post installation script, but I'm having a problem. depending on the previous version, in my script I do some inserts and upserts of different sObjects. For instance, a PushTopic is upserted, but when I run my test class an upsert error appears saying that a pushtopic with this name already exists. Even with this in my scripting code:

pushTopic.Name = 'Question_Answered' + (Test.isRunningTest() ? '_test' : '');

So, even with that validation, it says that the PushTopic "QuestionAnswered_test" already exists when the test class is running. How do I delete it, or make it so it erases itself after the test is over?

This is my test method:

static testMethod void myUnitTest() {
    PostInstallationScripting postInstallation = new PostInstallationScripting();
    Test.testInstall(postInstallation, null);
    Test.testInstall(postInstallation, new Version(1, 142));
    Package_Resources__c pResources = [Select Name__c, Prefix__c From Package_Resources__c limit 1];
    System.AssertEquals('ML_App_Resources', pResources.Name__c);
}

This is the entire method that fails in my post installation:

global void initStreamingApi(){
    //chekcea q no exista
    List<PushTopic> pushTopics = [Select ID From PushTopic Where Name = 'Question_Answered' limit 1];
    
    //26-02-2015 cambiado por un upsert para cambiar la query
    
    PushTopic pushTopic;
    
    if(pushTopics.size() == 0){
        pushTopic = new PushTopic();
        pushTopic.Name = 'Question_Answered' + (Test.isRunningTest() ? '_test' : '');
        pushTopic.ApiVersion = 31.0;
        pushTopic.NotifyForOperationCreate = true;
        pushTopic.NotifyForOperationUpdate = true;
        pushTopic.NotifyForOperationUndelete = false;
        pushTopic.NotifyForOperationDelete = false;
        pushTopic.NotifyForFields = 'Referenced';
    }else{
        pushTopic = pushTopics.get(0);
    }       
    
    pushTopic.Query = 'SELECT Id, mlsales__Answer_Date__c, mlsales__Seller_Nickname__c FROM mlsales__Question__c';
    upsert pushTopic;
    
}

1 Answer 1

2

The issue is that the PushTopic name must be unique. Could you please try to generate the random name and then set that as a name in test class context. Sample code to generate random string.

Integer len = 20;
Blob blobKey = crypto.generateAesKey(128);
String key = EncodingUtil.convertToHex(blobKey);
String Name = key.substring(0,len);
10
  • didn't work, the upsert fail keeps showing up :/ Commented Apr 21, 2021 at 12:32
  • This issue is for only test class, right? Is it working fine when it comes to actual testing? Instead of _Test, try adding the current date & time in the name. So it won't fail.
    – Kalpana
    Commented Apr 21, 2021 at 12:55
  • I don't really know what you mean by only for test class, but the chain of events is this. I run the test class and the upsert in that method fails. No other part of the script fails, just that Commented Apr 21, 2021 at 13:17
  • I'll try changing the '_test' for the date as you say. Thanks Commented Apr 21, 2021 at 13:17
  • now the upsert fails because the string is too long. this is incredible Commented Apr 21, 2021 at 13:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .