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
Derrick Abbey 21Derrick Abbey 21 

Topics in Apex unit tests

I am testing an Apex class that queries topics.  When creating test data, I am running into an issue with regards to Topics.  If I attempt to create topics for testing that happen to match real topics, then I get an error that says that the topic already exists.  However, if I query topics in the test class, then I get 0 results.  I'd like to run these tests without using seeAllData=true. Has anyone ever encounterd this before?  Any ideas as to how to get test topics to use in my testing?
Andrew GAndrew G
Hi Derrick.  
I have been able to replicate the issue you report regarding Topics picking Up that an existing Topic exists on insert, yet if you Query you don't get the existing topics in the system.  Most strange.
My only thought would be to try and randomise the Name for the Test data, unless the test is to fire based on the Topic Name.
@IsTest
    static private void test_One(){

        Topic topic = new Topic(Name='My Random Test Topic : ' + Datetime.now().format('yyyy-MM-dd'));
        insert topic;
        Test.startTest();
        List<Topic> topic_List  =  [SELECT Id, Name FROM Topic];
        System.assertEquals(1,topic_List.size());
        System.debug('***** debug : ' + topic_List[0].Name);
        Test.stopTest();
    }

But the behaviour that you have reported and I have replicated is most strange.  

Regards
Andrew