You need to sign in to do that
Don't have an account?
Archana Lal 2
Hi,Can anybody please help me to write a test class that gives a atleat 75% code Coverage
Hi All,
Can anybody help me to write a test class for the below apex class.
public class MobileShopeeClass{
public static void NewCustomerDiscount(list<Mobile__c>MobileList1){
for(Mobile__c m:MobileList1){
if(m.Brand__c=='Samsung'){
m.Price__c=m.Price__c-(m.Price__c*10)/100;
m.Discount_status__c='You have got 10 % Discount!!';
}
else if(m.Brand__c=='Apple')
{
m.Price__c=m.Price__c-(m.Price__c*20)/100;
m.Discount_status__c='You have got 20 % Discount!!';
}
}
}
public static void OldCustomerDiscount(list<Mobile__c>MobileList2){
for(Mobile__c m:MobileList2){
if(m.Brand__c=='Samsung'){
m.Price__c=m.Price__c-(m.Price__c*5)/100;
m.Discount_status__c='You have got 5% Discount!!';
}
else if(m.Brand__c=='Apple')
{
m.Discount_status__c='Sorry,no discount!!';
}
}
}
Can anybody help me to write a test class for the below apex class.
public class MobileShopeeClass{
public static void NewCustomerDiscount(list<Mobile__c>MobileList1){
for(Mobile__c m:MobileList1){
if(m.Brand__c=='Samsung'){
m.Price__c=m.Price__c-(m.Price__c*10)/100;
m.Discount_status__c='You have got 10 % Discount!!';
}
else if(m.Brand__c=='Apple')
{
m.Price__c=m.Price__c-(m.Price__c*20)/100;
m.Discount_status__c='You have got 20 % Discount!!';
}
}
}
public static void OldCustomerDiscount(list<Mobile__c>MobileList2){
for(Mobile__c m:MobileList2){
if(m.Brand__c=='Samsung'){
m.Price__c=m.Price__c-(m.Price__c*5)/100;
m.Discount_status__c='You have got 5% Discount!!';
}
else if(m.Brand__c=='Apple')
{
m.Discount_status__c='Sorry,no discount!!';
}
}
}
All Answers
Here is the test class
Thanks a lot on the answer ,but i have one more to clarify,i just want to mention that i have written a trigger for this.I this case do i need to write the test class like this.
trigger MobileShopee on Mobile__c (Before Insert,Before Update) {
if(Trigger.isInsert==true){
MobileShopeeClass.NewCustomerDiscount(Trigger.New);
if(trigger.isUpdate==true){
MobileShopeeClass.OldCustomerDiscount(Trigger.New);
Thanks & Regards,
Archana Lal
My test class will cover both your class and you trigger.
You trigger looks good to me.
If my test class help you, please dont' forget to mark it best.
Error MessageSystem.QueryException: List has no rows for assignment to SObject
Stack TraceClass.MobileShopeeClass_Test.test_oldcustomerdiscount: line 53, column
public class MobileShopeeClass_Test {
@TestSetup
static void setup(){
List<Mobile__c> mobileToInsertList = new List<Mobile__c> {
new Mobile__c (Customer_Name__c = 'Suma',Brand__c = 'Samsung', Price__c = 100.00),
new Mobile__c (Customer_Name__c = 'Gopan',Brand__c = 'Apple', Price__c = 200.00)
};
insert mobileToInsertList;
}
private static testMethod void test_newcustomerdiscount() {
List<Mobile__c> mobileList = [SELECT Id, Brand__c, Price__c FROM Mobile__c];
MobileShopeeClass.NewCustomerDiscount(mobileList);
Mobile__c mobileSamsung = [SELECT Discount_status__c FROM Mobile__c WHERE Customer_Name__c='Suma ' LIMIT 1];
Mobile__c mobileApple = [SELECT Discount_status__c FROM Mobile__c WHERE Customer_Name__c = 'Gopan' LIMIT 1];
system.assert(mobileSamsung.Discount_status__c.contains ('10 % Discount'));
system.assert(mobileApple.Discount_status__c.contains ('20 % Discount'));
}
private static testMethod void test_oldcustomerdiscount() {
List<Mobile__c> mobileList = [SELECT Id, Brand__c, Price__c FROM Mobile__c];
MobileShopeeClass.OldCustomerDiscount(mobileList);
Mobile__c mobileSamsung = [SELECT Discount_status__c FROM Mobile__c WHERE Customer_Name__c = 'Suma' LIMIT 1];
Mobile__c mobileApple = [SELECT Discount_status__c FROM Mobile__c WHERE Customer_Name__c = 'Gopan' Mobile' LIMIT 1];
system.assert(mobileSamsung.Discount_status__c.contains ('5% Discount'));
system.assert(mobileApple.Discount_status__c.contains ('no discount'));
}
}
Error MessageSystem.AssertException: Assertion Failed
Stack TraceClass.MobileShopeeClass_Test.test_oldcustomerdiscount: line 35, column 1
Stack TraceClass.MobileShopeeClass_Test.test_oldcustomerdiscount: line 28, column 1
public static void NewCustomerDiscount(list<Mobile__c>MobileList){
for(Mobile__c m:MobileList){
if(m.Brand__c=='Samsung'){
m.Price__c=m.Price__c-(m.Price__c*10)/100;
m.Discount_status__c='You have got 10 % Discount!!';
}
else if(m.Brand__c=='Apple')
{
m.Price__c=m.Price__c-(m.Price__c*20)/100;
m.Discount_status__c='You have got 20 % Discount!!';
}
}
}
public static void OldCustomerDiscount(list<Mobile__c>MobileList){
for(Mobile__c m:MobileList){
if(m.Brand__c=='Samsung'){
m.Price__c=m.Price__c-(m.Price__c*5)/100;
m.Discount_status__c='You have got 5% Discount!!';
}
else if(m.Brand__c=='Apple')
{
m.Discount_status__c='Sorry,no discount!!';
}
}
}
}
Thanks a lot for the help.