Skip to main content
added 2509 characters in body
Source Link
rwegner7
  • 455
  • 7
  • 25

Test Class Issue - My REST Method Isn't Getting Run

In my visualforce page I have a search field (inputText) with an id of 'query'. Query is passed into my controller and the runSearch() method starts a process which initiates a web service call. In my test class I am trying to initiate the API call by passing in a search value.

Extension

public with sharing class ClinicalStudySetupExtension {

private String studyJson;
private String result;
private String protocolNumber;
public String query {get; set;}
public Boolean searched {get; set;}
public Clinical_Study__c cStudy {get; set;}
private ApexPages.StandardController std {get; set;}

//controller
public ClinicalStudySetupExtension(ApexPages.StandardController std) {
    
    this.std = std;
    this.studyJson = callRestEndPoint();
    
    query = '';
    searched=false;
    String queryStr=ApexPages.currentPage().getParameters().get('query');
    if (queryStr!=null)
    {
        protocolNumber=queryStr;
        runSearch();
    }
}

public pageReference runSearch() {//runSearch takes the input 'query' and passes it to the REST api call
    
    searched=true;
    String searchStr = ''+query+'';
    system.debug('Study name: ' + query);
    protocolNumber = searchStr.deleteWhitespace();
            
    this.studyJson = callRestEndPoint();

    return null;
  }

private string callRestEndPoint() {//the rest api call generates a json string and passes it to the json parser      
    Http h = new Http();
    HttpRequest req = new HttpRequest();
    HttpResponse res = new HttpResponse();
    
    String username = 'ex';
    String password = 'ex';
    
    Blob headerValue = Blob.valueOf(username + ':' + password);
    String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(headerValue);
    req.setClientCertificateName('CN');
    req.setHeader('Authorization', authorizationHeader);
    req.setHeader('Api-Key', '777..');
    req.setTimeout(120000);
    
    req.setMethod('GET');
    req.setEndpoint('https://example.com/study/'+protocolNumber);      

    String result = '';
    try{
        res = h.send(req);
        result = res.getBody();
        }
    catch(System.CalloutException e){
        result = res.toString();
        }
    
    return result;     
}

List<StudyWrapper> Study = new List<StudyWrapper>();

public List<StudyWrapper> getStudy(){//the json parser parses the json string and puts it in a list, study
    JSONParser parser = JSON.createParser(this.studyJson);
    while(parser.nextToken() != null){
            while(parser.nextToken()!= null){
                if(parser.getCurrentToken()==JSONToken.START_OBJECT){
                    StudyWrapper s = (StudyWrapper)parser.readValueAs(StudyWrapper.class);
                    Study.add(s);
                }
            }
        }
    return Study;
}
    

I'm trying to set up my test class, so far it looks like this:

Test class

Test Class Issue - My Method Isn't Getting Run

In my visualforce page I have a search field (inputText) with an id of 'query'. Query is passed into my controller and the runSearch() method starts a process which initiates a web service call.

public with sharing class ClinicalStudySetupExtension {

....
public pageReference runSearch() {
    
    searched=true;
    String searchStr = ''+query+'';
    system.debug('Study name: ' + query);
    protocolNumber = searchStr.deleteWhitespace();
            
    this.studyJson = callRestEndPoint();
    

I'm trying to set up my test class, so far it looks like this:

Test Class Issue - My REST Method Isn't Getting Run

In my visualforce page I have a search field (inputText) with an id of 'query'. Query is passed into my controller and the runSearch() method starts a process which initiates a web service call. In my test class I am trying to initiate the API call by passing in a search value.

Extension

public with sharing class ClinicalStudySetupExtension {

private String studyJson;
private String result;
private String protocolNumber;
public String query {get; set;}
public Boolean searched {get; set;}
public Clinical_Study__c cStudy {get; set;}
private ApexPages.StandardController std {get; set;}

//controller
public ClinicalStudySetupExtension(ApexPages.StandardController std) {
    
    this.std = std;
    this.studyJson = callRestEndPoint();
    
    query = '';
    searched=false;
    String queryStr=ApexPages.currentPage().getParameters().get('query');
    if (queryStr!=null)
    {
        protocolNumber=queryStr;
        runSearch();
    }
}

public pageReference runSearch() {//runSearch takes the input 'query' and passes it to the REST api call
    
    searched=true;
    String searchStr = ''+query+'';
    system.debug('Study name: ' + query);
    protocolNumber = searchStr.deleteWhitespace();
            
    this.studyJson = callRestEndPoint();

    return null;
  }

private string callRestEndPoint() {//the rest api call generates a json string and passes it to the json parser      
    Http h = new Http();
    HttpRequest req = new HttpRequest();
    HttpResponse res = new HttpResponse();
    
    String username = 'ex';
    String password = 'ex';
    
    Blob headerValue = Blob.valueOf(username + ':' + password);
    String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(headerValue);
    req.setClientCertificateName('CN');
    req.setHeader('Authorization', authorizationHeader);
    req.setHeader('Api-Key', '777..');
    req.setTimeout(120000);
    
    req.setMethod('GET');
    req.setEndpoint('https://example.com/study/'+protocolNumber);      

    String result = '';
    try{
        res = h.send(req);
        result = res.getBody();
        }
    catch(System.CalloutException e){
        result = res.toString();
        }
    
    return result;     
}

List<StudyWrapper> Study = new List<StudyWrapper>();

public List<StudyWrapper> getStudy(){//the json parser parses the json string and puts it in a list, study
    JSONParser parser = JSON.createParser(this.studyJson);
    while(parser.nextToken() != null){
            while(parser.nextToken()!= null){
                if(parser.getCurrentToken()==JSONToken.START_OBJECT){
                    StudyWrapper s = (StudyWrapper)parser.readValueAs(StudyWrapper.class);
                    Study.add(s);
                }
            }
        }
    return Study;
}
    

I'm trying to set up my test class, so far it looks like this:

Test class

added 554 characters in body
Source Link
rwegner7
  • 455
  • 7
  • 25

Test Class Issue - How to set inputText valueMy Method Isn't Getting Run

In my visualforce page I have a search field (inputText) with an id of 'query'. Query is passed into my controller extension like soand the runSearch() method starts a process which initiates a web service call.

public with sharing class ClinicalStudySetupExtension {

....
public pageReference runSearch() {
    
    searched=true;
    String queryStr=ApexPagessearchStr = ''+query+'';
    system.currentPagedebug('Study name: ' + query);
    protocolNumber = searchStr.getParametersdeleteWhitespace();
            
    this.getstudyJson = callRestEndPoint('query');
    

I'm trying to set up my test class, so far it looks like this:

static testMethod void testRunSearch(){

    //create a test user
    Profile p1 = [SELECT Id FROM Profile WHERE Name='System Administrator'];

    User u1 = new User(Alias = 'George', UserName='JFFGeo@testorg#.com', Email='[email protected]', LastName='George', ProfileId = p1.Id, EmailEncodingKey='UTF-8',  LanguageLocaleKey='en_US', LocaleSidKey='en_US', TimeZoneSidKey='America/Los_Angeles' );
    insert u1;
    
    //Arrange the components
    PageReference pageRef = Page.ClinicalStudySetup;
    
    Test.setCurrentPage(pageRef);

    ApexPages.StandardController sc = new ApexPages.StandardController(new Clinical_Study__c());
    ClinicalStudySetupExtension ext = new ClinicalStudySetupExtension(sc);

    pageRef.getParameters().put('query','GA00806'); 

    Test.startTest();

        System.runAs(u1){

            ext.runSearch();

        }

    Test.stopTest();

}

Unfortunately, the way that I'm defining query and executing runSearch() doesn't seem to be working in my test class. The method isn't even being hit when I run my test class. Can someone tell me, what isam I doing wrong? Or what could be the proper way to define input for my 'query' variable in my test classcause of this?

Test Class Issue - How to set inputText value

In my visualforce page I have a search field (inputText) with an id of 'query'. Query is passed into my controller extension like so

String queryStr=ApexPages.currentPage().getParameters().get('query');

I'm trying to set up my test class, so far it looks like this:

static testMethod void testRunSearch(){

    //create a test user
    Profile p1 = [SELECT Id FROM Profile WHERE Name='System Administrator'];

    User u1 = new User(Alias = 'George', UserName='JFFGeo@testorg#.com', Email='[email protected]', LastName='George', ProfileId = p1.Id, EmailEncodingKey='UTF-8',  LanguageLocaleKey='en_US', LocaleSidKey='en_US', TimeZoneSidKey='America/Los_Angeles' );
    insert u1;
    
    //Arrange the components
    PageReference pageRef = Page.ClinicalStudySetup;
    
    Test.setCurrentPage(pageRef);

    ApexPages.StandardController sc = new ApexPages.StandardController(new Clinical_Study__c());
    ClinicalStudySetupExtension ext = new ClinicalStudySetupExtension(sc);

    pageRef.getParameters().put('query','GA00806'); 

    Test.startTest();

        System.runAs(u1){

            ext.runSearch();

        }

    Test.stopTest();

}

Unfortunately, the way that I'm defining query doesn't seem to be working in my test class. Can someone tell me, what is the proper way to define input for my 'query' variable in my test class?

Test Class Issue - My Method Isn't Getting Run

In my visualforce page I have a search field (inputText) with an id of 'query'. Query is passed into my controller and the runSearch() method starts a process which initiates a web service call.

public with sharing class ClinicalStudySetupExtension {

....
public pageReference runSearch() {
    
    searched=true;
    String searchStr = ''+query+'';
    system.debug('Study name: ' + query);
    protocolNumber = searchStr.deleteWhitespace();
            
    this.studyJson = callRestEndPoint();
    

I'm trying to set up my test class, so far it looks like this:

static testMethod void testRunSearch(){

    //create a test user
    Profile p1 = [SELECT Id FROM Profile WHERE Name='System Administrator'];

    User u1 = new User(Alias = 'George', UserName='JFFGeo@testorg#.com', Email='[email protected]', LastName='George', ProfileId = p1.Id, EmailEncodingKey='UTF-8',  LanguageLocaleKey='en_US', LocaleSidKey='en_US', TimeZoneSidKey='America/Los_Angeles' );
    insert u1;
    
    //Arrange the components
    PageReference pageRef = Page.ClinicalStudySetup;
    
    Test.setCurrentPage(pageRef);

    ApexPages.StandardController sc = new ApexPages.StandardController(new Clinical_Study__c());
    ClinicalStudySetupExtension ext = new ClinicalStudySetupExtension(sc);

    pageRef.getParameters().put('query','GA00806'); 

    Test.startTest();

        System.runAs(u1){

            ext.runSearch();

        }

    Test.stopTest();

}

Unfortunately, the way that I'm defining query and executing runSearch() doesn't seem to be working in my test class. The method isn't even being hit when I run my test class. Can someone tell me, what am I doing wrong? Or what could be the cause of this?

added 554 characters in body
Source Link
rwegner7
  • 455
  • 7
  • 25

In my visualforce page I have a search field (inputText) with an id of 'query'. Query is passed into my controller extension like so

String queryStr=ApexPages.currentPage().getParameters().get('query');

I'm trying to set up my test class, so far it looks like this:

static testMethod void testRunSearch(){

 Clinical_Study__c ClinicalStudy  //create a test user
    Profile p1 = [SELECT Id FROM Profile WHERE Name='System Administrator'];

    User u1 = new Clinical_Study__cUser(Alias = 'George', UserName='JFFGeo@testorg#.com', Email='[email protected]', LastName='George', ProfileId = p1.Id, EmailEncodingKey='UTF-8',  LanguageLocaleKey='en_US', LocaleSidKey='en_US', TimeZoneSidKey='America/Los_Angeles' );
    insert u1;
    
    //Arrange the components
    PageReference pageRef = Page.ClinicalStudySetup;
    
    Test.setCurrentPage(pageRef);

    ApexPages.StandardController sc = new ApexPages.StandardController(new Clinical_Study__c());
    ClinicalStudySetupExtension ext = new ClinicalStudySetupExtension(sc);

    pageRef.getParameters().put('query', 'GA00806');  

    Test.setCurrentPagestartTest(pageRef); 

        System.runAs(u1){

    ClinicalStudySetupExtension cse = new ClinicalStudySetupExtension(new ApexPages   ext.StandardControllerrunSearch(ClinicalStudy));

    //Act    }

    cseTest.runSearchstopTest();

}

Unfortunately, the way that I'm defining query doesn't seem to be working in my test class. Can someone tell me, what is the proper way to define input for my 'query' variable in my test class?

In my visualforce page I have a search field (inputText) with an id of 'query'. Query is passed into my controller extension like so

String queryStr=ApexPages.currentPage().getParameters().get('query');

I'm trying to set up my test class, so far it looks like this:

    Clinical_Study__c ClinicalStudy = new Clinical_Study__c();
    PageReference pageRef = Page.ClinicalStudySetup;
    pageRef.getParameters().put('query', 'GA00806'); 
    Test.setCurrentPage(pageRef);
    
    ClinicalStudySetupExtension cse = new ClinicalStudySetupExtension(new ApexPages.StandardController(ClinicalStudy));

    //Act
    cse.runSearch();

Unfortunately, the way that I'm defining query doesn't seem to be working in my test class. Can someone tell me, what is the proper way to define input for my 'query' variable in my test class?

In my visualforce page I have a search field (inputText) with an id of 'query'. Query is passed into my controller extension like so

String queryStr=ApexPages.currentPage().getParameters().get('query');

I'm trying to set up my test class, so far it looks like this:

static testMethod void testRunSearch(){

    //create a test user
    Profile p1 = [SELECT Id FROM Profile WHERE Name='System Administrator'];

    User u1 = new User(Alias = 'George', UserName='JFFGeo@testorg#.com', Email='[email protected]', LastName='George', ProfileId = p1.Id, EmailEncodingKey='UTF-8',  LanguageLocaleKey='en_US', LocaleSidKey='en_US', TimeZoneSidKey='America/Los_Angeles' );
    insert u1;
    
    //Arrange the components
    PageReference pageRef = Page.ClinicalStudySetup;
    
    Test.setCurrentPage(pageRef);

    ApexPages.StandardController sc = new ApexPages.StandardController(new Clinical_Study__c());
    ClinicalStudySetupExtension ext = new ClinicalStudySetupExtension(sc);

    pageRef.getParameters().put('query','GA00806');  

    Test.startTest(); 

        System.runAs(u1){

            ext.runSearch();

        }

    Test.stopTest();

}

Unfortunately, the way that I'm defining query doesn't seem to be working in my test class. Can someone tell me, what is the proper way to define input for my 'query' variable in my test class?

Post Undeleted by rwegner7
Post Deleted by rwegner7
Source Link
rwegner7
  • 455
  • 7
  • 25
Loading