sparks | 2 Jul 11:57

How to test a method that is wrapped in a private method?

Hi all,
I am quite new to jMock and I am using jMock 2.4.0. 

I am testing a service class which uses a 
DAO object to read and write data and
the DAO object has been mocked out. 

The service has a private method which delegates 
all the calls to the DAO 
object. The code is like following:

public class ServiceClass {
    private DAO dao;

    public void aOperation(){
        //some logics here
        updateData(data);
    }

    public void anotherOperation(){
        //some other logics here
        updateData(data);
    }

    private void updateData(Data someData){
        //pre processing......

        dao.update(someData);

        //post processing......
(Continue reading)

Nat Pryce | 2 Jul 12:42

Re: How to test a method that is wrapped in a private method?

You're on the right track.

But... how does the ServiceClass object get a reference to the DAO it
uses?  In your test you need to give it a mock instance, not a real
one.

--Nat

2008/7/2 sparks <ychen.lu@...>:
> Hi all,
> I am quite new to jMock and I am using jMock 2.4.0.
>
> I am testing a service class which uses a
> DAO object to read and write data and
> the DAO object has been mocked out.
>
> The service has a private method which delegates
> all the calls to the DAO
> object. The code is like following:
>
> public class ServiceClass {
>    private DAO dao;
>
>    public void aOperation(){
>        //some logics here
>        updateData(data);
>    }
>
>    public void anotherOperation(){
>        //some other logics here
(Continue reading)

sparks | 2 Jul 14:30

Re: How to test a method that is wrapped in a private method?

Nat Pryce <nat.pryce@...> writes:
> 
> You're on the right track.
> 
> But... how does the ServiceClass object get a reference to the DAO it
> uses?  In your test you need to give it a mock instance, not a real
> one.
> 
> --Nat

Thanks for your reply Nat. The ServiceClass has a setter method for the DAO so
that the test class can inject the mock DAO into the ServiceClass and I am sure
the setter is called before the method to test is called.

Anymore hints?

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

    http://xircles.codehaus.org/manage_email

Nat Pryce | 2 Jul 15:41

Re: Re: How to test a method that is wrapped in a private method?

2008/7/2 sparks <ychen.lu@...>:
> The ServiceClass has a setter method for the DAO so
> that the test class can inject the mock DAO into the ServiceClass and I am sure
> the setter is called before the method to test is called.
>
> Anymore hints?

It could be that the dao is not being set by the setter.  Instead of a
setter, use a constructor parameter and make the dao field final, so
that the setter can never get hold of the wrong DAO and it cannot
change after construction.

It could be that the data being passed to the DAO does not have an
equals method defined for it, so two different objects that represent
the same value compare as being different.  If the data being passed
to the DAO is just a value, then it should be made immutable and given
an equals method.  If it is mutable -- changes over time -- then it
should not be given an equals method.  Instead you could write a
Matcher to check that the data passed to the DAO has the expected
state (see the website).

Without looking at the code, it could be anything!  If you want more
help, create a tiny example that reproduces the problem.  I mean
really tiny, one screenfull of Java code should be enough. Doing so
may help you understand what is happening in your code.  If it
doesn't, you can post your example here for people to help you with.

--Nat

---------------------------------------------------------------------
(Continue reading)

sparks | 3 Jul 05:25

Re: How to test a method that is wrapped in a private method?

Nat Pryce <nat.pryce@...> writes:
> 
> It could be that the dao is not being set by the setter.  Instead of a
> setter, use a constructor parameter and make the dao field final, so
> that the setter can never get hold of the wrong DAO and it cannot
> change after construction.
> 
> It could be that the data being passed to the DAO does not have an
> equals method defined for it, so two different objects that represent
> the same value compare as being different.  If the data being passed
> to the DAO is just a value, then it should be made immutable and given
> an equals method.  If it is mutable -- changes over time -- then it
> should not be given an equals method.  Instead you could write a
> Matcher to check that the data passed to the DAO has the expected
> state (see the website).

Thanks for your help Nat! 

The parameter I passed to the test method made the logic flow to another branch
which doesn't call the DAO method at all.

Really appreciate the fast and helpful responses!

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

    http://xircles.codehaus.org/manage_email


Gmane