How to mock web service call?

View: New views
4 Messages — Rating Filter:   Alert me  

How to mock web service call?

by gurm :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,
I am new to Jmock.
Could you please tell me how to test below code?


publi Interface AccountWebService
{
   void addAccount(String accountDetails) throws Exception;
}

public class AccountWebServiceImpl implements AccountWebService
{
   public void addAccount(final String accountDetails) throws Exception
   {
       // Here web service actual implementation.
   }
}


public Interface AccountManager
{
    void addAccount(String accountDetails) throws Exception;
        // Some other methods

}

public class AccountManagerImpl implements AccountManager
{

  public AccountManagerImpl()
  {
  }
 
  public void addAccount(String accountDetails) throws Exception
  {
     AccountWebService accountService = new AccountWebServiceImpl();
     // In some cases getting web service implementation with static methods.
     //Do validation on input parameter.
         //.......
         accountService.addAccount(somerequest)
         
  }
}

How to mock AccountWebService here?
If we are creating objects within method instead of passing as parameters , How to test?

Thanks,
Guru

Re: How to mock web service call?

by Chris Miles-5 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

You have nothing to test for your AccountWebServiceImpl class as it has no
implementation yet.

Your AccountManagerImpl class can be tested that it delegates a call to
addAcccount to the AccountWebService though. Excuse any syntax errors as I
am typing the code straight into the box, but you should get the idea.

In AccountManagerImplTest:

@Test
public void testAddAccountDelegatesToService() {
    Account account = new Account();

    AccountWebService accountWebService =
this.mockery.mock(AccountWebService.class);

    this.mockery.checking(new Expectations() {
        {
            oneOf(accountWebService).addAccount(account);
        }
    });


    AccountManagerImpl accountManagerImpl = new AccountManagerImpl();
    accountManagerImpl.setAccountWebService(accountWebService);
    accountManagerImpl.addAccount(account);
}

You would need to remove the object creation within the addAccount
AccountManagerImpl method and create a setter for the dependency.

When you are testing AccountManagerImpl you want to "mock" any
collaborating objects which are used within it, and in this case it is
your AccountWebService.

Your test will create a mock AccountWebService, and test that the
AccountManagerImpl class correctly calls its collaborating object.

This is just a simple example going by what you have posted as you do not
have any behaviour other than that one method invokation.

If you dont understand that, you might be best to start with the
documentation, and possibly read up some documentation on IoC, depdendency
injection, test driven development etc

Best Regards,

Chris

>
> Hi,
> I am new to Jmock.
> Could you please tell me how to test below code?
>
>
> publi Interface AccountWebService
> {
>    void addAccount(String accountDetails) throws Exception;
> }
>
> public class AccountWebServiceImpl implements AccountWebService
> {
>    public void addAccount(final String accountDetails) throws Exception
>    {
>        // Here web service actual implementation.
>    }
> }
>
>
> public Interface AccountManager
> {
>     void addAccount(String accountDetails) throws Exception;
> // Some other methods
>
> }
>
> public class AccountManagerImpl implements AccountManager
> {
>
>   public AccountManagerImpl()
>   {
>   }
>
>   public void addAccount(String accountDetails) throws Exception
>   {
>      AccountWebService accountService = new AccountWebServiceImpl();
>      // In some cases getting web service implementation with static
> methods.
>      //Do validation on input parameter.
> //.......
> accountService.addAccount(somerequest)
>
>   }
> }
>
> How to mock AccountWebService here?
> If we are creating objects within method instead of passing as parameters
> ,
> How to test?
>
> Thanks,
> Guru
>
> --
> View this message in context:
> http://www.nabble.com/How-to-mock-web-service-call--tp25169189p25169189.html
> Sent from the jMock - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>
>


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Re: How to mock web service call?

by Nat Pryce :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Don't create the "impl" in the class that uses it. Instead, pass a
reference to the interface to it's constructor and store it in a
field. In your real system, pass in a reference to a real
implementation. In a unit test, pass in a reference to a mock object.

