AEM Mockito unit testing for nested dailog box or multifield in AEM - aem

I am new to implement AEM Mockito testing and want to write junit test cases
for nested dailog box for an example
We have one model class "AccordionListModel" which contain the list of "AccordionModel"
While writing Junit not getting model class reference
Below are the attached files for reference:
(AccordionListModel.java) via adaptTo method
AccordionListModel.java
-AccordionModel.java
-AccordionListModel.json
AccordionListModelTest.java

Related

Which class file to include in the "Class under Test" in the jnit test case creation?

I'm new to the testing environment,i have small confusion in adding the class file,i want to test the restful web services
Which class file to include in the Class under Test in the junit test case creation?
Whether the dao class or bean class or service class.
It doesnt matter, you could skip it. The class-under-test is for writing a single unit test for a single class, where the wizard creates the test method stubs for the methods of the class for you.
When you want to test a REST service, you don't need that and could skip that part. Just create the test and name the test method according to the use case or scenario you want to test.
Using JUnit4, the test itself is even a plain java class, the only thing that makes it special are #Test annotated methods. So you don't even need that entire wizard.
But in case you want to UnitTest the service, you select the service class itself (i.e. the Jax-RS service class annotated with #Path)

Play Framework 2.4 use injected variable in Scala template

I would like to show some data from the database in the menubar of my web page. To get the data, I have a data-access-object (DAO) which is usually created with Guice injection.
How can I use such an (injected) object in my Scala templates?
I could pass it as a parameter to the template, but I had to do this on every single page (because it should be displayed in the menubar). I'm looking for another solution where I don't have to pass it everywhere. Currently I'm creating a new object inside the template, whenever it is rendered (which gets me a cleaner code but a worse performance).
You can kinda-sorta fake this without too much effort.
First, create a Scala object that provides access to your DAO (this can contain as many things as you want, just repeat the pattern within the top-level object and the Implicits object).
package com.example.stuff
object ViewAccessPoint {
private[stuff] val myDaoCache = Application.instanceCache[MyDao]
object Implicits {
implicit def myDao(implicit application: Application): MyDao = myDaoCache(application)
}
}
In your view, you can then import the Implicits object into your template and get hold of the DAO created by Guice.
#import com.example.stuff.ViewAccessPoint.Implicits._
#import play.api.Play.current
myDao.whatever()
This works for both Java and Scala projects.
You can see this in practice here:
Access point
Template
On a side note, I would consider if you really want to be doing data access in your template layer.

JUnit Tests with GWT Editor

In my current project we have a gwt clientapplication based on mvp pattern. Now I have a View that implements Editor and a Presenter who gets the EditorDriver per getter access. The Presenter call edit() and flush() on the EditorDriver.
What I have found is a MockSimpleBeanEditorDriver but nothing like a editormock. A ready to use "MockSimpleBeanEditor" would be nice.
Is there any way to get that working in an JUnit test?
A possible route for you to take is to 'fabricate' a mock editor.
The simplest and elegant way I can think of is Mockito.
Check this out::
Comparable c = mock(Comparable.class);
when(c.compareTo("Test")).thenReturn(1);
assertEquals(1, c.compareTo("Test"));
This code snippet fabricates an instance of Comparable that will return '1' when passed the string 'Test'.
More info here

scala selenium dsl page object

I am using the Selenium dsl and would like to use a Page object. Currently however it seems I have to define the page object inside the test class. The reason I would want a page object is to share common features between tests so this seems a bit pointless... Has anyone been using the page object model with Selenium DSL? What is the idea behind defining the page object in the same class? How come I get a compiler error if I define the page object outside of the test class. Am I doing something wrong?
The compiler error I get is:
Expected MySpec.this.type#Page, actual: MyPage
You can define the class outside of the test class like this:
class TwitterPage {
val url = "http://twitter.com"
}
Then, use it inside the test by mixing in the Page trait:
val page = new TwitterPage with Page
go to page
title should be ("Welcome to Twitter")
This compiled and worked just fine for me.

Using Mockito in GWT project

I am new to using Mockito and I am running through an example test class written in our GWT project.
At some places ,in order to get a Mock we used Mockito.mock(SecurityDao.class)
but in other places in the same test class we instantiated other classes using the "new" keyword.
I think that in order to mock a class i need to pass in the interface as the parameter to Mockito.mock ,and if my class does not implement an interface then i need to use the "new" keyword to instantiate the class.
Is this correct?When should i really use Mockito.mock??
Thanks
Always use Mockito#mock() when creating an object other than that under test. Mockito can create mocks for interfaces and classes.