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
up_skyup_sky 

I need help for creating Apex test class

Hello

 

I am new to Apex and just starting to learn it, so I would appreciate any help here.

 

I was informed the class needs to pass 75% before I can depoly it to my production org

 

Can anyone help me write a Test Class for this?

 

   

    for this not need to have test

public interface CMIAJsonEntry {
	String render();
}

  this is require for test class:

public class CMIAJsonEntryPair implements CMIAJsonEntry {
	private String key;
	private CMIAJsonEntry value;
	
	public CMIAJsonEntryPair(String key, CMIAJsonEntry value) {
		this.key = key;
		this.value = value;
	}
	
	public String render() {
		return '"' + key + '" : ' + value.render();
	}
}

 and this

public class CMIAJsonEntryMap implements CMIAJsonEntry {
	private LIST<CMIAJsonEntryPair> pairs;

	public CMIAJsonEntryMap() {
		pairs = new LIST<CMIAJsonEntryPair>();
	}
	
	public void addEntry(CMIAJsonEntryPair p) {
		pairs.add(p);
	}
	
	public void addEntry(String key, CMIAJsonEntry value) {
		pairs.add(new CMIAJsonEntryPair(key, value));
	}
	
	public void addEntry(String key, boolean value) {
		pairs.add(new CMIAJsonEntryPair(key, value ? CMIAJsonEntryBool.R_TRUE : CMIAJsonEntryBool.R_FALSE));
	}

	public void addEntry(String key, double value) {
		pairs.add(new CMIAJsonEntryPair(key, new CMIAJsonEntryDouble(value)));
	}
	
	public void addEntry(String key, integer value) {
		pairs.add(new CMIAJsonEntryPair(key, new CMIAJsonEntryInt(value)));		
	}
	
	public void addEntry(String key, String value) {
		pairs.add(new CMIAJsonEntryPair(key, new CMIAJsonEntryString(value)));		
	}
	
	public String render() {
		String r = '{';
		integer last = pairs.size() - 1;
		for (integer i = 0; i < pairs.size(); i++) {
			r = r + pairs.get(i).render();
			if (i != last)
				r = r + ', ';
		}
		return r + ' }';
	}
}

 Thank you in advance,

 

Apple

Rajesh SriramuluRajesh Sriramulu

Hi

 

For the first one

@isTest

Public class CMIAJsonEntryPair_test{

public CMIAJsonEntryPair_test(){

String str='test';

CMIAJsonEntry cmia;

CMIAJsonEntryPair cjep = new CMIAJsonEntryPair(str,cmia);

String str2;

str2=cjep.render();

}}

 

 

for the second class

 

@isTest

public class CMIAJsonEntryMap_test{

public CMIAJsonEntryMap_test()

{

CMIAJsonEntryMap cmj = new CMIAJsonEntryMap();

String str1='test';

CMIAJsonEntry cmia1 = new CMIAJsonEntry();

cmj.addEntry(str1,cmia1);

CMIAJsonEntryPair cmia= new CMIAJsonEntryPair()

cmj.addEntry(cmia);

Boolean bol= true;

cmj.addEntry(str1,bol);

Double value1=4.124563;

cmj.addEntry(str1,value1);

Integer int1=898;

cmj.addEntry(str1,int1);

String str2='test2';

cmj.addEntry(str1,str2);

cmj.render();

}}

 

Hope this will help u

 

 

 

ClintLeeClintLee

To test your CMIAJsonEntryPair you would need to create a method like this and add it to your class.

 

static testmethod void testOne() {

           String key = 'mykey';

           CMIAJSONEntryMap cmiaMap = new CMIAJsonEntryPair();   // any concrete class that implements CMIAJsonEntry could go here.

           CMIAJsonEntryPair cmia = new CMIAJsonEntryPair( key, cmiaMap );

           String rendered  =  cmia.render();

 

           System.assertEquals( '"mykey " : { }', rendered );    // you'll probably need to tweak the first parameter as I'm not sure how it's supposed to look, 

                                                                                                    // but the first parameter should equal the second.

}

 

For the second one, you want to follow the similar procedure.  Instantiate your CMIAJsonEntryMap object and call each of its methods and make some assertions.  You can put the 

test method directly in the class.

 

static testmethod void testOne() {

          CMIAJsonEntryMap cmiaMap = new CMIAJsonEntryMap();

          cmiaMap.addEntry( pass in your argument );

          cmiaMap.addEntry( 'key', pass in your object );

          cmiaMap.addEntry( 'key2', true );

          cmiaMap.addEntry( 'key3', 1.0 );

          cmiaMap.addEntry( 'key4', 2 );

          cmiaMap.addEntry( 'key5', 'value' );

          String rendered = cmiaMap.render();

 

          // assert that render gives you what you expect. I won't attempt to guess what it would be.

          System.assertEquals( 'my expected string', rendered );

}

 

Hope that helps!

 

~ Clint