I'm running into an error - duplicate exists - while trying to test my PushTopic creation class. This is an known issue from 4 years ago (PushTopic Test: Duplicate Name Bug?)
In the meantime, is there any workaround how to test the following code snippet?
The only fix I see is to add a method parameter and name the pushtopic dynamically (eg. by adding String pushTopicName
) or something. But this doesn't seem best practice.
//Create Pushtopic only containing account creates
public static void startCreateTopic()
{
//clear existing topic
delete [SELECT Id, Name FROM PushTopic where name = 'AccountCreatesTopic'];
PushTopic topic = new PushTopic();
topic.Name = 'AccountCreatesTopic';
The following test method runs into issues (not even inserting assert statements - just trying to get it running first) :
@isTest(SeeAllData=false)
public class PushTopic_AccountTest {
static testMethod void testCreatePushTopicCreation(){
PushTopic_Account.startCreateTopic();
try {
PushTopic t = [SELECT Id, Name FROM PushTopic WHERE name='AccountCreatesTopic'];
// The pushtopic has to exist so it cannot return a null value -> else the test fails
} catch (Exception e){
}
}
The following error occurs:
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.: []
----------- UPDATE ------------
I managed to get some of the class tested by doing the following : But now still the problems occurs - I cannot reach 100% coverage?
@TestVisible private static void StartCreateTopic(String name){ startCreateTopicName(name); } // Standard method -- These 3 lines cannot be tested. public static void startCreateTopic(){ startCreateTopicName('AccountCreatesTopic'); } // Create Pushtopic only containing account creates public static void >startCreateTopicName(String pushTopicName) { //clear existing topic delete [SELECT Id, Name FROM PushTopic where name =: pushTopicName]; PushTopic topic = new PushTopic();