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.
I'm developing an application with React mobx. I have a complex data on the Java server side. How should I keep my data in my mobx store in for this data? I've read a lot about it. But I couldn't decide how to do it.
I have lots of classes nested. I'm just writing a part.
How should I keep nested classes? Or should I use inheritance in mobx? Thank you from now.
On the Java side I am holding List of PlanManagement.
public class Template {
private String templateName;
private String templateId;
.....
}
public class Group {
private String groupId;
private String groupName;
private String groupType;
private Template template;
....
}
public class PlanManagement {
private String id;
private String text;
private Group group;
.....
}
public class PlanCore extends PlanManagement {
private PlanStatusEnum statusValue;
private String statusType;
..............
}
public class OrderCore extends PlanManagement {
private OrderStatusEnum statusValue;
private String status;
private String task;
............
}
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.
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.
What is the use if we use private static final to create an object of a class which is in another package..??
package pack1;
class C1{
...
}
class L1{
...
}
package pack2;
import pack1.C1;
import pack1.L1;
public class Main{
private static final C1 c1=new C1();
private static final L1 l1=new L1();
public static void main(String args[]){
...
}
}
The first result on Google!
http://geekwhorled.blogspot.com/2004/07/simple-java-questions-1-private-static.html