Please help me in writing test class for below apex code,i wrote a test class which shows only 66% coverage,i am looking for 100%
public class PickListHandler {
#AuraEnabled
public static List<String> getLevel1(string strName) {
List<String> tempLst = new List<String>
for(AggregateResult ar : [select Level_1__c,COUNT(id) from Case_Type_Data__c group by Level_1__c])
{
tempLst.add('Level 1 data is'+ar.get('Level_1__c'));
return tempLst;
}
}
}
Here is test class
#isTest
public class testGetLevel1 {
static testMethod void testGetLevel1() {
List<String> s = PickListHandler.getLevel1('test');
//System.assert(....);
}
}
You need need to create test data for the object Case_Type_Data__c. If you don't create data, the logic inside for loop will not execute.
Related
I have a problem Ref<> usage with #Load. Basically I made a copy paste from Objectify website to test #Load annotation with Load Groups.
#Entity
public static class Thing {
public static class Partial {}
public static class Everything extends Partial {}
public static class Stopper {}
#Id Long id;
#Load(Partial.class) Ref<Other> withPartial;
#Load(Everything.class) Ref<Other> withEveryhthing;
#Load(unless=Stopper.class) Ref<Other> unlessStopper;
public Ref<Other> getWithPartial() {
return withPartial;
}
public void setWithPartial(Ref<Other> withPartial) {
this.withPartial = withPartial;
}
public Ref<Other> getWithEveryhthing() {
return withEveryhthing;
}
public void setWithEveryhthing(Ref<Other> withEveryhthing) {
this.withEveryhthing = withEveryhthing;
}
public Ref<Other> getUnlessStopper() {
return unlessStopper;
}
public void setUnlessStopper(Ref<Other> unlessStopper) {
this.unlessStopper = unlessStopper;
}
}
Then I wrote the following code.
Other other = new Other();
Key<Other> otherKey = ofy().save().entity(other).now();
Thing thing = new Thing();
thing.setWithPartial(Ref.create(otherKey));
Key<Thing> thingKey = ofy().save().entity(thing).now();
Thing t = ofy().load().key(thingKey).now();
System.out.println("Is loaded: " + t.getWithPartial().isLoaded());
Without writing ofy().load().group(Partial.class).key(thingKey).now(); other entity still loads into session. However in documentation it needs group class to be loaded.
The isLoaded() method tests whether the entity is in the session cache. Since you just save()d the entity, it's already in the session cache.
If you want to test the behavior of load groups, you need to ofy().clear() the cache.
Here is the class I managed to create, not sure where to start with code coverage as this is my first apex class, or doing anything with salesforce.
Can someone point me in the right direction. Thanks!
public with sharing class VelocifyAcctStatsController
{
public List<Account> acctstats {get;set;}
public VelocifyAcctStatsController()
{
acctstats = [select MVA_Type__c, MVA_Name__c, MVA_Is_VIP__c, MVA_Is_Brand_TM__c, MVA_Classification__c, MVA_Classification_Priority__c, Assets_Owned__c, Portfolio_Overview__c, Active_Opportunities__c, X3x3_Research_One__c, X3x3_Research_Two__c, X3x3_Research_Three__c from account WHERE Id = :ApexPages.currentPage().getParameters().get('Id')];
}
}
This should help you get started:
#isTest
public class VelocifyAcctStatsControllerTest {
#isTest
public static void test(){
Account a = new Account(Name = 'Test acct' );
insert a;
ApexPages.currentPage().getParameters().put('Id', a.Id);
VelocifyAcctStatsController v = new VelocifyAcctStatsController();
System.assertEquals(v.acctstats.size(), 1);
}
}
How to write a test class for a class containing public final static strings in salesforce?
I tried using system.assertequals
Doesnt seem to work properly.
#isTest
private class Test_TPET_Constants{
private static testMethod void test() {
//TPET_Constants inst= new TPET_Constants();
System.assertEquals(TPET_Constants.PICKLIST_COLLAB_SERVICE_SECURE_EMAIL,'Enterprise Secure Email');
System.assertEquals(TPET_Constants.DRAFT_STATUS, 'Draft');
System.assertEquals(TPET_Constants.ACTIVE_STATUS, 'Active');
System.assertEquals(TPET_Constants.INACTIVE_STATUS, 'Inactive');
System.assertEquals(TPET_Constants.SUBMITTED_STATUS, 'Submitted');
System.assertEquals(TPET_Constants.REJECTED_STATUS , 'Rejected');
System.assertEquals(TPET_Constants.PICKLIST_COLLAB_SERVICE , 'Collab Service');
System.assertEquals(TPET_Constants.PENDING_IMPLEMENTATION_STATUS ,'Pending Implementation');
}
}
In your class you need to markthe variable #TestVisible. Check here: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_annotation_testvisible.htm.
#TestVisible private static Integer recordNumber = 1;
Before I setup a test class like the code below:
1. the Factory and test Dataprovider both used excel as the dataprovider.
2. In the Factory dataprovider table, it has a list of url
3. Each time, it will find one of the url in the factory dataprovider table, and run the test in each test methods..
public class Test {
WebDriver driver;
private String hostName;
private String url;
#Factory(dataProvider = "xxxx global variables", dataProviderClass = xxxx.class)
public GetVariables(String hostName, String url) {
this.hostName = hostName;
this.url = url;
}
#BeforeMethod
#Parameters("browser")
public void start(String browser) throws Exception {
driver = new FirefoxDriver();
driver.get(url);
Thread.sleep(1000);
}
#Test(priority = 10, dataProvider = "dataprovider Test A", dataProviderClass = xxx.class)
public void TestA(Variable1,
Variable2,Variable3) throws Exception {
some test here...
}
#Test(priority = 20, dataProvider = "dataprovider Test B", dataProviderClass = xxx.class)
public void TestB(Variable1,
Variable2,Variable3)
throws Exception {
some test here...
}
#AfterMethod
public void tearDown() {
driver.quit();
}
Now I want to dynamically assign different group for each test for different url. I am thinking add a variable 'flag' in the #Factory dataprovider:
#Factory(dataProvider = "xxxx global variables", dataProviderClass = xxxx.class)
public GetVariables(String hostName, String url, String flag) {
this.hostName = hostName;
this.url = url;
this.flag = flag;
}
That when flag.equals("A"), it will only run test cases in test groups={"A"}.
When flag.equals("B"), it will only run test cases in test groups ={"B"},
When flag.equals("A,B"), it will only run test cases in test groups ={"A","B"}
Is there any way I can do that?
Thank you!
TestNG groups provides "flexibility in how you partition your tests" but it isn't for conditional test sets. For that you simply use plain old Java.
You can use inheritance or composition (I recommend the latter, see Item 16: Favor composition over inheritance from Effective Java).
Either way the general idea is the same: use a Factory to create your test class instances dynamically creating the appropriate class type with the appropriate test annotations and/or methods that you want to run.
Examples:
Inheritance
import org.testng.annotations.Factory;
import org.testng.annotations.Test;
public class DemoTest {
#Factory
public static Object[] createTests() {
return new Object[]{
new FlavorATest(),
new FlavorBTest(),
new FlavorABTest()
};
}
/**
* Base test class with code for both A-tests and B-tests.
*
* Note that none of these test methods are annotated as tests so that
* subclasses may pick which ones to annotate.
*/
public static abstract class BaseTest {
protected void testA() {
// test something specific to flavor A
}
protected void testB() {
// test something specific to flavor B
}
}
// extend base but only annotate A-tests
public static class FlavorATest extends BaseTest {
#Test
#Override
public void testA() {
super.testA();
}
}
// extend base but only annotate B-tests
public static class FlavorBTest extends BaseTest {
#Test
#Override
public void testB() {
super.testB();
}
}
// extend base and annotate both A-tests and B-tests
public static class FlavorABTest extends BaseTest {
#Test
#Override
public void testA() {
super.testA();
}
#Test
#Override
public void testB() {
super.testB();
}
}
}
Composition
import org.testng.annotations.Factory;
import org.testng.annotations.Test;
public class DemoTest {
#Factory
public static Object[] createTests() {
return new Object[]{
new FlavorATest(),
new FlavorBTest(),
new FlavorABTest()
};
}
private static void testA() {
// test something specific to flavor A
}
private static void testB() {
// test something specific to flavor B
}
// only create A-test methods and delegate to shared code above
public static class FlavorATest {
#Test
public void testA() {
DemoTest.testA();
}
}
// only create B-test methods and delegate to shared code above
public static class FlavorBTest {
#Test
public void testB() {
DemoTest.testB();
}
}
// create A-test and B-test methods and delegate to shared code above
public static class FlavorABTest {
#Test
public void testA() {
DemoTest.testA();
}
#Test
public void testB() {
DemoTest.testB();
}
}
}
Your factory methods won't be as simple as you'll need to use your "flag" from your test data to switch off of and create instances of the appropriate test classes.
I am trying to write a test class for a controller extension. This controller extension has another class in it . And this class has a few methods.
public class Extension_Account
{
public Extension_Account(ApexPages.StandardController controller)
{
public class Class1
{
public Class1()
{
/ * code here*/
}
public String getMethod()
{
/* code here */
}
}
}
}
How can i access the method getMethod in my test class?
In my test class i am able to access the contructor for Class1 but not sure how to get to the other method
public with sharing class TestExtension_Account
{
static testMethod void TestPrint()
{
Account a = new Account();
//a.Name='Test Account';
a.FirstName='TestFirst Name';
a.LastName='Test Last Name';
a.BillingStreet='Test billing Street';
a.BillingCity='Test Billing City';
a.BillingState='Test Billing State';
a.BillingCountry='Test Billing country';
a.BillingPostalCode='Test PostCode';
insert a;
PageReference pageRef = Page.printaddressaccount;
pageRef .getParameters().put('id',a.id);
Test.setCurrentPageReference(pageRef);
ApexPages.StandardController controller = new ApexPages.StandardController(a);
Extension_Account ae = new Extension_Account(controller);
ae.getClass1();
ae.getMethod()// gives a compiler error Method does not exist or incorrect signature
}
}
If your extension class has a getClass1() method that returns an instance of Class1, then you can access the methods from that instance, e.g. ae.getClass1().getMethod();