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
nikkeynikkey 

Test Class Code coverage for a trigger is 70% and Error as :Only top-level class methods can be declared static

Can any one help on this .I have a trigger written on Case object for the duplication to get it closed .I have written a Test class where it gives the code coverage as 70% only. When i Execute in Developer Console it throws an Error as :
Line: 3, Column: 24
Only top-level class methods can be declared static
Trigger :
trigger Case_DuplicateClose on Case (before insert) {
    Map<String, Case> newCaseMap = new Map<String, Case>();
    for(Case c : trigger.new)
    {
        while(c.Subject.startsWith('RE:') || c.Subject.startsWith('Re:') || c.Subject.startsWith('FW:') || c.Subject.startsWith('Fw:'))
            c.Subject = c.Subject.substring(3).trim();
        if(newCaseMap.containsKey(c.Subject))
            c.Status = 'Closed';
        else
            newCaseMap.put(c.Subject, c);
    }
    for(Case c : [Select Subject From Case Where Subject in :newCaseMap.keySet()])
        newCaseMap.get(c.Subject).status = 'Closed';
}

Test Class :
 
@istest 
private class TestCase_DuplicateClose{ 
static testmethod void testcaseDuplicate(){ 
list <case>cases = new list<case>{};
for(integer i=0;i<200;i++){
case c=new case(Subject='RE:'+i);
cases.add(c);
}
test.startTest();
insert cases;
test.stopTest();
List<case> insertedcases =[Select Subject,status from case where id in:cases];
for(case c :insertedcases){
system.assertEquals(
'Closed',c.status);
}
}
}

Any help very much appreciated.
 
Prabhat Kumar12Prabhat Kumar12
I have tested your code in Dev. org and i am not getting any error. I have made some changes please use this code.

@istest
private class TestCase_DuplicateClose{
static testmethod void testcaseDuplicate(){
list <case>cases = new list<case>{};
for(integer i=0;i<200;i++){
case c=new case(Subject='RE:'+i, Status = 'Closed');
cases.add(c);
}
test.startTest();
insert cases;
test.stopTest();
List<case> insertedcases =[Select Subject,status from case where id in:cases];
for(case c :insertedcases){
system.assertEquals(
'Closed',c.status);

}
}
}
nikkeynikkey
Hi Prabhat Kumar , Thanks for your reply. The code works fine now ,but when i moved the code into production its showing code coverage as 0% while in Sandbox it is showing as 90%.Will the code work in production now.Any help very much appreciated.
Prabhat Kumar12Prabhat Kumar12
Yes it will work in production. To get code coverage in production run all test from console.
 
nikkeynikkey
Can we run all test classes in developer console in production.The data will got get disappeared.
Prabhat Kumar12Prabhat Kumar12
We can run - Go to > Developer Console > Test > Rull All Test.