-6

// Class Code

public class SendMassMailDealsheet {
    public List < Dealsheet__c > dealsheetIdList {get;set;}
    public set < ID > dealsheetId {get;set;}
    public SendMassMailDealsheet(ApexPages.StandardSetController controller) {
        dealsheetId = new set < ID > ();
        dealsheetIdList = (List < Dealsheet__c > ) controller.getSelected();
        for (dealsheet__c deal: dealsheetIdList) {
            dealsheetId.add(deal.Id);
        }
    }
    public String Sendmail() {
        integer i;
        String retURL = ApexPages.currentPage().getParameters().get('retURL');
        List < Dealsheet__c > dealsheetList = [select ID, MailTmplate__c, Broker_s_Name__c, CreatedDate, Name, Buy_Sell__c, Counter_Party__c,
            Start_Date__c, End_Date__c, Owner.Name, Owner.Email, Broker__r.Name, Counter_Party__r.Name, CP_Trader__r.Name,
            Counter_Party__r.CP_NASEB_date__c, Counter_Party__r.CP_City__c, Counter_Party__r.Commercial_Fax__c, Counter_Party__r.CP_Address_Line1__c, Counter_Party__r.CP_Address_Line2__c,
            Counter_Party__r.CP_State__c, Broker__c, FieldBrokerMl__c, Deal_Type__c, PriceIndex_Name__c, Fixed_Price__c, Price_Diff__c, Vlm_Day__c, NG_Pipe__c, Delivery_Meter__c, Total_Volume_MT__c,
            CP_NASEB_Date__c, Dealsheet__c.CP_Fax_no__c, Trader_Comments__c, Confirmation_Email_Send__c,
            Trade_Date__c, Focus_ID__c, CP_Trader__c, Counter_Party__r.Confirms_Email__c, Dealsheet__c.CP_Address__c from dealsheet__c Where id =: dealsheetId
        ];
        for (Dealsheet__c dealsheet: dealsheetList) {
            if (dealsheet.Focus_ID__c == Null) {
                ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR, 'please enter BUContract');
                apexpages.addMessage(myMsg);
                return null;
            } else {
                if (dealsheet.Confirmation_Email_Send__c == FALSE) {
                    if (dealsheet.Price_Diff__c != null) {
                        if (dealsheet.Price_Diff__c > 0) {
                            dealsheet.resultprice__c = 'Plus $' + dealSheet.Price_Diff__c;
                            i = dealsheet.resultprice__c.length();
                            dealsheet.resultprice__c = dealsheet.resultprice__c.substring(0, i - 1);
                        } else {
                            if (dealsheet.Price_Diff__c < 0) {
                                dealsheet.resultprice__c = 'Minus $' + dealSheet.Price_Diff__c;
                                dealsheet.resultprice__c = dealsheet.resultprice__c.replace('-', '');
                                i = dealsheet.resultprice__c.length();
                                dealsheet.resultprice__c = dealsheet.resultprice__c.substring(0, i - 1);
                            } else {
                                dealsheet.resultprice__c = 'Flat';
                            }
                        }
                    } else {
                        if (dealsheet.Fixed_Price__c > 0) {
                            dealsheet.resultprice__c = 'Plus $' + dealsheet.Fixed_Price__c;
                            i = dealsheet.resultprice__c.length();
                            dealsheet.resultprice__c = dealsheet.resultprice__c.substring(0, i - 1);
                        } else {
                            if (dealsheet.Fixed_Price__c < 0) {
                                dealsheet.resultprice__c = 'Minus $' + dealsheet.Fixed_Price__c;
                                dealsheet.resultprice__c = dealsheet.resultprice__c.replace('-', '');
                                i = dealsheet.resultprice__c.length();
                                dealsheet.resultprice__c = dealsheet.resultprice__c.substring(0, i - 1);
                            } else {
                                dealsheet.resultprice__c = 'Flat';
                            }
                        }
                    }
                    dealsheet.Confirmation_Email_Send__c = TRUE;
                    dealsheet.Counter_Party_Name__c = dealsheet.Counter_Party__r.Name;
                    dealsheet.CP_Email__c = dealsheet.Counter_Party__r.Confirms_Email__c;
                    dealsheet.CP_Trader_Name__c = dealsheet.Counter_Party__r.Name;
                    dealsheet.CP_Trader_Name__c = dealsheet.CP_Trader__r.Name;
                    dealsheet.CP_NASEB_Date__c = dealsheet.Counter_Party__r.CP_NASEB_date__c;
                    dealsheet.CP_Address__c = dealsheet.Counter_Party__r.CP_City__c;
                    dealsheet.CP_Address_Line1__c = dealsheet.Counter_Party__r.CP_Address_Line1__c;
                    dealsheet.CP_Address_Line2__c = dealsheet.Counter_Party__r.CP_Address_Line2__c;
                    dealsheet.CP_State__c = dealsheet.Counter_Party__r.CP_State__c;
                    //  dealsheet.CP_P_O_CODE__c=dealsheet.Counter_Party__r.CP_P_O_CODE__c; 
                    dealsheet.CP_Fax_no__c = dealsheet.Counter_Party__r.Commercial_Fax__c;
                    dealsheet.Broker_s_Name__c = dealsheet.Broker__r.Name;
                    Update dealsheet;
                    Contact recipient = new contact(LastName = 'Test', email = dealsheet.Counter_Party__r.Confirms_Email__c);
                    insert recipient;
                    sendEmail(recipient.ID, dealsheet.id);
                    delete recipient;
                    dealsheet.Confirmation_Email_Send__c = TRUE;
                }
            }
        }
        return retURL;
    }
    public static void sendEmail(ID recipient, ID dealsheet) {
        //New instance of a single email message
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        // Who you are sending the email to
        mail.setTargetObjectId(recipient);
        // The email template ID used for the email
        mail.setTemplateId('00X37000000Yi1U');
        mail.setWhatId(dealsheet);
        mail.setBccSender(false);
        String[] toaddress = new String[] {
            '[email protected]'
        };
        mail.setCcAddresses(toaddress);
        mail.setUseSignature(false);
        mail.setSaveAsActivity(false);
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] {
            mail
        });
    }
}

