0

In my controller I have a List:

public List<ts2__Job__c> uaVacancies {
  get;
  set;
}

And a simple function to query:

public PageReference getUAVacancies() {
  uaVacancies = [SELECT Id, Name, ts2__Account__c, ts2__Contact__c, ts2__Priority__c, ts2__Stage__c, ts2__Job_Number__c, CreatedDate FROM ts2__Job__c WHERE (ts2__Recruiter__c = :user OR Development_Owner__c = :user) AND ts2__Status__c = 'Open'];

  return null;
}

In my test class' @testSetup I'm creating a ts2__Job___c object, then I have a method that calls the function getUAVacancies().

It all covers fine, but I wanted to assert that the list is populated with 1 record. So when I do:

System.assertEquals(1, uaVacancies.size());

The test fails saying I'm expecting 1, but getting 0.

I'm not sure what I'm doing wrong here! But here's my full test method:

@isTest static void testSearch() {
  User u;

  for (User i : [SELECT Id FROM User WHERE Email = '[email protected]' LIMIT 1]) {
    u = i;
  }

  System.runAs(u) {
    KN_VacanciesController con = new KN_VacanciesController();

    con.getUAVacancies();
    con.getPostedKNVacancies();

    System.assertEquals(1, uaVacancies.size());
  }
}

In the test record I'm inserting, I'm making sure that ts2__Status__c = 'Open' and set the Development_Owner__c to the user created in the test class.

Any reason why this is returning 0 when it should return 1?

Edit I changed the method as follows:

@isTest static void testSearch() {
  User u;

  for (User i : [SELECT Id FROM User WHERE Email = '[email protected]' LIMIT 1]) {
    u = i;
  }

  ts2__Job__c j = new ts2__Job__c(
    Name = 'Test Job',
    ts2__Status__c = 'Open',
    Development_Owner__c = u.Id
  );

  INSERT j;

  System.runAs(u) {
    KN_VacanciesController con = new KN_VacanciesController();

    con.getUAVacancies();
    con.getPostedKNVacancies();

    System.assertEquals(1, con.uaVacancies.size());
  }
}

And the test passed. What I don't understand is I created the exact same test__Job__c object in @testSetup static void testData() method. I checked the Sharing Settings of the object and they're Public Read/Write and the Controller itself is public with sharing class. I'm curious to know how come my test method can't access data in the @testSetup?

8
  • where are you inserting the records? Commented Jan 29, 2016 at 9:38
  • 1
    @TusharSharma I've got a @testSetup static void testData() {} method.
    – Dan Jones
    Commented Jan 29, 2016 at 9:38
  • 2
    What is the sharing model for the ts2__Job__c? Is your controller with or without sharing? You're inserting it with the system user in your @testSetup and trying to read it with user u. If the OWD are set to private, u won't be able to see that record.
    – mkorman
    Commented Jan 29, 2016 at 9:41
  • i think you should do this System.assertEquals(1, con.uaVacancies.size()); Commented Jan 29, 2016 at 9:49
  • @TusharSharma I created the ts2__Job__c record in the testSearch() method and it passed. The controller itself is public with sharing class and the OWD for that object is Public Read/Write? Also yeah sorry I missed the con. off I didn't copy it straight from the code!
    – Dan Jones
    Commented Jan 29, 2016 at 9:51

1 Answer 1

0


You have to create a ts2__Job__c record in your test class.

@isTest static void testSearch() {
    User u;

    for (User i : [SELECT Id FROM User WHERE Email = '[email protected]' LIMIT 1]) {
         u = i;
    }

    System.runAs(u) {

        ts2__Job__c tj = new ts2__Job__c ();
        tj.ts2__Status__c = 'Open';
        tj.value__c = 'value';
        tj.value__c = 'value';
        tj.value__c = 'value';

        insert(tj);

        Test.startTest();  
            KN_VacanciesController con = new KN_VacanciesController();
            con.getUAVacancies();
            con.getPostedKNVacancies();
        Test.stopTest();

        System.assertEquals(1, uaVacancies.size());
    }
}

Toufik

2
  • He said he was inserting them in a @testSetup method...
    – mkorman
    Commented Jan 29, 2016 at 9:41
  • @mkorman Technically, this worked. When I created the record in the @isTest method, the assertion worked. However, when I put it in the @testSetup it doesn't. Strange and I'm not sure why!
    – Dan Jones
    Commented Jan 29, 2016 at 10:21

You must log in to answer this question.

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