0
public void byStatus(){
    //Temporary multipicklist, because my main picklist isn't multiselected:
    String temporary = jobAdvertisement.Temporary__c;
    List<String> temporaryList = new List<String>();
    if(temporary != null){
        temporaryList = temporary.split(';');
    }
    //statusList - a list with choosed variants from multipicklist:
    statusList = new List<String>();
    for(String s : temporary){
        statusList.add(s);
    }
status = String.join(soughtStatusList,',');
}

    //Then I use query to make a filter:
    public List<Item__c> mySerch(){
        String[] filters = new String[0];
        if(String.isNotBlank(title)) {
            filters.add('Title__c =: title');
        }
        ... //another if's
        if(String.isNotBlank(status)) {
            filters.add('Field__c =: statusList');
        }
        return Database.query('SELECT Id, Name FROM Item__c'+
                             (filters.isEmpty()?'':' WHERE ' + String.join(filters,' AND ')));
    }

public ApexPages.StandardSetController setCon {
    get {
        if(setCon == null) {                
            setCon = new ApexPages.StandardSetController(mySerch());
            setCon.setPageSize(size);  
            noOfRecords = setCon.getResultSize();  //Integer: number of records
        }
        return setCon;
    }
    set;
}

public PageReference refreshPageSize() {
    setCon.setPageSize(size);
    noOfRecords = setCon.getResultSize();
    return null;
}

public Pagereference methodForSearch() {
    setCon = new ApexPages.StandardSetController(mySerch());
    refreshPageSize();
    return null;
}  

Test:

static void test() {
Item__c item = new Item__c(Title__c = 'Test', Status__c = 'New');
List<Item__c> itemList = new List<Item__c>{item};
insert itemList;

PageReference pref = Page.vf;
Test.setCurrentPage(pref);  
ApexPages.StandardController sc = new ApexPages.StandardController(item);

MyClass.byStatus();
//List<String> statusList = new List<String>{'New'};  //With list it's wrong too

List<Item__c> itemListWithFilters = MyClass.mySerch();
//MyClass.statusList = statusList;
MyClass.title = 'Right Title';
MyClass.status = 'New';

PageReference result = MyClass.methodForSearch();
System.assertEquals(itemListWithFilters, itemList);
System.assertEquals(MyClass.noOfRecords, 1);

}

But I get error on this row: my filtered with right parameters list is null. If I use MyClass.statusList instead MyClass.status, I can write even wrong status there and all is fine. How can I test this part of filter? What I need to check here?

2
static void test() {
Item__c item = new Item__c(Title__c = 'Test', Status__c = 'New');
List<Item__c> itemList = new List<Item__c>{item};
insert itemList;

PageReference pref = Page.vf;
Test.setCurrentPage(pref);  
ApexPages.StandardController sc = new ApexPages.StandardController(item);

MyClass.byStatus();
//List<String> statusList = new List<String>{'New'};  //With list it's wrong too

/*List<Item__c> itemListWithFilters = MyClass.mySerch();
//MyClass.statusList = statusList;
MyClass.title = 'Right Title';
MyClass.status = 'New';*/

//We put it here:
List<String> l = MyClass.statusList;
l.add('New');
String status = String.join(l,',');

MyClass.status = status;

PageReference result = MyClass.methodForSearch();
System.assertEquals(itemListWithFilters, itemList);
System.assertEquals(MyClass.noOfRecords, 1);

}

In controller the String isn't just a string: status = String.join(soughtStatusList,','); So we need just to model a List with correct status and then make the same string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.