1

I want to upload my application on appExchange. Now I want to know about all steps to do my task. Here I noticed that we have to write test cases for the code to check its coverage. And atleast 70% code coverage is necessary to uplaod an app on appExchange. How can we make test cases and on which area of code we have to make test cases, and what is the process of getting code coverage upto 70% of the whole code. Also differentiate between test class and triggers. Thanks. Any help will be appreciated.

2

This is a pretty broad question, but I'll try to break it down.

How can we make test cases

Test cases are nothing more than Apex classes. Take a look at this documentation for an introduction. Think of Test Classes as a programmatic way of testing use cases.

For instance, if you have a button that calls a certain function, you'd want to call that in your code and then check whether or not the outcome is as expected.

As a mundane example, if you have a piece of code like the following:

public Integer add(Integer x, Integer y) {
    return x + y;
}

You'd want to call that function and assert that the two parameters you pass in return the expected output. If x = 2 and y = 3, you know the answer is 5, you'd want to assert that you actually get 5. If you don't, you have a problem.

and on which area of code we have to make test cases

All of it. Salesforce's minimum requirement is 75%, but they advise 100% and rightly so. The reason for this is that you want to ensure that every line of code, under different use cases, works as expected. Sometimes, this isn't always possible. But aim for it as best as you possibly can.

what is the process of getting code coverage upto 70% of the whole code

Literally what I mentioned above! Programatically testing what you've written!

Also differentiate between test class and triggers

I think you might be a little confused here as Triggers and Test Classes aren't really equivalent unless I'm misinterpreting what you're asking. Test classes are designed for what I've explained above, Triggers fire on DML. Triggers should also be covered by a test class which can usually be pretty easily achieved by simulating data being inserted/updated/deleted/undeleted from the database.

It's what happens when that trigger is fired, and again, checking that when it is fired, the system and your code does what you expect.

I appreciate this is quite a simplistic answer, but it's a really, really broad question. There's tonnes of material on unit testing and although it takes a little bit of time getting your head around it, it's incredibly important! Imagine pushing out an update to your app, forgetting that something you changed has a knock on effect to something you hadn't considered and then mistakenly breaking the entire thing. Test classes are there to help prevent that.

  • Yeah, your answer is very helpful Poet :-) And I know, I have to write test cases for the whole code of my application to get atleast 70% coverage. Do the Visualforce code is also included? I think it will not be included in code coverage percentage. but Till now I am confused on how to make test cases. Any ways I read the documentation thoroughly and try to grasp all the information provided on that page, then will discuss here if there will be some problem.. Thanks again. :) – Khawaja M. Awais Dec 21 '15 at 13:48
  • One more thing , please tell me the difference between test classes and unit tests. Unit test is a test written to test a method. and test classes may contain many methods. So we can say, there may be many unit tests in a test class. Am I right? – Khawaja M. Awais Dec 21 '15 at 13:51
  • @KhawajaM.Awais Visualforce isn't necessary for Test Classes. :) Just Apex Classes and Triggers. It does take a little getting used to, going forward you might want to look at Test-Driven development where you do your testing as you develop your classes. Sometimes I hear unit tests and test classes used interchangeably. But yes, you could have many unit tests in a single test class. :) – Dan Jones Dec 21 '15 at 13:52
  • Thanks for quick response and making me clear about points. :) – Khawaja M. Awais Dec 21 '15 at 13:57
  • @KhawajaM.Awais No problem. If you think my answer is sufficient for now, please consider marking it as correct. Any future problems/questions you should ask in a new question. :) – Dan Jones Dec 21 '15 at 13:58
0

Unit Testing a SOQL Method

The link above provides you a good example of Unit Testing and information about test classes.

And the difference between Test Class and Triggers is: Test Class is used to Test the Class code with all possible scenarios; whereas Trigger is used to write functionality when certain condition triggers. e.g. You may want to write a feature after inserting account record or you may want to check some condition before inserting that record.

Hope it helps.!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.