// Test Code

@isTest
public class SendMassMailDealsheetTest {
    public static testMethod void testMyController() {
        CP__c testCP = New CP__c();
        testcp.name = 'testCp';
        testcp.Confirms_Email__c = '[email protected]';
        insert testCp;
        Meter__c m = new Meter__c();
        m.name = 'test';
        insert m;
        NG_Pipe__c ng = new NG_Pipe__c();
        ng.name = 'testpipe';
        insert ng;
        CP_Trader__c testTrader = new CP_Trader__c();
        testTrader.name = 'testTrador';
        testTrader.Cp__C = testcp.id;
        Dealsheet__c testDeal = new Dealsheet__c();
        testdeal.Buy_Sell__c = 'Buy';
        testDeal.Counter_Party__c = testCP.id;
        testDeal.CP_Trader__c = testTrader.id;
        testDeal.Start_Date__c = date.today();
        testDeal.End_Date__c = date.today();
        testDeal.Vlm_Day__c = 20;
        testDeal.RadioBroker__c = 'Direct';
        testDeal.Price_Type__c = 'Fixed';
        testDeal.Trade_Date__c = date.today();
        testDeal.NG_Pipe__c = ng.id;
        testDeal.Delivery_Meter__c = m.id;
        testDeal.Price_Diff__c = Null;
        testDeal.resultprice__c = Null;
        insert testdeal;
        List < dealsheet__c > dealsheetList = NEW List < dealsheet__c > ();
        dealsheetList.ADD(TESTDEAL);
        ApexPages.StandardSetController sc = new ApexPages.StandardSetController(dealsheetList);
        ApexPages.currentPage().getParameters().put('id', testdeal.id);
        PageReference pageRef = Page.SendMassMailDealsheet;
        //pageRef = sc.view(dealsheetList);

        sc.setSelected(DealsheetList);
        SendMassMailDealsheet controller = new SendMassMailDealsheet(sc);

        controller.Sendmail();
        // Add parameters to page URL
        Test.setCurrentPage(pageRef);

        pageRef = Page.SendMassMailDealsheet;
        ApexPages.currentPage().getParameters().put('id', testdeal.id);
        ApexPages.currentPage().getParameters().put('retURL', testdeal.id);
        controller.Sendmail();

        Test.setCurrentPage(pageRef);
        // Instantiate a new controller with all parameters in the page
        controller = new SendMassMailDealsheet(sc);
    }
}

i am using That Test Code But Its giving only 24%.. Give me suggestions plz

2
  • 3
    Srihari .. posting a wall of text is really not a good approach to getting help. If you're not sure how to test this code there are probably different aspects of this that you need to learn and ask us about. How about you clean up this code and focus on the first method or piece of code that is not covered by your test ? Do you know how to see what code is covered and what is not ? May 27, 2016 at 13:08
  • 3
    So .. tell us. It would really help us if you update your question and provide more specific information on where you need help. Right now your question is too broad. May 27, 2016 at 13:18

2 Answers 2

4

Some suggestions as you asked:

  • Use Test.startTest() and Test.stopTest() methods
  • It might be useful to move the setup part into another method
  • Make sure to test EVERY POSSIBLE SCENARIO! You can and should NOT test everything in one test method(or even worse: get coverage only)

And, probably the most important: USE ASSERTS WHEN WRITING TEST CLASSES!!!
Test classes are called test classes because they should actually test something and not only provide coverage, otherwise we would call them coverage classes. A test class without assert is useless, doesn't test anything and should not even be called a test class.

And to get more coverage make sure that you set the value to Focus_ID__c somewhere, because now it is null which skips a huge part of your code.

1

You can use the developer console to see which lines are not covered by your current tests, this will show you the 76% of lines which aren't being executed.

Then write test conditions to cover those lines.

To get 100% coverage you need to go through each possible path of code.

You should also put in asserts based on business logic to prove the code is working as expected.

5
  • if(dealsheet.Price_Diff__c>0){ dealsheet.resultprice__c='Plus $'+dealSheet.Price_Diff__c;i= dealsheet.resultprice__c.length(); dealsheet.resultprice__c = dealsheet.resultprice__c.substring(0,i-1); }else{if (dealsheet.Price_Diff__c<0){ dealsheet.resultprice__c='Minus $'+dealSheet.Price_Diff__c; dealsheet.resultprice__c = dealsheet.resultprice__c.replace('-',''); i= dealsheet.resultprice__c.length(); dealsheet.resultprice__c = dealsheet.resultprice__c.substring(0,i-1); } else{dealsheet.resultprice__c='Flat'; } } con every tthing working now i am got 71% except that lines
    – srihari
    May 27, 2016 at 9:11
  • after else and one more if iam gettiing errors
    – srihari
    May 27, 2016 at 9:12
  • By errors do you mean that the code won't save/compile or that it doesn't perform how you are expecting?
    – Doug B
    May 27, 2016 at 9:41
  • giving red color
    – srihari
    May 27, 2016 at 11:41
  • "Giving red color" is not a very helpful description of an error condition.
    – Doug B
    May 27, 2016 at 13:38

Not the answer you're looking for? Browse other questions tagged .