Questions tagged [unit-test]

A unit test is code that is written and executed to test a specific portion/unit of source code. Unit tests are class methods with the testMethod modifier or @isTest annotation and are usually placed in separate test classes annotated with the @isTest annotation. Code that is executed by unit tests is considered covered. At least 75% of an organization's codebase must be covered by unit tests and every trigger must have at least 1% unit test coverage.

Filter by
Sorted by
Tagged with
35
votes
1answer
27k views

Writing Test Classes for Apex RestService

I was writing test classes for my apex Rest Service that I had built for an Ipad app. I noted some very interesting things while experimenting. Before doing anything, I just did a google search and ...
1
vote
1answer
1k views

Getting Test failure for page parameters

Getting error message when I'm running the test don't know what's wrong with this code this is the error msg: System.StringException: Invalid id: recordtype.id Stack Trace: Class.LeadEventCreation....
-1
votes
1answer
614 views

Test Coverage For Page parameters is not covering

public with sharing class LeadEventCreation { public PageReference doSave() { insert objL; objL.recordTypeId = apexpages.currentPage().getparameters().get('id');// these lines are ...
2
votes
3answers
484 views

Geolocation field, inconsistent unit test results IDE vs. UI

I'm using the new Geolocation field data type and SOQL DISTANCE function in a Visualforce page. I'm using the SOQL with DISTANCE to retrieve records into a standardSetController. It works great - ...
1
vote
2answers
1k views

How to write test for following apex code

How to write test for following part of apex code? I am fairly new to sfdc development. public with sharing class ddDashboard { public class phonesterDat{ String type; Integer total; Integer ...
4
votes
1answer
724 views

Why does my assertion not fail when I call it after stopTest()?

I have opportunity code that calls a future method. While testing it, I have run into some odd behaviour. I cannot get my assertion to fail if it occurs after the stopTest() call. In fact, it seems ...
0
votes
1answer
818 views

Test Coverage 93% how to Make this code cover to 100% [closed]

public class TriggerClass { public void Mycodecoverage(Branch__c Mycodecoverage) { string BankId = Mycodecoverage.Project_Bank__c; List<Bank__c> bank = [select id,project__c,project__r.name,...
7
votes
2answers
162 views

How do you create an object by a specific user in a unit test?

I have a trigger that examines the creator of a Case object and executes code if it matches a user in a specific list. I need to unit test this code, and so I need to create a Case object, but have ...
21
votes
2answers
4k views

Can't get Test.loadData method to run

I cannot get past various system exceptions trying to use the loadData method. It seems to be something with the csv file; changing it alters the cause of failure. I am using data from production ...
20
votes
2answers
5k views

WebServiceMock causing "CalloutException: You have uncommitted work pending" when creating managed package

I have a number of test methods that call Test.setMock(WebServiceMock.class, new SomeWebServiceMockImpl()); where SomeWebServiceMockImpl implements WebServiceMock. These test methods pass when I run ...
4
votes
2answers
309 views

Testing unwriteable fields

I am trying to write tests for code that depends on the Status picklist field of EmailMessage objects. The Status field is unwriteable. For testing purposes, I have been able to create an '...
8
votes
1answer
2k views

Why are code coverage results different in IDE and developer console?

The Apex coverage results for unit tests in the Force.com IDE and the developer console are very different. I just ran 2 tests in both and got: test 1 IDE: 75 lines not tests, 68% covered Developer ...
8
votes
2answers
4k views

Test future dates

Struggling with how best to test my controller extension - My extension onload sets a picklist value based on the month number. My issue is I can only test current month e.g. Nov. If I explicitly set ...
47
votes
6answers
16k views

Is it possible to test apex that relies on field history tracking?

I have some code that relies on field history tracking by querying the History for a custom object. This works fine, but it appears that the history records aren't created during the test so none of ...
7
votes
2answers
2k views

test method always fails. why isn't the soql returning any records?

I've been running a chatter tutorial and ran across something I don't understand. First off here's the link to the tutorial . The following code is straight out of the tutorial and works, except for ...
23
votes
8answers
4k views

Faster unit tests?

I'm running unit tests in Eclipse (Force.com IDE) and they are taking 5min+ to execute. Is there an alternative way to run them, or are there tips to speeding this up? I know I can run the tests from ...
1
vote
2answers
666 views

Failure in pkb_Controller when running the unit tests on SalesForce

We are attempting to deploy some code into a production SalesForce instance. But deployement fail because already installed third party packages are failing the unit test requirements. We get the ...
10
votes
1answer
2k views

Unit-Testing Sites Permission Involving a Sharing Rule with Unit-Test Created Data

We currently have a Salesforce Site, and we're trying to add some unit-tests, around permissions and sharing rules that need to be set. We've smoke tested everything via the UI, and permissions appear ...
13
votes
2answers
13k views

How to access custom settings in test class without using the seealldata?

I want to use a custom settings inside a test class, without using the SeeAllData=true What are the options I have? How can I accomplish it with a reasonable solution?
35
votes
6answers
46k views

Testing HttpCallout with HttpCalloutMock and UnitTest Created Data

We have a callout to an external webservice that requires retrieving some data from the database via SOQL and then issuing an HTTP GET request, and performing various logic based upon the response. ...
10
votes
3answers
11k views

1.how to stop run all tests? 2.validation running for about 6 hours in production. how to stop it?

How to stop run all tests? I have clicked run all tests in my sandbox and usually gets completed in about 15 minutes. Now its running for about an hour and still not completed? Note :- Mine is a ...
7
votes
3answers
7k views

is there a possibility for Run all tests (in target sandbox) before deployment?

is there a possibility for Run all tests (in target sandbox) before deployment from my dev sandbox? If so how to do that? I have about 700 components in my changeset and its successfully validated. ...
3
votes
2answers
3k views

System.ListException: Missing ID at index 0

I am trying to add code coverage for the method below and keep getting this error. Basically it's a link that deletes the attachment on a table. The method is called from commandlink that passes the ...
1
vote
2answers
2k views

Test Case Failing b/c of Validation Rule, But Should Be Passing Based on Debug Log

My test case is inserting and updating records that should be meeting the criteria of the following validation rule, but the test says that it is failing: Validation Rule: AND( OR( ISPICKVAL(...
4
votes
1answer
1k views

Test case fails in eclipse, same test case succeeds in webinterface. Click-through also succeeds

We have a rather elaborate setup where we have partner portal users approve/reject account-records using a visualforce page. We've got a test case doing this whole flow and it succeeds when you run ...
3
votes
3answers
1k views

Test Visualforce Page With Over a Thousand Select Options

I have a custom visual force page which is throwing the error - I have found the line which causes the error and know how to fix, but just need to write a test class which will recreate the error ...
1
vote
1answer
642 views

Help with a test for a trigger

The code coverage is good, and the trigger works when testing it manually, however, my system.assertequals statements are failing. the finAcc1.wealth_advisor_lookup__c (and the others) is null. Any ...
5
votes
3answers
7k views

Testing WebServiceCallout with HttpCalloutMock

I am trying to use the new HttpCalloutMock following the guide here. This is a new feature, and I was hoping to make some better test cases for using web-service callouts that respond with a SOAP ...
3
votes
1answer
1k views

Code Coverage question

I saw for the first time in one of my orgs where there is a 29% (61/208) what does that number in the brackets say ? I have never seen it in any other org. Is this a new release for winter 13 or has ...
1
vote
3answers
9k views

Why is my reference field returning null my test code?

I am trying to test some code that is working in the sandbox. The code, shown below, uses a reference to obtain the BusinessHoursId field of the Milestone's Case object (m.Case__r.BusinessHoursId): ...
4
votes
1answer
1k views

Chatter Answers Test Failures - Invalid field CommunityId for sObject Case

After winter '13 when deploying to production I'm getting a failure in ChatterAnswersEscalationTriggerTest that communityId is not a valid case field. Any ideas why this field would spontaneously ...
3
votes
3answers
3k views

Deployment of managed package

I have a managed package which is going to be installed in several enterprise orgs. Salesforce while deployment all unit tests will be run and if they all success with 75% code coverage then only you ...
13
votes
2answers
6k views

How can I get code coverage programatically through Apex?

I've looked at the Automated Unit test Execution Recipe and see that it will email the results of running unit tests. I do not see anything in it that gives code coverage results, though. I'd like ...
11
votes
2answers
2k views

How do you Apex Unit Test Object(CRUD)/Field(FLS) Permissions in an Org Portable manner?

Specifically, how might one get an org portable Apex Unit Test User that DOES NOT have READ access to Accounts? Creating a Test Permission Set that DOES NOT have READ access to ACCOUNTS and assigning ...
5
votes
1answer
557 views

test class for triggers on standard object which will have a good coverage independant of the environment

I need to write a test class for trigger on a standard object. opportunity. The plan is package the trigger and test class as a free app so users can re-use the component. As we all know the test ...
3
votes
1answer
2k views

How can I create a Chatter Free License user in a test class?

I am trying to create a user in a test class and this user needs to be a Chatter Free License. Usertype is the field to set the license but this one is not writable. Any pointers on creating a user ...
2
votes
1answer
2k views

Forcing a test method to fail if a web service callout is attempted

When an apex test method attempts to make a web service callout something like the following is logged: 23:11:45.288 (4288851000)|EXCEPTION_THROWN|[116]|System.TypeException: Methods defined as ...
8
votes
7answers
5k views

Why are my Test Classes (marked @isTest) getting listed with Code Coverage?

For some reason, two (out of ten) of my test classes are showing up in the Class Code Coverage section when I run all Apex tests. They also have a Code Coverage percentage on my Apex class list. The ...
3
votes
1answer
2k views

how to create a opportunity converted from lead in test class

I am writing a test class and need to create test data on an opportunity converted from a lead. I can create a lead in test class Lead testLead = new Lead(); testLead.FirstName = 'Test First'; ...
1
vote
1answer
2k views

Test coverage for setCookies method

I am adding cookie to my page in the controller using ApexPages.currentPage().setCookies(new Cookie[]{CookieName}); When am trying to get the code coverage, my test class is not covering the above ...
33
votes
8answers
14k views

Use of @IsTest versus testMethod and location of test methods in classes

Is there any advantage to using the older testMethod keyword to identify test methods or is it better to use the @IsTest annotation? Does the location of these test methods have any significance? E.g....
38
votes
8answers
5k views

Is there a way to setup continous integration for apex tests?

I would like to have continous integration (with Jenkins) for my apex code in order to run them automatically after each push to my git repository. Does anyone know a way how to do that?
11
votes
3answers
5k views

Salesforce Trigger - "DML not allowed on RecordType"

I have created a trigger which is dependant on a custom record type existing. In my test for the trigger, I want to ensure that when this record type doesn't exist the trigger throws the appropriate ...
4
votes
2answers
2k views

Test class for Visual Workflow

I have a flow, which shows a list of Opportunities, one by one based on score. This flow has been referenced in a Visualforce page: <apex:page controller="OppPriorityController"> <h1&...
7
votes
4answers
3k views

Run anonymous apex as if it were a test case

Is there any way to run anonymous apex so that it is treated the same way as a unit test? Sometimes it would be ideal to just try some code out and know that any DML operations that get performed won'...
10
votes
6answers
2k views

How do you unit test a trigger when you don't know the required fields?

This is another issue addressed largely towards app developers. Let's say I write an insert trigger. When I write the test case for it, what I would normally do is create a new object, insert it, and ...
15
votes
2answers
5k views

How do you write a unit test for a trigger whose only function is to make a callout?

What is considered a best practice for writing a unit test (to maintain code coverage) on a trigger whose sole function is making a callout (Which also means it has to use a future method)? Neither ...
10
votes
2answers
1k views

Unit test SOQL limit setting up data model

I have run in to this situation where the data model is very complex and since SF doesn't support mocks (e.g., Mockito) there are so many SOQL executions in just setting up the data model (triggers, ...
4
votes
1answer
2k views

Assignment rules don't run in test class

I am writing a test class for cases. I want to test certain things of course and want to rely on the actual behaviour. Therefore I would like assignment rules to run when inserting test cases. They ...
28
votes
3answers
19k views

How should I build test methods for Visualforce Controller Extensions

I would like to know the best way to write test methods for controller extension classes that are used by Visualforce. What is the minimum set code that I need to cover the basic operation of the ...