-5
public class testexample5 {
    public string  aname{get;set;}
    public string aphone{get;set;}
    public string aindustry{get;set;}
    public void create(){
        integer count = [select  count() from account where name=:aname];
        if(count>0){
            apexpages.Message msg = new apexpages.Message(apexpages.Severity.ERROR,'Duplicate record exists');
            apexpages.addMessage(msg);
        }else{
            account acc = new account();
            acc.name=aname;
            acc.Phone=aphone;
            acc.Industry=aindustry;
            insert acc;

        }
    }
}

I wrote the test class it is giving 84%. How can I cover the following line?

apexpages.Message(apexpages.Severity.ERROR,'Duplicate record exists');
        apexpages.addMessage(msg);?

my test class is

@istest
private class testclassexample5 {
    testmethod static void test1(){
        testexample5 te5  = new testexample5();
        te5.create();
        te5.aname = 'hhhh';
        te5.aphone = '7587578585';
        te5.aindustry='Banking';
    }
}

2 Answers 2

2

Please note from How to Write Good Unit Tests (emphasis mine):

Verify the results are correct

Verifying that your code works as you expect it to work is the most important part of unit testing. It’s also one of the things that Force.com developers commonly neglect. Unit tests that do not verify the results of the code aren’t true unit tests. They are commonly referred to as smoke tests, which aren’t nearly as effective or informative as true unit tests.

A good way to tell if unit tests are properly verifying results is to look for liberal use of the System.assert() methods. If there aren’t any System.assert() method calls, then the tests aren’t verifying results properly. And, no, System.assert(true); doesn’t count.

Memorize this and live by it. The primary objective of your unit test is to verify application behavior.


You test it like you would any other code. Set up data such that your conditions are satisfied. For example:

// set up data matching the condition
String name = 'Some Account Name';
insert new Account(Name=name); // populate additional required fields as needed

Test.startTest();
    // execute your code path
    MyClass instance = new MyClass();
    instance.aName = name;
    instance.create();
    Integer dmlStatements = Limits.getDmlStatements();
Test.stopTest();

system.assert(ApexPages.hasMessages(),
    'A message should be added to the page');
system.assertEquals(0, dmlStatements,
    'No insert should be attempted if a record already exists with the specified name');
2
  • It is not showing 100% code coverage.
    – siva
    Commented Oct 12, 2017 at 21:39
  • 1
    You need two separate test methods, one per execution path...
    – Adrian Larson
    Commented Oct 12, 2017 at 21:40
-1

Please try this piece of code.It covers 100% code coverage. Hope It will help you.

@isTest
public class testclassexample5{
    testmethod static void test(){
        Account acc = new Account();
        acc.Name = 'testAcc';
        insert acc;
        testexample5  te5  = new testexample5();
        te5.aname = 'testAcc';
        te5.create();
    }
    testmethod static void test1(){
        Account acc = new Account();
        acc.Name = 'testAcc1';
        insert acc;
        testexample5  te5  = new testexample5();
        te5.aname = 'testAccDummy';
        te5.create();
    }
}
1
  • This is not a test method. It only tests that an error is not thrown
    – Eric
    Commented Oct 13, 2017 at 8:25

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