You need to sign in to do that
Don't have an account?
sumit 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?
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?
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);
}
Still showing me the error.
Any suggestions?
i changes it still showing the error?
Can i use if (!Test.isRunningTest())?
is it good?
Any suggestions?
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
its giving me the 100% coverage but showing the exception.
And i dont think if (!Test.isRunningTest()) is good Approach?
Any suggestions?
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 :)
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.
so how to do it with other Approach?
And Test Class
Still showing the same error.
Any another solution?