You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@cloudstack.apache.org by Meghna Kale <me...@sungard.com> on 2012/10/29 17:51:05 UTC

need help for test cases of api/commands

Hi All,



I was writing some test cases for the API layer
api\src\com\cloud\api\commands.

I facing some issue can someone help me.



There are some service layer calls in the classes , I tried to mock the
service layer.

The Service layer internally calls the DAO, rather than calling the mock
DAO I was thinking  of returning the object from the service layer.

But as the return value of the DAO is an interface in the service layer I’m
not able to mock the object and pass it on to the API layer.



Is any other way or I will have to mock the DAO layer also ?



-          Meghna.

Re: need help for test cases of api/commands

Posted by Chiradeep Vittal <Ch...@citrix.com>.
Sounds reasonable.

On 10/31/12 12:05 PM, "Meghna Kale" <me...@sungard.com> wrote:

>Chiradeep,
>
>Thanks for the response.
>I tried making the ProjectService a Mockito object, and it worked.
>The only problem that I faced was to pass the "id"   in the call
>(referring to ActivateProjectCmd.java).  I was not able to assign a value
>to" id " parameter as it is a private member.
>
>               Project project= _projectService.getProject(id);
>
>Can we make use of the public getId() method rather than using "id"
>directly in the code.
>
>So I had to set the id value in the setup() method or else it throws
>nullPointerException for not finding the id.
>
>public class ActivateProjectCmdTest extends TestCase {
>	
>	private ActivateProjectCmd activateProjectCmd;
>	private Parameter parameter;
>	
>
>	@Before
>	public void setUp() {
>		activateProjectCmd = new ActivateProjectCmd(){
>			
>			@Override
>			public Long getId() {
>				return 2L;
>			}
>		};
>	}
>
>	@Test
>	public void testGetEntityOwnerIdForProject() {
>		Project project = Mockito.mock(Project.class);
>		Mockito.when(project.getId()).thenReturn(2L);
>		ProjectService projectService =
>Mockito.mock(ProjectService.class);
>		Account account = Mockito.mock(Account.class);
>		Mockito.when(account.getId()).thenReturn(2L);
>	
>Mockito.when(projectService.getProject(Mockito.anyLong())).thenReturn(proj
>ect);
>		
>	
>Mockito.when(projectService.getProjectOwner(Mockito.anyLong())).thenReturn
>(account);
>		activateProjectCmd._projectService = projectService;
>		
>		Assert.assertEquals(2L,
>activateProjectCmd.getEntityOwnerId());
>		
>	}
>}
>
>- Meghna.
>
>
>
>-----Original Message-----
>From: Chiradeep Vittal [mailto:Chiradeep.Vittal@citrix.com]
>Sent: Wednesday, October 31, 2012 12:50 AM
>To: Meghna Kale; Alex Huang
>Cc: CloudStack DeveloperList
>Subject: Re: need help for test cases of api/commands
>
>
>
>On 10/30/12 12:12 PM, "Meghna Kale" <me...@sungard.com> wrote:
>
>>_projectService =
>>ComponentLocator.inject(MockProjectServiceImpl.class);
>
>When you do this,  the _projectService is no longer a Mockito object.


RE: need help for test cases of api/commands

Posted by Meghna Kale <me...@sungard.com>.
Chiradeep,

Thanks for the response.
I tried making the ProjectService a Mockito object, and it worked.
The only problem that I faced was to pass the "id"   in the call
(referring to ActivateProjectCmd.java).  I was not able to assign a value
to" id " parameter as it is a private member.

               Project project= _projectService.getProject(id);

Can we make use of the public getId() method rather than using "id"
directly in the code.

So I had to set the id value in the setup() method or else it throws
nullPointerException for not finding the id.

public class ActivateProjectCmdTest extends TestCase {
	
	private ActivateProjectCmd activateProjectCmd;
	private Parameter parameter;
	

	@Before
	public void setUp() {
		activateProjectCmd = new ActivateProjectCmd(){
			
			@Override
			public Long getId() {
				return 2L;
			}
		};
	}

