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
1
vote
1answer
3k views

Test Class for Xml Dom Document & WebServices

My Apex class public with sharing class helpTextClass { public string main(string url) { Http h = new Http(); HttpRequest req = new HttpRequest(); req....
4
votes
1answer
263 views

Is a test method isolated from effects of other test methods

Suppose I have test class TestClassName with two test methods: @isTest private class TestClassName { static testMethod void testMethodName() { // DML to insert data } static ...
14
votes
2answers
26k views

Where can I read the debug output of test classes

Suppose I have a test class TestClassName. @isTest private class TestClassName { static testMethod void testMethodName() { System.debug('My debug statement'); System.assertEquals(1, 1)...
4
votes
1answer
8k views

No more than one executeBatch can be called from within a testmethod

I am getting following error System.UnexpectedException: No more than one executeBatch can be called from within a testmethod. Please make sure the iterable returned from your start method matches ...
13
votes
3answers
9k views

How to reduce the test class execution time while deploying?

I have optimized almost all my test classes but the "Run All Tests" execution time is still not reduced. I ran the tests in my full copy sandbox and it took 28 minutes. There are about 464 test ...
5
votes
2answers
2k views

Help with test coverage

I was wondering if someone would be able to help me create a test class for the following code as I am programmatically challenged :) any help would be greatly appreciated!!! Thanks in advance!!! ...
-4
votes
1answer
2k views

Testing batch apex [closed]

Help me in writingTest class for Batch apex I created Batch apex to send emails to owners of the lead based on certain conditions. I have to write a test class to test gov limits. Thanks in advance
4
votes
1answer
114 views

Directly attributable code coverage?

Since the last release, when I execute a single unit test (via Develop > Apex Classes > Test Class > Run Test), I get taken to the "Apex Test Execution" screen. This screen shows whether the test ...
3
votes
1answer
12k views

REQUIRED_FIELD_MISSING, Required fields are missing: [Name]: [Name]

when i trying to insert opportunity in my testclass i am getting an error Test_opportunityTrigger.Test_opportunityTrigger: line 24, column 1 22:28:17.312 (312087000)|FATAL_ERROR|System....
17
votes
5answers
11k views

UNABLE_TO_LOCK_ROW error while running all apex test classes

In spring13, I am getting UNABLE_TO_LOCK_ROW error in my few test classes while running all test classes in new view provided in spring13 release. But in winter12 I don't get any error like this. Do I ...
6
votes
1answer
845 views

Spring 13 Asynchronous Test Execution & Standard Pricebook

I'm facing a very annoying issue with the new Spring 13 release. Now all tests run asynchronously by default. Which would be fine except for the fact that a test locks all records it's using until it ...
2
votes
3answers
2k views

Where is the documentation for the Page keyword?

In the documentation for testing controllers and extensions, there's a statement that references a Page object, but I've been unable to find where this is located elsewhere in the documentation. Where ...
15
votes
3answers
1k views

How can I speed up Developer Console?

Since Salesforce have moved their test execution to be asynchronous in Spring '13 we are forced to use the Developer Console to see Code Coverage for a single test class. My issue with this is that ...
1
vote
3answers
5k views

INVALID_CROSS_REFERENCE_KEY, Can not select a person account: [AccountId]

My test class for inserting contact is static testmethod void invoicetest(){ string strrecord = [select id from RecordType where SobjectType='Account' AND IsPersonType=True limit 1].id; ...
4
votes
1answer
2k views

Weird issue with inner class not being covered via Test Class?

I have a test class that is basically covering a class which has structure like public class classA { public class classB implements Triggers.Handler { public void handle(){ ---...
0
votes
1answer
241 views

SOQL query produces different result with same input

In the process of writing a unit test on Opportunities, I'm inserting a custom object Journal_c. This custom object, Journal_c, has an after insert trigger associated with it. When running the test, I ...
0
votes
2answers
2k views

how to cover Id!=null part in test Class

public class democlass{ public string invoiceid{get;set;} PUBLIC DEMOCLASS(apexpages.standardcontroller controller){ invoiceid=apexpages.currentpage().getparameters().get('id'); **if(invoiceid!=...
0
votes
1answer
3k views

How to query CodeCoverage from SOQL? [duplicate]

Possible Duplicate: How can I get code coverage programatically through Apex? How to query CodeCoverage of a class by writing an SOQL inside Apexclass? Is there an sobject which stores the ...
27
votes
2answers
9k views

How to check if any error has been added to a record?

I have created a method which is being called through a trigger. I am adding error to a record if it is a duplicate one using 'addError()' method . Now , I want to cover this method in my test class. ...
6
votes
1answer
3k views

How to identify classes covered in test class in spring 13 release?

Friends, in spring 13 release, Salesforce has removed the test class which shows what are all the classes covered for a test class, if we run that particular test class alone and now everything goes ...
1
vote
1answer
4k views

System.ListException: List index out of bounds: 0 in test Class

My apex class is tempinvoiceid = apexpages.currentpage().getparameters().get('tempinvoiceid'); tempin = [select name,Invoice_contact__c,First_Name__c,State__c,Invoice_policy__c,Invoice_Check__c,...
2
votes
2answers
2k views

Apex Unit Tests - Approach!

Hello Folks, I think all of my questions cover the approach more than the technical detail or aspect. Here is another one, this one relates to force.com unit tests!** I know there have been ...
0
votes
1answer
966 views

“Illegal assignment” from SOQL query in unit test

In writing a unit test, I have to insert a Journal_c record. Instead of using Ids for several lookup fields, I've run soql queries assuming that the record Ids maybe different from test to production....
0
votes
1answer
1k views

Insert Error during unit test on Opportunity trigger

I'm running tests on a trigger I've created and can't figure out why the following error is being produced. Error: System.DmlException: Insert failed. First exception on row 0; first error: ...
10
votes
1answer
720 views

Testing callout with zipped static resource

How can I access the content of a static resource zip file to create several callout mocks? I'd like to group all service responses in a single zip file without create several static resources. In ...
2
votes
2answers
6k views

Passing parameters to Extension Controller for test

I have a Extension Controller which gets back a different set of Opportunities based on the value of HTTP parameter named display sent in. So my code is as such: public OpportunityListExtension(...
3
votes
2answers
2k views

Set variable in Extension before Constructor is called

If anyone has a better idea for a subject - feel free to update. So I'm setting a private variable in my constructor. I also have some logic in place to set that variable. I want to test both ...
2
votes
0answers
455 views

Invalid sObject type 'DataDotComEntitySetting'

We have Data.com, namespace jigsaw_clean, v 2.16.2 installed and I am encountering an sObject type error when I try to do a simple DML statement in a unit test script. The test method instantiates a ...
2
votes
1answer
2k views

Test data fields return null in class but are populated in unit test

I have a Visualforce page that is using a Controller extension. The extension's SOQL query is not returning the data that I expect when I run my unit tests, although it works inside of our sandbox. ...
5
votes
2answers
2k views

Required fields are missing:

I am writing a test class. When i run my test class i am getting an error System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are ...
0
votes
1answer
1k views

Code Coverage on Triggers - color coding

This is probably overly obvious, but I do not wish to assume, and I am finding no reference anywhere: What does the COLOR CODING when viewing percentage coverage on a Trigger mean? Some code is ...
3
votes
2answers
10k views

Code coverage for Classes with only final static string Variables

I have a class which contains only public static final strings how do i write a test class to cover this code? public class SFA_MobileConstants { public static final string ERROR='ERROR'; public ...
0
votes
1answer
137 views

Getting Code Coverage on a Class That Should Show Related Objects

I have the following code: http://codepad.org/AavgIrHB and Salesforce says that with my two test cases I only have 42% coverage. I feel I should have more, but it's not showing the latter part of the ...
13
votes
1answer
10k views

How to Create Profile in test method?

I developed my code in my DE and created a custom profile named "XYZ". I used that profile in my Apex class and test methods. While installing that managed beta package in my sandbox my test method ...
8
votes
2answers
3k views

Are rollup fields accessible in test methods?

When I create an object and some associated child objects in a test method, the rollup fields on the parent are always null. Is there some common approach to handling this? I think the same issue is ...
1
vote
1answer
750 views

When does an activity get logged in openactivities or ActivityHistory?

In Test class I am trying to create an event and see this event is getting logged in Open Activities or Activity History Account a = new Account(name ='Test Account'); insert a; Task t = ...
0
votes
1answer
916 views

Can/should we use assert statement in a test class for batch processes?

I have a Batch process which updates a field on Account. I have also written a test class for it. I have an assert statement to check if the field is getting updated. Unfortunately this assert fails,...
35
votes
1answer
25k 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 very interesting things while experimenting.Before anything i just did a google and found an interesting ...
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
450 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
489 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
817 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
153 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
1answer
3k 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 ...
18
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
287 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
3k 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 ...