You need to sign in to do that
Don't have an account?
![Abhishek Ray Abhishek Ray](https://dfc-org-production.my.site.com/img/userprofile/default_profile_45_v2.png)
Apex Code Coverage is 36%. Please help
Please help. Trigger I am getting only of 36%. Please help.
trigger BusinessSegment on Opportunity (before insert) {
for(Opportunity op: Trigger.new){
if(op.Solution_Offering__c=='Audit/Compliance'&& (op.Business_Application__c=='Audit Management' ||op.Business_Application__c=='Financial Audit Management'||op.Business_Application__c=='Regulatory Audit Management') ){
op.Use_Type_Class__c='Audit/Compliance' ;
op.Use_Type__c='Financial Audit';
op.Solution__c='IL Exchange' ;
}
}
{
for(Opportunity opp: Trigger.new){
if(opp.Solution_Offering__c=='Audit/Compliance'&& opp.Business_Application__c=='Compliance Management' ){
opp.Use_Type_Class__c='Audit/Compliance' ;
opp.Use_Type__c='Other Audit/Compliance';
opp.Solution__c='Compliance Link';
}
}
}
{
for(Opportunity opp1: Trigger.new){
if(opp1.Solution_Offering__c=='Fund Management'&&
(opp1.Business_Application__c=='Investor Reporting'
|| opp1.Business_Application__c=='Portfolio Company Reporting'
|| opp1.Business_Application__c=='AI Fundraising' )){
opp1.Use_Type_Class__c='Financing/Capital Financing' ;
opp1.Use_Type__c='AI Fundraising';
opp1.Solution__c='IL Exchange';
}
}
}
{
for(Opportunity opp2: Trigger.new){
if(opp2.Business_Application__c=='None' ){
opp2.Use_Type_Class__c='General' ;
opp2.Use_Type__c='Other (Specify in Description box)';
opp2.Solution__c='IL Exchange';
}
}
}
{
for(Opportunity opp3: Trigger.new){
if(opp3.Solution_Offering__c=='Corporate Services'&&
(opp3.Business_Application__c=='Contract Management'
|| opp3.Business_Application__c=='Vendor Management'
|| opp3.Business_Application__c=='Investor Reporting'
|| opp3.Business_Application__c=='Regulatory Reporting') ){
opp3.Use_Type_Class__c='General' ;
opp3.Use_Type__c='Contract Management';
opp3.Solution__c='IL Exchange';
}
}
}
{
for(Opportunity opp4: Trigger.new){
if(opp4.Solution_Offering__c=='Strategic Transactions'&&
(opp4.Business_Application__c=='Financing/Capital Raising'
|| opp4.Business_Application__c=='IPO'
|| opp4.Business_Application__c=='JV/Alliance Management'
|| opp4.Business_Application__c=='Acquisition Mgmt (Buy-side)'
|| opp4.Business_Application__c=='Bankruptcy/Restructuring' ) ){
opp4.Use_Type_Class__c='Financing/Capital Financing' ;
opp4.Use_Type__c='IPO';
opp4.Solution__c='IL Exchange';
}
}
}
{
for(Opportunity opp5: Trigger.new){
if(opp5.Solution_Offering__c=='Strategic Transactions'&& opp5.Business_Application__c=='Licensing' ){
opp5.Use_Type_Class__c='Licensing & Strategic Alliances' ;
opp5.Use_Type__c='Licensing';
opp5.Solution__c='Compliance Link';
}
}
}
}
Apex Class:
@isTest
private class testMyBusinessSegment {
Public static testMethod void testTBusinessSegment ()
{
Opportunity opp = new Opportunity();
opp.name = 'Test - New Mapping';
opp.Type = 'Transaction - New';
opp.Region__c = 'Americas';
opp.Template__c='Blocks';
opp.StageName = 'Propose';
opp.dlDealType__c = 'Micro Deal';
opp.Solution_Offering__c= 'Audit/Compliance';
opp.Business_Application__c= 'Audit Management';
insert opp;
}
{
Opportunity oppp = new Opportunity();
oppp.name = 'Test - Trigger 1';
oppp.Type = 'Transaction - New';
oppp.Region__c = 'Americas';
oppp.Template__c='Blocks';
oppp.StageName = 'Propose';
oppp.dlDealType__c = 'Small Deal';
oppp.Solution_Offering__c= 'Audit/Compliance';
oppp.Business_Application__c= 'Compliance Management';
insert oppp;
}
{
Opportunity oppp1 = new Opportunity();
oppp1.name = 'Test - Trigger 21';
oppp1.Type = 'Transaction - New';
oppp1.Region__c = 'Americas';
oppp1.Template__c='Blocks';
oppp1.StageName = 'Propose';
oppp1.dlDealType__c = 'Small Deal';
oppp1.Solution_Offering__c= 'Fund Management';
oppp1.Business_Application__c= 'AI Fundraising';
insert oppp1;
}
}
Padding coverage is a bad idea.
You should look at WHY your coverage is low.
Is it because of test failures? If so, this trick won't work, because you still have to fix the problems.
Is it because you just don't know how to write the test? If so, you need to understand the code better. If you can't write a test method for your code, you should understand why you're having a problem with it.
Here's an optimization (you can take it or leave it) that's 20 lines shorter, easier to maintain (in theory, anyways), and achieves 100% coverage:
And here's the associated test class:
Note that I'm not running a proper "test" (you should use Asserts to make sure your results are what you expect), but it should be trivial to modify your test to do this, and it should be REALLY trivial to make changes to your logic in the future. Of course, you could have used a Custom Setting to map this data out, which would make it even simpler, but that's really beyond the scope of what I'm demonstrating here (it's less code, but more setup work).
All Answers
Add this to the end of your trigger. This is what I always do to make sure I have lots of coverage.
integer x;
x=0;
x=0;
x=0;
x=0;
x=0;
x=0;
x=0;
x=0;
x=0;
x=0;
x=0;
x=0;
x=0;
x=0;
x=0;
x=0;
x=0;
x=0;
x=0;
x=0;
x=0;
x=0;
x=0;
x=0;
x=0;
x=0;
x=0;
x=0;
x=0;
x=0;
x=0;
x=0;
x=0;
x=0;
x=0;
x=0;
x=0;
x=0;
x=0;
x=0;
Padding coverage is a bad idea.
You should look at WHY your coverage is low.
Is it because of test failures? If so, this trick won't work, because you still have to fix the problems.
Is it because you just don't know how to write the test? If so, you need to understand the code better. If you can't write a test method for your code, you should understand why you're having a problem with it.
Here's an optimization (you can take it or leave it) that's 20 lines shorter, easier to maintain (in theory, anyways), and achieves 100% coverage:
And here's the associated test class:
Note that I'm not running a proper "test" (you should use Asserts to make sure your results are what you expect), but it should be trivial to modify your test to do this, and it should be REALLY trivial to make changes to your logic in the future. Of course, you could have used a Custom Setting to map this data out, which would make it even simpler, but that's really beyond the scope of what I'm demonstrating here (it's less code, but more setup work).
SFDC Fox has a provided a viable alternative.
Please review the "Testing Best Practices" links below. Hopefully these will help you out.
http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#StartTopic=Content%2Fapex_testing_best_practices.htm|SkinName=webhelp
http://wiki.developerforce.com/page/An_Introduction_to_Apex_Code_Test_Methods
http://developer.force.com/df_session?id=a0J300000009wdyEAA
Carl,
Your programming lead won't even let you use a conditional branch to detect null pointers, so I don't see how you could possibly have the knowledge and experience to say that "collection framework generics" are the source of "all kinds of crazy problems" which you have deemed to be "impossible to fix", and yet a more advanced developer has no problem diagnosing.
Little do you know, I've spent the last three months cleaning up code that was written by someone that probably received some advice from someone just like you: "Collections are bad and impossible to use." "Pad your code so you pass test coverage" "Use try-catch blocks for everything, never handle any exceptions, and do not present meaningful errors to the user" "Do not use dynamic code, because it is too hard to understand" "Waste time coding meaningless test methods for coverage, because nobody cares about the bugs if they can't see them".
It is advice like this that causes users to come to hate a system (e.g. salesforce.com) and seek a "better solution", when really it was not salesforce.com's fault, but the fault of programmers that took on bad practices and spread them through the industry. As a long-time Technical Support, Premier Support, and Salesforce.com Developer (over six years now), I understand the importance of useful, enriching software to keep salesforce.com alive. The platform's core power is not in its out-of-the-box core features, but in the robust and rich environment that we can use to build all types of advanced enhancements.
sfdcfox is absolutely RIGHT!!!. The goal of your test class is to test the funtionality of your code and not just making code covergage. Please follow his example.
Regards
-HenryAG
Hi
Try this
@isTest
private class testMyBusinessSegment {
Public static testMethod void testTBusinessSegment ()
{
Opportunity opp = new Opportunity();
opp.name = 'Test - New Mapping';
opp.Type = 'Transaction - New';
opp.Region__c = 'Americas';
opp.Template__c='Blocks';
opp.StageName = 'Propose';
opp.dlDealType__c = 'Micro Deal';
opp.Solution_Offering__c= 'Audit/Compliance';
opp.Business_Application__c= 'Audit Management';
insert opp;
opp.Business_Application__c= 'Audit Management';
update opp;
}
Public static testMethod void testTBusinessSegment1 ()
{
Opportunity opp1 = new Opportunity();
opp1.name = 'Test - New Mapping';
opp1.Type = 'Transaction - New';
opp1.Region__c = 'Americas';
opp1.Template__c='Blocks';
opp1.StageName = 'Propose';
opp1.dlDealType__c = 'Micro Deal';
opp1.Solution_Offering__c= 'Fund Management';
opp1.Business_Application__c= 'Portfolio Company Reporting';
insert opp1;
opp1.Business_Application__c= 'None';
update opp1;
}
Public static testMethod void testTBusinessSegment2 ()
{
Opportunity opp1 = new Opportunity();
opp1.name = 'Test - New Mapping';
opp1.Type = 'Transaction - New';
opp1.Region__c = 'Americas';
opp1.Template__c='Blocks';
opp1.StageName = 'Propose';
opp1.dlDealType__c = 'Micro Deal';
opp1.Solution_Offering__c= 'Corporate Services';
opp1.Business_Application__c= 'Contract Management';
insert opp1;
opp1.Solution_Offering__c='Strategic Transactions';
opp1.Business_Application__c= 'IPO';
update opp1;
opp1.Business_Application__c= 'Licensing';
update opp1;
}}
Regards,
Rajesh.
I tried your code. It has an error: Error: Compile Error: line 59:123 no viable alternative at character '' at line 59 column 123.
What does that mean?
Hi
Change first line as @isTest(SeeAllData=True).
And also change opp1.Business_Application__c= 'None'; to opp1.Business_Application__c= None;
i.e I just removed single quotes for None.
and also I have wrote test class line after line u just modify that there should be no extra line between the two line.
Regards,
Rajesh S.
Sorry Rajesh it was not your code. Your code is achieving only 55% coverage. Any solutions
I tried your code for sfdcfox.
It has an error: Error: Compile Error: line 59:123 no viable alternative at character '' at line 59 column 123.
What does that mean? Please help.
Please help. It is urgent.