	@Test
	public void testGetEntityOwnerIdForProject() {
		Project project = Mockito.mock(Project.class);
		Mockito.when(project.getId()).thenReturn(2L);
		ProjectService projectService =
Mockito.mock(ProjectService.class);
		Account account = Mockito.mock(Account.class);
		Mockito.when(account.getId()).thenReturn(2L);
	
Mockito.when(projectService.getProject(Mockito.anyLong())).thenReturn(proj
ect);
		
	
Mockito.when(projectService.getProjectOwner(Mockito.anyLong())).thenReturn
(account);
		activateProjectCmd._projectService = projectService;
		
		Assert.assertEquals(2L,
activateProjectCmd.getEntityOwnerId());
		
	}
}

- Meghna.



-----Original Message-----
From: Chiradeep Vittal [mailto:Chiradeep.Vittal@citrix.com]
Sent: Wednesday, October 31, 2012 12:50 AM
To: Meghna Kale; Alex Huang
Cc: CloudStack DeveloperList
Subject: Re: need help for test cases of api/commands



On 10/30/12 12:12 PM, "Meghna Kale" <me...@sungard.com> wrote:

>_projectService =
>ComponentLocator.inject(MockProjectServiceImpl.class);

When you do this,  the _projectService is no longer a Mockito object.

RE: need help for test cases of api/commands

Posted by Meghna Kale <me...@sungard.com>.
Do you mean to use mock(ProjectManagerImpl.class) rather than creating
MockProjectServiceImpl.java

In this case
projectService = ComponentLocator.inject(mock(ProjectManagerImpl.class));


-----Original Message-----
From: Chiradeep Vittal [mailto:Chiradeep.Vittal@citrix.com]
Sent: Wednesday, October 31, 2012 12:50 AM
To: Meghna Kale; Alex Huang
Cc: CloudStack DeveloperList
Subject: Re: need help for test cases of api/commands



On 10/30/12 12:12 PM, "Meghna Kale" <me...@sungard.com> wrote:

>_projectService =
>ComponentLocator.inject(MockProjectServiceImpl.class);

When you do this,  the _projectService is no longer a Mockito object.

Re: need help for test cases of api/commands

Posted by Chiradeep Vittal <Ch...@citrix.com>.

On 10/30/12 12:12 PM, "Meghna Kale" <me...@sungard.com> wrote:

>_projectService =
>ComponentLocator.inject(MockProjectServiceImpl.class);

When you do this,  the _projectService is no longer a Mockito object.


RE: need help for test cases of api/commands

Posted by Meghna Kale <me...@sungard.com>.
Hi,

I'm trying with Mockito/Junit. Here's my test class for
ActivateProjectCmd.java

@RunWith(MockitoJUnitRunner.class)
public class Test {

	private ActivateProjectCmd activateProjectCmd;
	@Mock private ProjectService _projectService;

	@Before
	public void setUp() {
		activateProjectCmd = new ActivateProjectCmd();
		MockComponentLocator locator = new
MockComponentLocator(ManagementService.Name);
		locator.addManager("ProjectManager",
MockProjectServiceImpl.class);
		_projectService =
ComponentLocator.inject(MockProjectServiceImpl.class);

	}

	@org.junit.Test
	public void testGetEntityOwnerIdForNotNullProject() throws
Exception {


when(_projectService.getProject(anyLong())).thenReturn(mock(Project.class)
);
//	exception.expect(InvalidParameterValueException.class);
	activateProjectCmd.getEntityOwnerId();

	}

}


But it fails with an error as ,

org.mockito.exceptions.misusing.MissingMethodInvocationException:
when() requires an argument which has to be a method call on a mock.
For example:
    when(mock.getArticles()).thenReturn(articles);

Also, this error might show up because you stub either of:
final/private/equals()/hashCode() methods.
Those methods *cannot* be stubbed/verified.

	at
com.cloud.api.commands.Test.testGetEntityOwnerIdForNull(Test.java:49)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

....

Trying to figure out this issue.

- Meghna.

-----Original Message-----
From: Chiradeep Vittal [mailto:Chiradeep.Vittal@citrix.com]
Sent: Tuesday, October 30, 2012 11:30 PM
To: Alex Huang; Meghna Kale
Cc: CloudStack DeveloperList
Subject: Re: need help for test cases of api/commands

I think Meghna's problem is that she has to mock the Project interface
since the DAO returns Project instead of a concrete class ProjectVO.
If we used Mockito, it would be as trivial as

