I have a test class where I have to insert a specific topic. Whenever I try to insert it, it throws an error saying the topic already exists. This would be fine, however if I try to query that same topic within the test class, it returns 0 rows and so throws a QueryException
. Any ideas on how to fix this or get around this error?
Here is a simple test class you can use to get the error. You must first ensure that the Topic exists within the org you are testing in:
@isTest
private class TestTopicSample {
@isTest
static void testTopic() {
Topic[] topics = [select Id from Topic where Name = 'Paused'];
System.assert(topics.isEmpty());
insert new Topic(Name = 'Paused');
}
}
Here is the error received running this:
System.DmlException: Insert failed. First exception on row 0; first error: DUPLICATE_VALUE, A topic with the name, Paused, already exists.: [Name]