function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
VishwanathVishwanath 

Need help to write test class on Static method

Hi,

I have a static method as given below im tryong to writte a test class for this it shows error

please any one give some idea to solve this problem

 

public class controller
{
public static SPQ__c product { get; set; }
public String productname { get; set; }   
@RemoteAction
    global static SPQ__c getSPQPr(String productname) {
        product = [select id ,Standard_Cost__c, name from SPQ__c where name= :productname and Year__c='Mid Year 2011' limit 1];  
        return product;
    }  


@RemoteAction
    global static Test__c getType() {
        AccType = [select Accessory_Type__c , name from Test__c where id= :'a2mQ0000001MpvE' limit 1];
        return AccType;
    }  
    }

 Thanks,

Best Answer chosen by Admin (Salesforce Developers) 
vishal@forcevishal@force

Hi,

 

Below code should help you :

 

public class controller
{
	public static SPQ__c product { get; set; }
	public String productname { get; set; }   
	
	@RemoteAction
	global static SPQ__c getSPQPr(String productname) 
	{
		product = [select id ,Standard_Cost__c, name from SPQ__c where name= :productname and Year__c='Mid Year 2011' limit 1];  
		return product;
	}  


	@RemoteAction
	global static Test__c getType() 
	{
		AccType = [select Accessory_Type__c , name from Test__c where id= :'a2mQ0000001MpvE' limit 1];
		return AccType;
	}
	
	@isTest
	private static void testController()
	{
		SPQ__c spq = new SPQ__c();
		spq.Name = 'Test';
		insert spq;
		// Also give any other required fields if SPQ has any other required field.
		
		controller.getSPQPr('Test');
		controller.getType(); // calling this method will give you an Exception because you have  hard-coded your Id in the query. Avoid it if possible
		                         or pass it as a parameter. 
	}

}

 Once you handle the exceptions, the above code should be good to cover your class! Hope this helps.

All Answers

vishal@forcevishal@force

Hi,

 

Below code should help you :

 

public class controller
{
	public static SPQ__c product { get; set; }
	public String productname { get; set; }   
	
	@RemoteAction
	global static SPQ__c getSPQPr(String productname) 
	{
		product = [select id ,Standard_Cost__c, name from SPQ__c where name= :productname and Year__c='Mid Year 2011' limit 1];  
		return product;
	}  


	@RemoteAction
	global static Test__c getType() 
	{
		AccType = [select Accessory_Type__c , name from Test__c where id= :'a2mQ0000001MpvE' limit 1];
		return AccType;
	}
	
	@isTest
	private static void testController()
	{
		SPQ__c spq = new SPQ__c();
		spq.Name = 'Test';
		insert spq;
		// Also give any other required fields if SPQ has any other required field.
		
		controller.getSPQPr('Test');
		controller.getType(); // calling this method will give you an Exception because you have  hard-coded your Id in the query. Avoid it if possible
		                         or pass it as a parameter. 
	}

}

 Once you handle the exceptions, the above code should be good to cover your class! Hope this helps.

This was selected as the best answer
Arijit_rArijit_r

Vishwanath -

 

Wats the error you are getting?

kiranmutturukiranmutturu

could you plz post ur error so that it will give a view abt what exactly the prob is 

but try this once

@isTest
private class TestView{
static testMethod void testtView() {
controller obj = new controller();
spq__c s = obj.product;
string str = obj.productname();
controller.getSPQPr();
controller.getType();
}
}

VishwanathVishwanath

Thank You Vishal,

 

its working