return mock(Project.class)

--
Chiradeep

On 10/29/12 1:30 PM, "Alex Huang" <Al...@citrix.com> wrote:

>Meghna,
>
>I see that there's MockProjectManagerImpl.  So basically, you're trying
>to mock the service itself?
>
>You can use your own components.xml that has all the Mock classes.
>Take a look at components.xml, it has a reference to
>DefaultComponentLibrary which contains code to return different
>managers.  You don't have to write your own componentlibrary because
>the components.xml can also specify manager implementations.  You can add
the following:
>
><manager name="name" class="class"/>
>
>You can even pass paramters to the implementation.
>
><manager name="name" class="class">
>  <param name="name">"value"</param>
></manager>
>
>So in this case, you probably want something like
>
><manager name="ProjectManager"
>class="com.cloud.projects.MockProjectManagerImpl">
>  <param name="abc">def</param>
></manager>
>
>You can also write your own components.xml and set the following
>parameter in environment.properties to reference your components.xml.
>
>cloud-stack-components-specification=[path to your own components.xml]
>
>Let me know if you have any questions.
>
>
>--Alex
>
>> -----Original Message-----
>> From: Meghna Kale [mailto:meghna.kale@sungard.com]
>> Sent: Monday, October 29, 2012 12:23 PM
>> To: Chiradeep Vittal
>> Cc: cloudstack-dev@incubator.apache.org; Alex Huang
>> Subject: RE: need help for test cases of api/commands
>>
>> I was trying with mocks.
>>
>> -----Original Message-----
>> From: Chiradeep Vittal [mailto:Chiradeep.Vittal@citrix.com]
>> Sent: Tuesday, October 30, 2012 12:04 AM
>> To: CloudStack DeveloperList; Alex Huang
>> Subject: Re: need help for test cases of api/commands
>>
>> Are you writing mocks, or are you planning to use a framework like
>>Mockito?
>>
>> On 10/29/12 11:29 AM, "Meghna Kale" <me...@sungard.com> wrote:
>>
>> >Alex,
>> >
>> >A method from ActivateProjectCmd.java, the projectService call.
>> >
>> >     @Override
>> >
>> >    public long getEntityOwnerId() {
>> >
>> >        Project project= _projectService.getProject(id);
>> >
>> >        //verify input parameters
>> >
>> >        if (project == null) {
>> >
>> >                     throw new
>> >InvalidParameterValueException("Unable
>> >to find project by id " + id);
>> >
>> >        }
>> >
>> >       return _projectService.getProjectOwner(id).getId();
>> >
>> >    }
>> >
>> >
>> >
>> > I created a mock class of the service
>> >
>> >
>> >
>> >@Local(value = { ProjectService.*class* })
>> >
>> >*public* *class* MockProjectServiceImpl *implements* ProjectService,
>> >Manager{
>> >
>> >    public static final Logger s_logger =
>> >Logger.getLogger(MockProjectServiceImpl.class);
>> >
>> >
>> >
>> >       @Override
>> >
>> >       *public* *boolean* deleteProject(*long* id) {
>> >
>> >              // *TODO* Auto-generated method stub
>> >
>> >              *return* *false*;
>> >
>> >       }
>> >
>> >
>> >
>> >       @Override
>> >
>> >       *public* Project getProject(*long* id) {
>> >
>> >              // *TODO* Auto-generated method stub
>> >
>> >
>> >
>> >         // We Cannot mock the Project object as it an interface and
>> >return from this method.
>> >
>> >              *return* *null*;
>> >
>> >       }
>> >
>> >ŠŠŠŠ..
>> >
>> >
>> >
>> >}
>> >
>> >
>> >
>> >--   -  Meghna.
>> >
>> >
>> >
>> >
>> >On Mon, Oct 29, 2012 at 11:42 PM, Alex Huang <Al...@citrix.com>
>> >wrote:
>> >
>> >> Meghna,
>> >>
>> >> Can you give an example of which call you're talking about and
>> >> point out the specific line of code you're having problem with?
>> >>
>> >> Thanks.
>> >>
>> >> --Alex
>> >>
>> >> > -----Original Message-----
>> >> > From: Meghna Kale [mailto:meghna.kale@sungard.com]
>> >> > Sent: Monday, October 29, 2012 9:51 AM
>> >> > To: cloudstack-dev@incubator.apache.org
>> >> > Subject: need help for test cases of api/commands
>> >> >
>> >> > Hi All,
>> >> >
>> >> >
>> >> >
>> >> > I was writing some test cases for the API layer
>> >> > api\src\com\cloud\api\commands.
>> >> >
>> >> > I facing some issue can someone help me.
>> >> >
>> >> >
>> >> >
>> >> > There are some service layer calls in the classes , I tried to
>> >> > mock
>> >>the
>> >> service
>> >> > layer.
>> >> >
>> >> > The Service layer internally calls the DAO, rather than calling
>> >> > the
>> >>mock
>> >> DAO I
>> >> > was thinking  of returning the object from the service layer.
>> >> >
>> >> > But as the return value of the DAO is an interface in the
>> >> > service
>> >>layer
>> >> I'm not
>> >> > able to mock the object and pass it on to the API layer.
>> >> >
>> >> >
>> >> >
>> >> > Is any other way or I will have to mock the DAO layer also ?
>> >> >
>> >> >
>> >> >
>> >> > -          Meghna.
>> >>
>> >>

Re: need help for test cases of api/commands

Posted by Chiradeep Vittal <Ch...@citrix.com>.
I think Meghna's problem is that she has to mock the Project interface
since the DAO returns Project instead of a concrete class ProjectVO.
If we used Mockito, it would be as trivial as

return mock(Project.class)

--
Chiradeep

On 10/29/12 1:30 PM, "Alex Huang" <Al...@citrix.com> wrote:

>Meghna,
>
>I see that there's MockProjectManagerImpl.  So basically, you're trying
>to mock the service itself?
>
>You can use your own components.xml that has all the Mock classes.  Take
>a look at components.xml, it has a reference to DefaultComponentLibrary
>which contains code to return different managers.  You don't have to
>write your own componentlibrary because the components.xml can also
>specify manager implementations.  You can add the following:
>
><manager name="name" class="class"/>
>
>You can even pass paramters to the implementation.
>
><manager name="name" class="class">
>  <param name="name">"value"</param>
></manager>
>
>So in this case, you probably want something like
>
><manager name="ProjectManager"
>class="com.cloud.projects.MockProjectManagerImpl">
>  <param name="abc">def</param>
></manager>
>
>You can also write your own components.xml and set the following
>parameter in environment.properties to reference your components.xml.
>
>cloud-stack-components-specification=[path to your own components.xml]
>
>Let me know if you have any questions.
>
>
>--Alex
>
>> -----Original Message-----
>> From: Meghna Kale [mailto:meghna.kale@sungard.com]
>> Sent: Monday, October 29, 2012 12:23 PM
>> To: Chiradeep Vittal
>> Cc: cloudstack-dev@incubator.apache.org; Alex Huang
>> Subject: RE: need help for test cases of api/commands
>> 
>> I was trying with mocks.
>> 
>> -----Original Message-----
>> From: Chiradeep Vittal [mailto:Chiradeep.Vittal@citrix.com]
>> Sent: Tuesday, October 30, 2012 12:04 AM
>> To: CloudStack DeveloperList; Alex Huang
>> Subject: Re: need help for test cases of api/commands
>> 
>> Are you writing mocks, or are you planning to use a framework like
>>Mockito?
>> 
>> On 10/29/12 11:29 AM, "Meghna Kale" <me...@sungard.com> wrote:
>> 
>> >Alex,
>> >
>> >A method from ActivateProjectCmd.java, the projectService call.
>> >
>> >     @Override
>> >
>> >    public long getEntityOwnerId() {
>> >
>> >        Project project= _projectService.getProject(id);
>> >
>> >        //verify input parameters
>> >
>> >        if (project == null) {
>> >
>> >                     throw new InvalidParameterValueException("Unable
>> >to find project by id " + id);
>> >
>> >        }
>> >
>> >       return _projectService.getProjectOwner(id).getId();
>> >
>> >    }
>> >
>> >
>> >
>> > I created a mock class of the service
>> >
>> >
>> >
>> >@Local(value = { ProjectService.*class* })
>> >
>> >*public* *class* MockProjectServiceImpl *implements* ProjectService,
>> >Manager{
>> >
>> >    public static final Logger s_logger =
>> >Logger.getLogger(MockProjectServiceImpl.class);
>> >
>> >
>> >
>> >       @Override
>> >
>> >       *public* *boolean* deleteProject(*long* id) {
>> >
>> >              // *TODO* Auto-generated method stub
>> >
>> >              *return* *false*;
>> >
>> >       }
>> >
>> >
>> >
>> >       @Override
>> >
>> >       *public* Project getProject(*long* id) {
>> >
>> >              // *TODO* Auto-generated method stub
>> >
>> >
>> >
>> >         // We Cannot mock the Project object as it an interface and
>> >return from this method.
>> >
>> >              *return* *null*;
>> >
>> >       }
>> >
>> >ŠŠŠŠ..
>> >
>> >
>> >
>> >}
>> >
>> >
>> >
>> >--   -  Meghna.
>> >
>> >
>> >
>> >
>> >On Mon, Oct 29, 2012 at 11:42 PM, Alex Huang <Al...@citrix.com>
>> >wrote:
>> >
>> >> Meghna,
>> >>
>> >> Can you give an example of which call you're talking about and point
>> >> out the specific line of code you're having problem with?
>> >>
>> >> Thanks.
>> >>
>> >> --Alex
>> >>
>> >> > -----Original Message-----
>> >> > From: Meghna Kale [mailto:meghna.kale@sungard.com]
>> >> > Sent: Monday, October 29, 2012 9:51 AM
>> >> > To: cloudstack-dev@incubator.apache.org
>> >> > Subject: need help for test cases of api/commands
>> >> >
>> >> > Hi All,
>> >> >
>> >> >
>> >> >
>> >> > I was writing some test cases for the API layer
>> >> > api\src\com\cloud\api\commands.
>> >> >
>> >> > I facing some issue can someone help me.
>> >> >
>> >> >
>> >> >
>> >> > There are some service layer calls in the classes , I tried to mock
>> >>the
>> >> service
>> >> > layer.
>> >> >
>> >> > The Service layer internally calls the DAO, rather than calling the
>> >>mock
>> >> DAO I
>> >> > was thinking  of returning the object from the service layer.
>> >> >
>> >> > But as the return value of the DAO is an interface in the service
>> >>layer
>> >> I'm not
>> >> > able to mock the object and pass it on to the API layer.
>> >> >
>> >> >
>> >> >
>> >> > Is any other way or I will have to mock the DAO layer also ?
>> >> >
>> >> >
>> >> >
>> >> > -          Meghna.
>> >>
>> >>


RE: need help for test cases of api/commands

Posted by Alex Huang <Al...@citrix.com>.
Meghna,

I see that there's MockProjectManagerImpl.  So basically, you're trying to mock the service itself?  

You can use your own components.xml that has all the Mock classes.  Take a look at components.xml, it has a reference to DefaultComponentLibrary which contains code to return different managers.  You don't have to write your own componentlibrary because the components.xml can also specify manager implementations.  You can add the following:

<manager name="name" class="class"/>

You can even pass paramters to the implementation.

<manager name="name" class="class">
  <param name="name">"value"</param>
</manager>

So in this case, you probably want something like

<manager name="ProjectManager" class="com.cloud.projects.MockProjectManagerImpl">
  <param name="abc">def</param>
</manager>

You can also write your own components.xml and set the following parameter in environment.properties to reference your components.xml.

cloud-stack-components-specification=[path to your own components.xml]

Let me know if you have any questions.


--Alex

> -----Original Message-----
> From: Meghna Kale [mailto:meghna.kale@sungard.com]
> Sent: Monday, October 29, 2012 12:23 PM
> To: Chiradeep Vittal
> Cc: cloudstack-dev@incubator.apache.org; Alex Huang
> Subject: RE: need help for test cases of api/commands
> 
> I was trying with mocks.
> 
> -----Original Message-----
> From: Chiradeep Vittal [mailto:Chiradeep.Vittal@citrix.com]
> Sent: Tuesday, October 30, 2012 12:04 AM
> To: CloudStack DeveloperList; Alex Huang
> Subject: Re: need help for test cases of api/commands
> 
> Are you writing mocks, or are you planning to use a framework like Mockito?
> 
> On 10/29/12 11:29 AM, "Meghna Kale" <me...@sungard.com> wrote:
> 
> >Alex,
> >
> >A method from ActivateProjectCmd.java, the projectService call.
> >
> >     @Override
> >
> >    public long getEntityOwnerId() {
> >
> >        Project project= _projectService.getProject(id);
> >
> >        //verify input parameters
> >
> >        if (project == null) {
> >
> >                     throw new InvalidParameterValueException("Unable
> >to find project by id " + id);
> >
> >        }
> >
> >       return _projectService.getProjectOwner(id).getId();
> >
> >    }
> >
> >
> >
> > I created a mock class of the service
> >
> >
> >
> >@Local(value = { ProjectService.*class* })
> >
> >*public* *class* MockProjectServiceImpl *implements* ProjectService,
> >Manager{
> >
> >    public static final Logger s_logger =
> >Logger.getLogger(MockProjectServiceImpl.class);
> >
> >
> >
> >       @Override
> >
> >       *public* *boolean* deleteProject(*long* id) {
> >
> >              // *TODO* Auto-generated method stub
> >
> >              *return* *false*;
> >
> >       }
> >
> >
> >
> >       @Override
> >
> >       *public* Project getProject(*long* id) {
> >
> >              // *TODO* Auto-generated method stub
> >
> >
> >
> >         // We Cannot mock the Project object as it an interface and
> >return from this method.
> >
> >              *return* *null*;
> >
> >       }
> >
> >ŠŠŠŠ..
> >
> >
> >
> >}
> >
> >
> >
> >--   -  Meghna.
> >
> >
> >
> >
> >On Mon, Oct 29, 2012 at 11:42 PM, Alex Huang <Al...@citrix.com>
> >wrote:
> >
> >> Meghna,
> >>
> >> Can you give an example of which call you're talking about and point
> >> out the specific line of code you're having problem with?
> >>
> >> Thanks.
> >>
> >> --Alex
> >>
> >> > -----Original Message-----
> >> > From: Meghna Kale [mailto:meghna.kale@sungard.com]
> >> > Sent: Monday, October 29, 2012 9:51 AM
> >> > To: cloudstack-dev@incubator.apache.org
> >> > Subject: need help for test cases of api/commands
> >> >
> >> > Hi All,
> >> >
> >> >
> >> >
> >> > I was writing some test cases for the API layer
> >> > api\src\com\cloud\api\commands.
> >> >
> >> > I facing some issue can someone help me.
> >> >
> >> >
> >> >
> >> > There are some service layer calls in the classes , I tried to mock
> >>the
> >> service
> >> > layer.
> >> >
> >> > The Service layer internally calls the DAO, rather than calling the
> >>mock
> >> DAO I
> >> > was thinking  of returning the object from the service layer.
> >> >
> >> > But as the return value of the DAO is an interface in the service
> >>layer
> >> I'm not
> >> > able to mock the object and pass it on to the API layer.
> >> >
> >> >
> >> >
> >> > Is any other way or I will have to mock the DAO layer also ?
> >> >
> >> >
> >> >
> >> > -          Meghna.
> >>
> >>

RE: need help for test cases of api/commands

Posted by Meghna Kale <me...@sungard.com>.
I was trying with mocks.

-----Original Message-----
From: Chiradeep Vittal [mailto:Chiradeep.Vittal@citrix.com]
Sent: Tuesday, October 30, 2012 12:04 AM
To: CloudStack DeveloperList; Alex Huang
Subject: Re: need help for test cases of api/commands

Are you writing mocks, or are you planning to use a framework like
Mockito?

On 10/29/12 11:29 AM, "Meghna Kale" <me...@sungard.com> wrote:

>Alex,
>
>A method from ActivateProjectCmd.java, the projectService call.
>
>     @Override
>
>    public long getEntityOwnerId() {
>
>        Project project= _projectService.getProject(id);
>
>        //verify input parameters
>
>        if (project == null) {
>
>                     throw new InvalidParameterValueException("Unable
>to find project by id " + id);
>
>        }
>
>       return _projectService.getProjectOwner(id).getId();
>
>    }
>
>
>
> I created a mock class of the service
>
>
>
>@Local(value = { ProjectService.*class* })
>
>*public* *class* MockProjectServiceImpl *implements* ProjectService,
>Manager{
>
>    public static final Logger s_logger =
>Logger.getLogger(MockProjectServiceImpl.class);
>
>
>
>       @Override
>
>       *public* *boolean* deleteProject(*long* id) {
>
>              // *TODO* Auto-generated method stub
>
>              *return* *false*;
>
>       }
>
>
>
>       @Override
>
>       *public* Project getProject(*long* id) {
>
>              // *TODO* Auto-generated method stub
>
>
>
>         // We Cannot mock the Project object as it an interface and
>return from this method.
>
>              *return* *null*;
>
>       }
>
>ŠŠŠŠ..
>
>
>
>}
>
>
>
>--   -  Meghna.
>
>
>
>
>On Mon, Oct 29, 2012 at 11:42 PM, Alex Huang <Al...@citrix.com>
>wrote:
>
>> Meghna,
>>
>> Can you give an example of which call you're talking about and point
>> out the specific line of code you're having problem with?
>>
>> Thanks.
>>
>> --Alex
>>
>> > -----Original Message-----
>> > From: Meghna Kale [mailto:meghna.kale@sungard.com]
>> > Sent: Monday, October 29, 2012 9:51 AM
>> > To: cloudstack-dev@incubator.apache.org
>> > Subject: need help for test cases of api/commands
>> >
>> > Hi All,
>> >
>> >
>> >
>> > I was writing some test cases for the API layer
>> > api\src\com\cloud\api\commands.
>> >
>> > I facing some issue can someone help me.
>> >
>> >
>> >
>> > There are some service layer calls in the classes , I tried to mock
>>the
>> service
>> > layer.
>> >
>> > The Service layer internally calls the DAO, rather than calling the
>>mock
>> DAO I
>> > was thinking  of returning the object from the service layer.
>> >
>> > But as the return value of the DAO is an interface in the service
>>layer
>> I'm not
>> > able to mock the object and pass it on to the API layer.
>> >
>> >
>> >
>> > Is any other way or I will have to mock the DAO layer also ?
>> >
>> >
>> >
>> > -          Meghna.
>>
>>

Re: need help for test cases of api/commands

Posted by Chiradeep Vittal <Ch...@citrix.com>.
Are you writing mocks, or are you planning to use a framework like Mockito?

On 10/29/12 11:29 AM, "Meghna Kale" <me...@sungard.com> wrote:

>Alex,
>
>A method from ActivateProjectCmd.java, the projectService call.
>
>     @Override
>
>    public long getEntityOwnerId() {
>
>        Project project= _projectService.getProject(id);
>
>        //verify input parameters
>
>        if (project == null) {
>
>                     throw new InvalidParameterValueException("Unable to
>find project by id " + id);
>
>        }
>
>       return _projectService.getProjectOwner(id).getId();
>
>    }
>
>
>
> I created a mock class of the service
>
>
>
>@Local(value = { ProjectService.*class* })
>
>*public* *class* MockProjectServiceImpl *implements* ProjectService,
>Manager{
>
>    public static final Logger s_logger =
>Logger.getLogger(MockProjectServiceImpl.class);
>
>
>
>       @Override
>
>       *public* *boolean* deleteProject(*long* id) {
>
>              // *TODO* Auto-generated method stub
>
>              *return* *false*;
>
>       }
>
>
>
>       @Override
>
>       *public* Project getProject(*long* id) {
>
>              // *TODO* Auto-generated method stub
>
>
>
>         // We Cannot mock the Project object as it an interface and
>return
>from this method.
>
>              *return* *null*;
>
>       }
>
>ŠŠŠŠ..
>
>
>
>}
>
>
>
>--   -  Meghna.
>
>
>
>
>On Mon, Oct 29, 2012 at 11:42 PM, Alex Huang <Al...@citrix.com>
>wrote:
>
>> Meghna,
>>
>> Can you give an example of which call you're talking about and point out
>> the specific line of code you're having problem with?
>>
>> Thanks.
>>
>> --Alex
>>
>> > -----Original Message-----
>> > From: Meghna Kale [mailto:meghna.kale@sungard.com]
>> > Sent: Monday, October 29, 2012 9:51 AM
>> > To: cloudstack-dev@incubator.apache.org
>> > Subject: need help for test cases of api/commands
>> >
>> > Hi All,
>> >
>> >
>> >
>> > I was writing some test cases for the API layer
>> > api\src\com\cloud\api\commands.
>> >
>> > I facing some issue can someone help me.
>> >
>> >
>> >
>> > There are some service layer calls in the classes , I tried to mock
>>the
>> service
>> > layer.
>> >
>> > The Service layer internally calls the DAO, rather than calling the
>>mock
>> DAO I
>> > was thinking  of returning the object from the service layer.
>> >
>> > But as the return value of the DAO is an interface in the service
>>layer
>> I'm not
>> > able to mock the object and pass it on to the API layer.
>> >
>> >
>> >
>> > Is any other way or I will have to mock the DAO layer also ?
>> >
>> >
>> >
>> > -          Meghna.
>>
>>


Re: need help for test cases of api/commands

Posted by Meghna Kale <me...@sungard.com>.
Alex,

A method from ActivateProjectCmd.java, the projectService call.

     @Override

    public long getEntityOwnerId() {

        Project project= _projectService.getProject(id);

        //verify input parameters

        if (project == null) {

                     throw new InvalidParameterValueException("Unable to
find project by id " + id);

        }

       return _projectService.getProjectOwner(id).getId();

    }



 I created a mock class of the service



@Local(value = { ProjectService.*class* })

*public* *class* MockProjectServiceImpl *implements* ProjectService,
Manager{

    public static final Logger s_logger =
Logger.getLogger(MockProjectServiceImpl.class);



       @Override

       *public* *boolean* deleteProject(*long* id) {

              // *TODO* Auto-generated method stub

              *return* *false*;

       }



       @Override

       *public* Project getProject(*long* id) {

              // *TODO* Auto-generated method stub



         // We Cannot mock the Project object as it an interface and return
from this method.

              *return* *null*;

       }

…………..



}



--   -  Meghna.




On Mon, Oct 29, 2012 at 11:42 PM, Alex Huang <Al...@citrix.com> wrote:

> Meghna,
>
> Can you give an example of which call you're talking about and point out
> the specific line of code you're having problem with?
>
> Thanks.
>
> --Alex
>
> > -----Original Message-----
> > From: Meghna Kale [mailto:meghna.kale@sungard.com]
> > Sent: Monday, October 29, 2012 9:51 AM
> > To: cloudstack-dev@incubator.apache.org
> > Subject: need help for test cases of api/commands
> >
> > Hi All,
> >
> >
> >
> > I was writing some test cases for the API layer
> > api\src\com\cloud\api\commands.
> >
> > I facing some issue can someone help me.
> >
> >
> >
> > There are some service layer calls in the classes , I tried to mock the
> service
> > layer.
> >
> > The Service layer internally calls the DAO, rather than calling the mock
> DAO I
> > was thinking  of returning the object from the service layer.
> >
> > But as the return value of the DAO is an interface in the service layer
> I'm not
> > able to mock the object and pass it on to the API layer.
> >
> >
> >
> > Is any other way or I will have to mock the DAO layer also ?
> >
> >
> >
> > -          Meghna.
>
>

RE: need help for test cases of api/commands

Posted by Alex Huang <Al...@citrix.com>.
Meghna,

Can you give an example of which call you're talking about and point out the specific line of code you're having problem with?

Thanks.

--Alex

> -----Original Message-----
> From: Meghna Kale [mailto:meghna.kale@sungard.com]
> Sent: Monday, October 29, 2012 9:51 AM
> To: cloudstack-dev@incubator.apache.org
> Subject: need help for test cases of api/commands
> 
> Hi All,
> 
> 
> 
> I was writing some test cases for the API layer
> api\src\com\cloud\api\commands.
> 
> I facing some issue can someone help me.
> 
> 
> 
> There are some service layer calls in the classes , I tried to mock the service
> layer.
> 
> The Service layer internally calls the DAO, rather than calling the mock DAO I
> was thinking  of returning the object from the service layer.
> 
> But as the return value of the DAO is an interface in the service layer I'm not
> able to mock the object and pass it on to the API layer.
> 
> 
> 
> Is any other way or I will have to mock the DAO layer also ?
> 
> 
> 
> -          Meghna.