java - Check if a method was called inside another method -


is there way in java check if method called inside method? testing class , method having trouble plays sound , there virtually no way of getting audio file played(private attribute inside inner class) without changing code. way method plays sounds calls method plays single sound (playsadmusic, playhappymusic, etc). methods in interface have create mock object for. i'm little stuck on how go testing this. thoughts? other ideas on how possibly test other check if method call welcome.

i using jmock 2.6.0 , junit 4

the audio inteface

public interface stocktickeraudiointerface {      public abstract void playhappymusic();      public abstract void playsadmusic();      public abstract void playerrormusic(); } 

anther interface have create mock for

public interface stockquotegeneratorinterface {     public abstract stockquoteinterface getcurrentquote() throws exception;      public abstract string getsymbol();      public abstract void setsymbol(string symbol);      public abstract stockquotegeneratorinterface createnewinstance(string symbol);  } 

the class being tested

public class stockquoteanalyzer {     private stocktickeraudiointerface audioplayer = null;     private string symbol;     private stockquotegeneratorinterface stockquotesource = null;      private stockquoteinterface lastquote = null;     private stockquoteinterface currentquote = null;       public stockquoteanalyzer(string symbol,         stockquotegeneratorinterface stockquotesource,         stocktickeraudiointerface audioplayer)         throws invalidstocksymbolexception, nullpointerexception,         stocktickerconnectionerror {         super();       // check validity of symbol.         if (stocktickerlisting.getsingleton().isvalidtickersymbol(symbol) == true){             this.symbol = symbol;         } else {         throw new invalidstocksymbolexception("symbol " + symbol                 + "not found.");         }         if (stockquotesource == null) {              throw new nullpointerexception(                 "the source stock quotes can not null");         }         this.stockquotesource = stockquotesource;         this.audioplayer = audioplayer;     }     public double getchangesincelast() {         double retval = 0.0;         if (this.lastquote != null) {             double delta = this.currentquote.getlasttrade() - this.lastquote.getlasttrade();             retval = 100 * (delta / this.lastquote.getlasttrade());            }            return retval;     }      public double getchangesinceyesterday() {         double delta = (this.currentquote.getlasttrade() - this.currentquote             .getclose());         return 100 * (delta / this.currentquote.getclose());      }      public void playappropriateaudio() {         if ((this.getchangesinceyesterday() > 2)             || (this.getchangesincelast() > 0.5)) {             audioplayer.playhappymusic();     }          if ((this.getchangesinceyesterday() < -2)             || (this.getchangesincelast() < -0.5)) {             audioplayer.playsadmusic();         }     }  } 

if use mockito can use verify() check number of times method called. use this:

verify(mockedobject, times(1)).methodtovalidate(); 

you can check if methodtovalidate() called specific string, e.i verify(mockedobject, times(1)).methodtovalidate("a specific value"); or can use anystring() this: verify(mockedobject, times(1)).methodtovalidate(anystring());.

unless method called specified paramterer, test fail

read more verify here.

update

since edited post states using jmock, quick googeling showed me possible achieve similar behaviour jmock , it's expect method. it's used below:

mockedobject.expects(once()).method("nameofmethod").with( eq("an optional paramter") ); 

more detailed explanation can found reading jmocks getting started page.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -