I have the case of an object having two lookup relationship and I call for a list containing both relationship as follows. This is my Setup code

public static List<Job_Application__c> createApplications(){
        List<Job_Application__c> testApplications = new List<Job_Application__c>();
        List<Position__c> testPositions3 = new List<Position__c>();
        List<Candidate_Name__c> testCandidates = new List<Candidate_Name__c>();      
        for(Integer i=0; i<10; i++){
            testPositions3.add(new Position__c(Name = 'Test'+ i, Status__c = 'Open - Approved'));
            testCandidates.add(new Candidate_Name__c(First_Name__c ='Bob', Last_Name__c = 'Smith'+i));
        insert testPositions3;
        insert testCandidates;
        for(Integer i=0; i<10; i++){
            testApplications.add(new Job_Application__c(Candidate__c=testCandidates[i].Id, 
        return testApplications;

    public static List<Review__c> createReviews(){
        List<Position__c> testPositions4 = new List<Position__c>();
        List<Job_Application__c> testApplications2 = new List<Job_Application__c>();
        List<Candidate_Name__c> testCandidates2 = new List<Candidate_Name__c>();
        List<Review__c> testReviews = new List<Review__c>();

        for(Integer i=0; i<10; i++){
            testPositions4.add(new Position__c(Name = 'Test'+ i, Status__c = 'Open - Approved'));
            testCandidates2.add(new Candidate_Name__c(First_Name__c ='Bob'+i, Last_Name__c = 'Smith'+i));

        insert testPositions4;
        insert testCandidates2;

        for(Integer i=0; i<10; i++){
            testApplications2.add(new Job_Application__c(Candidate__c=testCandidates2[i].Id, 
        insert testApplications2;

        for(Integer i=0; i<10; i++){
            testReviews.add(new Review__c(Job_Review__c=testApplications2[i].Id,Rating__c = 1));
        return testReviews;

So when I run my test I say I'm expecting 10 Job_Applications but it fails the test and says there is 20.

Is this because it is returning both Position__c and Candidate_Name__c individually from the list?

Or is there an error in my code I'm overlooking.

EDIT This is the Testing Code

    static void testGetPositionsAndApplicants(){
        List<Job_Application__c> result = RecruitingHelper.getPositionsAndApplicants();

and this is the actual query call

public static List<Job_Application__c> getPositionsAndApplicants(){
        return [Select Position__c, Candidate__c FROM Job_Application__c];

EDIT2: So I figured out that the double size of Job_Application__c happens when I run the Review__c. Like mentioned earlier these two methods are part of my TestDataFactory.


public class RecruitingHelper_test {
    static void setup(){
        List<Position__c> testPositions = TestDataFactory.createPositions();
        insert testPositions;
        List<Job_Application__c> testApplications = TestDataFactory.createApplications();
        insert testApplications;
        List<Job_Posting__c> testJobPostings = TestDataFactory.createPostings();
        insert testJobPostings;
        List<Review__c> testReviews = TestDataFactory.createReviews();
        insert testReviews;

This is the testsetup as requested. I can't find where the data is duplicating but I know for a fact it's somewhere when Review__c data is created because I have commented everything out and when I comment that out the test works perfectly for Job_Application__c.

  • 3
    Your provided code here seems to be simple enough that there's not really much room for errors. Since you say that you have a test that's failing, it'd be helpful to include your test class. Also, it's possible that there is some other code (perhaps a trigger, which could be on Position__c, Candidate_Name__c, or maybe on Job_Application__c) or declarative solution (like process builder) that could be throwing a wrench into your testing.
    – Derek F
    Commented Feb 1, 2019 at 21:08
  • There I added more information so I narrowed it down where the extra 10 Job_Application__c it happens when the Review__c creation happens I still can't figure out where though.
    – YGarcia
    Commented Feb 4, 2019 at 17:23
  • 1
    I still think we're missing some pieces of the puzzle here. It isn't clear how your code fragments are connected to each other. Assuming that the snippet for getPositionsAndApplicants() is indeed in the RecuritingHelper class, your test method isn't doing any setup. Solving this problem requires seeing and understanding the entire path taken through your code. What's creating your data? What code calls your data factory? Do you have an @testSetup annotated method?
    – Derek F
    Commented Feb 4, 2019 at 17:45

2 Answers 2


You're creating Job_Application__c records twice - once in createApplications() and once createReviews(), where the Job_Application__c records are created and inserted inside the test factory. Since each method generates 10 records, your test code will see 20.

Your assertions just don't match your test setup. You can correct this by either factoring your test setup into separate setup increments for individual unit tests, so each test is exposed only to its own specific data set, or by adjusting your assertions to take this into account.

  • OK I understand it now so when the setup runs since I have createReviews() create Job_Application__c those are then inserted to the previous created list from createApplications().
    – YGarcia
    Commented Feb 5, 2019 at 4:35

We can see that you are creating 10 Job_Application__c records in createApplications, so no it is not because it is returning Position__c AND Candidate_Name__c. I agree with Derek F that you may have a trigger that is duplicating it somehow.

Another possible cause may be how you are getting your data. Where is the createApplications method? It looks like it may be a data factory, because I don't see a @isTest or @TestSetup annotation. When are you calling it to create the data? Could you be calling it twice somehow? Or are you using @isTest(SeeAllData=true) ?

You must log in to answer this question.

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