You need to sign in to do that
Don't have an account?
Written test class but getting only 16% of coverage
- @isTest
- public class alu_Internship_ControllerTest{
- Id OppRecordTypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('Internship Opportunity').getRecordTypeId();
- public static testMethod void testaluController(){
- Account testAccount = new Account();
- testAccount.Name = 'Test Account' ;
- insert testAccount;
- alu_Internship_Cycle__c ic = new alu_Internship_Cycle__c();
- ic.Name = 'Test Cycle';
- ic.Start_Date__c = System.Today();
- ic.End_Date__c = System.Today() + 60;
- insert ic;
- Opportunity opp = new Opportunity();
- opp.RecordTypeId = '0120Y000000QELh';
- opp.Name = 'Test Opportunity';
- opp.StageName = 'Under Discussion';
- opp.CloseDate = System.Today();
- opp.AccountId = testAccount.Id;
- opp.Internship_Cycle__c = ic.Id;
- opp.Number_of_Internships_Committed__c = 1;
- opp.Opportunity_Countries__c = 'Algeria';
- insert opp;
- PageReference pageRef = Page.alu_Internship_form;
- Test.setCurrentPage(pageRef);
- alu_Internship_Controller testIntCyc = new alu_Internship_Controller();
- testIntCyc.doInsert();
- }
- }
- public class alu_Internship_Controller {
- Id OppRecordTypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('Internship Opportunity').getRecordTypeId();
- public final static String STAGE_NAME_UD = 'Under Discussion';
- public Opportunity opps {get;set;}
- public string str {get;set;}
- public string counName {get;set;}
- public String oppdes {get; set;}
- public String names {get; set;}
- public string cycleName { get; set;}
- public Id Idc {get; set;}
- public string cmpweb { get; set;}
- public string oppcitiesofOper{get;set;}
- public string oppPotPro{get;set;}
- public List<string> couName { get; set;}
- public List<String> listOfCompany {get; set;}
- public Map<Id,String> mapIdStr {get; set;}
- public Id Ids {get; set;}
- public Id opIds {get; set;}
- public String acc {get; set;}
- public Decimal noOfPosition {get;set;}
- public alu_Internship_Controller() {
- opps = new Opportunity();
- opps.RecordTypeId = OppRecordTypeId;
- mapIdStr = new Map<Id,String>();
- listOfCompany = new List<String>();
- for(Account a: [SELECT Id, Name From Account]) {
- mapIdStr.put(a.Id, a.Name);
- listOfCompany.add(a.name);
- }
- }
- public list<SelectOption> listCycleName {
- get{
- list<SelectOption> listSO = new list<SelectOption>();
- listSO.add(new SelectOption('', 'Select Internship Cycle'));
- for (alu_Internship_Cycle__c cyc : [select Id, Name from alu_Internship_Cycle__c]) {
- if(cyc != null) {
- listSO.add(new SelectOption(cyc.Id, cyc.Name));
- }
- }
- return listSO;
- }
- set;
- }
- public List<SelectOption> getCountries() {
- List<SelectOption> options = new List<SelectOption>();
- Schema.DescribeFieldResult fieldResult = Opportunity.Opportunity_Countries__c.getDescribe();
- List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
- for( Schema.PicklistEntry f : ple) {
- options.add(new SelectOption(f.getLabel(), f.getValue()));
- }
- return options;
- }
- public List<SelectOption> getStages() {
- List<SelectOption> options = new List<SelectOption>();
- Schema.DescribeFieldResult fieldResult = Opportunity.StageName.getDescribe();
- List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
- for(Schema.PicklistEntry f : ple){
- options.add(new SelectOption(f.getLabel(), f.getValue()));
- }
- return options;
- }
- // Logic for Save Button.
- public PageReference doInsert() {
- // Storing Id and Acccount Name If acc contains same name.
- Map<Id,String> mapString = new Map<Id,String>();
- for(Id tmp : mapIdStr.KeySet()) {
- String name = mapIdStr.get(tmp);
- if(acc.Contains(name)){
- mapString.put(tmp,acc);
- }
- }
- for(Id Accid: mapString.KeySet()){
- Ids = Accid;
- }
- System.debug('Ids'+Ids);
- // validation Logic Start
- if (acc == ''){
- ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Please select a Company Name'));
- return null;
- }
- if (cmpweb == ''){
- ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Please enter an Company Website'));
- return null;
- }
- if (cycleName == null){
- ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Please enter an Preffered Internship Cycle'));
- return null;
- }
- if (noOfPosition == null){
- ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Please select a Number of Position'));
- return null;
- }
- // Iterating OpportunityId from List Of Opportunity
- for(Opportunity opps : opportunitylists(Ids, cycleName)) {
- opIds = opps.Id;
- }
- for(string s : couName) {
- if(counName != null){
- counName= counName+';'+s;
- }else{
- counName = s;
- }
- }
- if (counName == null){
- ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Please select a Countries Operation'));
- return null;
- }
- Account ac = new Account();
- if(opIds != null) {
- Opportunity opp = new Opportunity();
- opp.Id = opIds;
- opp.Opportunity_Countries__c = '';
- opp.Opportunity_Description__c = oppdes;
- opp.AccountId = Ids;
- opp.Internship_Cycle__c = cycleName;
- opp.Number_of_Internships_Committed__c = noOfPosition;
- opp.Opportunity_Countries__c = counName;
- update opp;
- system.debug('Opp Update'+ opp);
- ac.Id= opp.AccountId;
- ac.Website = cmpweb;
- update ac;
- system.debug('ACC Update'+ ac);
- } else {
- Opportunity opp = new Opportunity();
- opp.RecordTypeId = OppRecordTypeId;
- opp.name = '-UG Internship-';
- opp.Opportunity_Description__c = oppdes;
- opp.CloseDate = System.today();
- opp.AccountId = Ids;
- opp.StageName = STAGE_NAME_UD;
- opp.Internship_Cycle__c = cycleName;
- opp.Opportunity_Countries__c = counName;
- opp.Number_of_Internships_Committed__c = noOfPosition;
- insert opp;
- system.debug('Opp Insert'+ opp);
- ac.Id= opp.AccountId;
- ac.Website = cmpweb;
- update ac;
- system.debug('ACC Update'+ ac);
- }
- pagereference ref = new pagereference('/apex/alu_Internship_Thanks_Msg');
- ref.setredirect(true);
- return ref;
- }
- // getting list of Opportunity
- public List<Opportunity> opportunitylists (Id Ids, String cycleName) {
- return [SELECT Id, AccountId, Internship_Cycle__c
- FROM Opportunity
- WHERE AccountId =:Ids
- AND Internship_Cycle__c = : cycleName
- AND Stagename=: STAGE_NAME_UD
- ];
- }
- }
Try writing @isTest(SeeAllData=true) instead @isTest
If still face issue let me know. I would be happy to help you out
Thanks
alu_Internship_Controller tempObject= new alu_Internship_Controller();
List<SelectOption> options1 = tempObject.getStages();
List<SelectOption> options2 = tempObject.getCountries();
let me known if it works
Thanks
// public PageReference doInsert()
Try this code
PageReference pageRef = Page.yourPageName;
Test.setCurrentPage(pageRef);
pageRef.getParameters().put('id',testAccount.id);
ApexPages.StandardController sc = new ApexPages.standardController(testAccount);
alu_Internship_Controller controller = new alu_Internship_Controller(sc);
controller.doInsert();
i Think you are missing Account id
let me know if any issue
Thanks
//Constructor not defined: [alu_Internship_Controller].<Constructor>(ApexPages.StandardController)