Jump to content

Mocking an object within a method and private field

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
7 replies to this topic

#1
sourlemon

sourlemon

    Programmer

  • Members
  • PipPipPip
  • 99 posts
Hi,
I hope everyone is having weekend. I, on the other hand, is struggling to figure this out. I'm not even sure what to search on.

I have the following sample class:

public class Temp{

     private MyOven oven;

     public Temp(){
          oven = new MyOven();   
    }

     public boolean foo(Apple apple) throws OvenException, PreparerException{
             Preparer preparer = new Preparer();
             PreparedPie preparedPie = preparer.prepare(apple);
             CookedPie pie = oven.cook(preparedPie);
             return pie.isGood();
    }
}
I'm currently using EasyMock to create mock objects for MyOven and Preparer. But I don't know how to tell the method to call my mock Preparer. Is there a simple way to do that?

Thanks.

Edited by sourlemon, 08 August 2010 - 06:08 PM.



#2
DustinStriplin

DustinStriplin

    Newbie

  • Members
  • Pip
  • 4 posts
im not sure if this is what you are asking, but you need to instantiate the oven object.

private MyOven oven = new MyOven();

#3
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
I got no idea what your question is about :(

#4
josep

josep

    Learning Programmer

  • Members
  • PipPipPip
  • 56 posts
Please make your question more clear so that we can be able to help you!

#5
sourlemon

sourlemon

    Programmer

  • Members
  • PipPipPip
  • 99 posts
...my question is, how do I test the Temp class? More specially, how can I control what MyOven and Preparer return?


#6
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
If you want to see what it returns you can simply let it output to the console. For testing this is good enough.
public Temp(){

          oven = new MyOven();   

          System.out.println("result of foo: " + oven.foo( new Apple() ));

    }


Or you can debug

#7
Sinipull

Sinipull

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 386 posts
I think he wants to use JUnit for testing and needs to 'mock' some objects inside the temp class. Printing out isn't quite the same, as JUnit testing is done before every run and separately from the real program. (the REAL testing)
I've seen one of my java teachers using the Easymock, but i can't seem to remember exactly how to use it.

You can write JUnit4 tests, by writing @Test annotation in front of a method.

Easymock documentation: http://easymock.org/...umentation.html

#8
sourlemon

sourlemon

    Programmer

  • Members
  • PipPipPip
  • 99 posts
thank you for trying to help :) Sinipull, that's exactly what I'm trying to do. I'm wring a test case using JUnit. I just realize I have a JUnit test book. I'm going to do some more research.

ps sinipull, there is more than one gender in this forum ;)