--Nat

On Thursday, August 27, 2009, gurm <guru.maheswarareddy@...> wrote:

>
> Hi,
> I am new to Jmock.
> Could you please tell me how to test below code?
>
>
> publi Interface AccountWebService
> {
>    void addAccount(String accountDetails) throws Exception;
> }
>
> public class AccountWebServiceImpl implements AccountWebService
> {
>    public void addAccount(final String accountDetails) throws Exception
>    {
>        // Here web service actual implementation.
>    }
> }
>
>
> public Interface AccountManager
> {
>     void addAccount(String accountDetails) throws Exception;
>         // Some other methods
>
> }
>
> public class AccountManagerImpl implements AccountManager
> {
>
>   public AccountManagerImpl()
>   {
>   }
>
>   public void addAccount(String accountDetails) throws Exception
>   {
>      AccountWebService accountService = new AccountWebServiceImpl();
>      // In some cases getting web service implementation with static
> methods.
>      //Do validation on input parameter.
>          //.......
>          accountService.addAccount(somerequest)
>
>   }
> }
>
> How to mock AccountWebService here?
> If we are creating objects within method instead of passing as parameters ,
> How to test?
>
> Thanks,
> Guru
>
> --
> View this message in context: http://www.nabble.com/How-to-mock-web-service-call--tp25169189p25169189.html
> Sent from the jMock - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>
>

--
http://www.natpryce.com

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Re: How to mock web service call?

by andrew.j.macgilvery :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Hi Guru,
        It's much easier not to test code if you don't create "new" objects in your code.
By using the new operator and creating a concrete class you're binding your code to the implementation.
Try this :

public class AccountManagerImpl implements AccountManager
{
 private AccountWebService accountService;
 public AccountManagerImpl(final AccountWebService accountService)
 {

    this.accountService = accountService;
 }
 
 public void addAccount(String accountDetails) throws Exception
 {
    // In some cases getting web service implementation with static
methods.
    //Do validation on input parameter.
                 //.......
                 this.accountService.addAccount(somerequest)
                 
 }
}


And pass your AccountWebServiceImpl implementation into the AccountManagerImpl in the class that creates it.

Cheers,
        Andy


"gurm" <guru.maheswarareddy@...>

27-Aug-2009 15:30
Please respond to user@...

       
To
user@...
cc
Subject
[jmock-user] How to mock web service call?






Hi,
I am new to Jmock.
Could you please tell me how to test below code?


publi Interface AccountWebService
{
  void addAccount(String accountDetails) throws Exception;
}

public class AccountWebServiceImpl implements AccountWebService
{
  public void addAccount(final String accountDetails) throws Exception
  {
      // Here web service actual implementation.
  }
}


public Interface AccountManager
{
   void addAccount(String accountDetails) throws Exception;
                // Some other methods

}

public class AccountManagerImpl implements AccountManager
{

 public AccountManagerImpl()
 {
 }
 
 public void addAccount(String accountDetails) throws Exception
 {
    AccountWebService accountService = new AccountWebServiceImpl();
    // In some cases getting web service implementation with static
methods.
    //Do validation on input parameter.
                 //.......
                 accountService.addAccount(somerequest)
                 
 }
}

How to mock AccountWebService here?
If we are creating objects within method instead of passing as parameters ,
How to test?

Thanks,
Guru

--
View this message in context: http://www.nabble.com/How-to-mock-web-service-call--tp25169189p25169189.html
Sent from the jMock - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email




-----------------------------------------------------------
This e-mail was sent by GlaxoSmithKline Services Unlimited 
(registered in England and Wales No. 1047315), which is a 
member of the GlaxoSmithKline group of companies. The 
registered address of GlaxoSmithKline Services Unlimited 
is 980 Great West Road, Brentford, Middlesex TW8 9GS.
-----------------------------------------------------------