[SalesForce] Apex class won’t save for lack of test coverage

I have been using Force.com IDE to work on VisualForce pages and classes directly in a live development environment. I was unaware of the requirement that 75% of your code had to be covered by tests. Suddenly today, when I wanted to change one line of code in one of my classes, the file fails to save with this error message:

Average test coverage across all Apex Classes and Triggers is 15%, at least 75% test coverage is required.

So I wrote some test methods to get the coverage for the class I am working on to 78%, but it still won't save. Catch 22?

What shall I do?

Update:

I am trying to improve my code coverage by adding tests in my sandbox org and then deploying that to the development org via change sets. For example:

  • I take a class that has no coverage
  • write some test methods that covers the code more than 75%
  • make a new change set and add the class
  • upload the change set to my development org
  • when I try to deploy the change set in my dev org, the deployment fails with

Deploy Error | Average test coverage across all Apex Classes and
Triggers is 15%, at least 75% test coverage is required.

Also trying to deploy one class via Force.com IDE fails with the same method. Does this mean that I can't fix and deploy class by class?

Best Answer

If you are doing development in your live production environment, I suggest changing to a sandbox org first. Create a sandbox, configure the IDE to connect to the sandbox, and make all your code changes there. You can run tests there to see how good your coverage is. Once you are at 75% for the org (bare minimum), deploy your new code to production and it should succeed.

Developing in production is not impossible, but it's definitely not best practice. I learned the hard way that it's not worth it in the long run, even if it seems like it's saving you time right now.