我无法上传我的包,因为测试覆盖率低于 75%。如何提高覆盖率。如何编写触发器的测试方法。请帮忙。
我试过这段代码:这是我的控制器类:
public class MyController{
public List<Merchandise__c> merch{get;set;}
public Merchandise__c toDelete{get;set;}
public MyController(){
merch = [SELECT ID,Merchandise__c.name,Price__c,Description__c,Total_Inventory__c FROM Merchandise__c];
}
public pageReference del(){
string id = Apexpages.currentpage().getParameters().get('merchandiseId');
toDelete = [SELECT Id,Merchandise__c.name,Price__c,Description__c,Total_Inventory__c FROM Merchandise__c WHERE Id =: id];
delete toDelete;
return null;
}
public pageReference add(){
Merchandise__c item = new Merchandise__c(
Name = 'Enter Name',
Description__c = 'Enter Description',
Price__c = 0.00,
Total_Inventory__c = 0
);
merch.add(item);
insert item;
return null;
}
public PageReference save(){
string id = Apexpages.currentpage().getParameters().get('merchandiseId');
Merchandise__c toUpdate = [SELECT Name, Description__c, Price__c FROM Merchandise__c WHERE ID =: id];
update toUpdate;
return null;
}
}
我已经创建了这个测试类:
@isTest
Public Class demoTest{
static testMethod void checkDatatableData() {
List<Merchandise__c> merch;
Test.startTest();
merch = [SELECT ID,Merchandise__c.name,Price__c,Description__c,Total_Inventory__c FROM Merchandise__c];
Test.stopTest();
}
static testMethod void checkAdd() {
Merchandise__c item = new Merchandise__c(
Name = 'Enter Name',
Description__c = 'Enter Description',
Price__c = 0.00,
Total_Inventory__c = 0
);
Test.startTest();
insert item;
System.assertNotEquals(null,item.Id);
List<Merchandise__c> merchItem = [SELECT Id FROM Merchandise__c WHERE Id =: item.Id];
System.assertEquals(1,merchItem.size());
Test.stopTest();
}
static testMethod void checkUpdateDelete() {
Merchandise__c testmerch = new Merchandise__c(
Name = 'NewProduct',
Description__c = 'this is anew product',
Price__c = 20.00,
Total_Inventory__c = 100
);
Test.startTest();
insert testmerch;
system.assertNotEquals(null,testmerch.Id);
testmerch.Price__c = 40.00;
update testmerch;
Merchandise__c updatedmerch = [SELECT Id,Price__c FROM Merchandise__c WHERE Id =: testmerch.Id];
system.assertEquals(40.00,updatedmerch.Price__c);
Merchandise__c toDelete = [SELECT ID FROM Merchandise__c WHERE Id =: testmerch.Id];
system.assertNotEquals(null,toDelete.Id);
delete toDelete;
Test.stopTest();
}
}
你能告诉我我需要做哪些改变来提高覆盖率吗?