|
View:
New views
14 Messages
—
Rating Filter:
Alert me
|
|
|
Testing a listenerHello,
I am trying to test a parser class I wrote. In my test method I invoke the parser method I want to test with a string of data. However , the method returns nothing. Its result is sent to a listener. How can I test this method based on the results it sends to the listener. I hope this makes sense :s Thank You, jd |
|
|
Re: Testing a listenerOn Feb 2, 2008 11:37 PM, Jason Davis <mohadib@...> wrote:
> I am trying to test a parser class I wrote. In my test method I > invoke the parser method I want to test with > a string of data. However , the method returns nothing. Its result is > sent to a listener. How can I test this method > based on the results it sends to the listener. By substituting a test double (a spy) for the listener in your unit test, effectively giving you access to the interactions between the parser and the listener. Lasse -- Lasse Koskela * Author of "Test Driven" (Manning Publications, 2007) * * Reaktor Innovations * http://www.ri.fi/en * |
|
|
Re: Testing a listenerYou need to create a object that implements the Listener interface, and use
that in your test. These are generally known Mocks, Stubs,Fakes... e.g interface Listener { void hear(String sound); } class MockListener implements Listener { private String lastHeard; public void hear(String sound) { lastHeard = sound; } public String whatWasLastHeard() { return lastHeard; } } class ParserTest { @Test public void parsingCorrectlyShouldInformListener() { MockListener listener = new MockListener(); Parser parser = new Parse(listener); parser.parse("Hello World"); assertEquals(listener.whatWasLastHeared(), "Hello World"); } } Now obviously, if your Listener is not an interface, but instead its a class, then you will need to create a new class that derives from it, in order to do the same kind of interception of the event. HTH Andrew On 02/02/2008, Jason Davis <mohadib@...> wrote: > > Hello, > I am trying to test a parser class I wrote. In my test method I > invoke the parser method I want to test with > a string of data. However , the method returns nothing. Its result is > sent to a listener. How can I test this method > based on the results it sends to the listener. I hope this makes sense :s > > Thank You, > jd > > [Non-text portions of this message have been removed] |
|
|
<junit> with ANT V 1.6I am trying to use <junit> target in ANT 1.6 and have
class loader issues as described in this FAQ. http://ant.apache.org/faq.html#delegating-classloader I am not sure if <junit> is a optional task vor version 1.6 of ant and I shuld be using <taskdef> to define it. If yes..Not sure how to go about this. Could someone please help . Sri ____________________________________________________________________________________ Never miss a thing. Make Yahoo your home page. http://www.yahoo.com/r/hs |
|
|
Re: Testing a listenerOn 02/02/2008, Andrew McDonagh <andrewmcdonagh@...> wrote:
> You need to create a object that implements the Listener interface, and use > that in your test. These are generally known Mocks, Stubs,Fakes... When parsing structured data, the parameters and precise sequence of calls to the listener will be important. If you write your mock objects by hand it becomes increasingly complicated to test parameters of different calls, the order of calls, etc. This is what a mock object framework like jMock (http://www.jmock.org) helps you with. You specify the expected calls, their parameters, ordering constraints and so forth at the top of the test. You then exercise the code under test and the mock object framework fails the test if the code under test does not make the expected calls. --Nat |
|
|
Re: Testing a listenerOn 03/02/2008, Nat Pryce <nat.pryce@...> wrote:
> > > > > > > On 02/02/2008, Andrew McDonagh <andrewmcdonagh@...> wrote: > > You need to create a object that implements the Listener interface, and use > > that in your test. These are generally known Mocks, Stubs,Fakes... > > When parsing structured data, the parameters and precise sequence of > calls to the listener will be important. If you write your mock > objects by hand it becomes increasingly complicated to test parameters > of different calls, the order of calls, etc. > Not necessarily, we don't know what the parsing algorithm is, so can't make this call. > This is what a mock object framework like jMock (http://www.jmock.org) > helps you with. You specify the expected calls, their parameters, > ordering constraints and so forth at the top of the test. You then > exercise the code under test and the mock object framework fails the > test if the code under test does not make the expected calls. > > --Nat Don't get me wrong, Jmock and the other mocking frameworks are fine, nothing against them at all. However, I find introducing them as the immediate solution to this typical problem, to people new to xUnit testing, doesn't help as much. Once you explain how to do the job by hand in a simple way, then you can introduce the mocking frameworks as an alternative way. I say alternative as I've yet to see them be an easier way, even though I use them daily. ymmv Andrew |
|
|
Re: Testing a listenerThanks everyone for the replies :) I wound up searching the archives with the
word asynchronous and found mention of FutureResult. Pretty cool class :) That took care of my problem :) Thanks Again, jd On Feb 3, 2008 4:59 AM, Nat Pryce <nat.pryce@...> wrote: > On 02/02/2008, Andrew McDonagh <andrewmcdonagh@...> wrote: > > You need to create a object that implements the Listener interface, and use > > that in your test. These are generally known Mocks, Stubs,Fakes... > > When parsing structured data, the parameters and precise sequence of > calls to the listener will be important. If you write your mock > objects by hand it becomes increasingly complicated to test parameters > of different calls, the order of calls, etc. > > This is what a mock object framework like jMock (http://www.jmock.org) > helps you with. You specify the expected calls, their parameters, > ordering constraints and so forth at the top of the test. You then > exercise the code under test and the mock object framework fails the > test if the code under test does not make the expected calls. > > --Nat > > > > > Yahoo! Groups Links > > > > > |
|
|
Re: <junit> with ANT V 1.6If you exactly follow the instructions for Ant 1.6, it will work.
faq: <style> or <junit> ignores my <classpath> - Ant 1.6.x version http://ant.apache.org/faq.html#delegating-classloader-1.6 JUnit task is optional, but you don't have to taskdef it. Try if using JUnit 3.8 solves your problem. I think that full support for JUnit 4 came with Ant 1.7 Regards, Jan --- In junit@..., Srinivasan TK <tksri2000@...> wrote: > > I am trying to use <junit> target in ANT 1.6 and have > class loader issues as described in this FAQ. > http://ant.apache.org/faq.html#delegating-classloader > > I am not sure if <junit> is a optional task vor > version 1.6 of ant and I shuld be using <taskdef> to > define it. If yes..Not sure how to go about this. > Could someone please help . > > Sri > > > ______________ > Never miss a thing. Make Yahoo your home page. > http://www.yahoo.com/r/hs > |
|
|
Re: Re: <junit> with ANT V 1.6I ran diagnostics on the ant i am using.
I see junit : Missing dependency junit.framework.Test for junit.. I didnt quite follow the instructions on how to go about this when choosing not to taskdef junit task. I appreciate some more help [sngdev03]:/sep/java > ant -diagnostics ------- Ant diagnostics report ------- Apache Ant version 1.6.2 compiled on August 5 2004 ------------------------------------------- Implementation Version (JDK1.2+ only) ------------------------------------------- core tasks : null optional tasks : 1.6.2 ------------------------------------------- ANT_HOME/lib jar listing ------------------------------------------- ant.home: /usr/share/ant ant-optional.jar (324076 bytes) activation.jar (25776 bytes) bcel.jar (405813 bytes) ant.jar (645146 bytes) commons-logging.jar (13931 bytes) jakarta-regexp.jar (19405 bytes) java_cup.jar (68499 bytes) junit.jar (34562 bytes) mail.jar (230473 bytes) xalan.jar (1506168 bytes) xerces.jar (1082968 bytes) ------------------------------------------- Tasks availability ------------------------------------------- p4opened : Not Available image : Missing dependency com.sun.media.jai.codec.FileSeekableStream sshexec : Missing dependency com.jcraft.jsch.UserInfo scp : Missing dependency com.jcraft.jsch.UserInfo cvsversion : Not Available p4changes : Not Available p4versionstring : Not Available ddinit : Not Available jdepend : Missing dependency jdepend.xmlui.JDepend junit : Missing dependency junit.framework.Test --- j_cumps <j_cumps@...> wrote: > If you exactly follow the instructions for Ant 1.6, > it will work. > > faq: <style> or <junit> ignores my <classpath> - Ant > 1.6.x version > > http://ant.apache.org/faq.html#delegating-classloader-1.6 > > JUnit task is optional, but you don't have to > taskdef it. > Try if using JUnit 3.8 solves your problem. I think > that full support > for JUnit 4 came with Ant 1.7 > > Regards, Jan > > --- In junit@..., Srinivasan TK > <tksri2000@...> wrote: > > > > I am trying to use <junit> target in ANT 1.6 and > have > > class loader issues as described in this FAQ. > > > > > > > I am not sure if <junit> is a optional task vor > > version 1.6 of ant and I shuld be using <taskdef> > to > > define it. If yes..Not sure how to go about this. > > Could someone please help . > > > > Sri > > > > > > > > ______________ > > Never miss a thing. Make Yahoo your home page. > > http://www.yahoo.com/r/hs > > > > > ____________________________________________________________________________________ Looking for last minute shopping deals? Find them fast with Yahoo! Search. http://tools.search.yahoo.com/newsearch/category.php?category=shopping |
|
|
Re: <junit> with ANT V 1.6What version of junit.jar did you place in ant/lib? Try if replacing your junit.jar with JUnit version 3.8 solves the issue. Regards, Jan --- In junit@..., Srinivasan TK <tksri2000@...> wrote: > > I ran diagnostics on the ant i am using. > I see junit : Missing dependency junit.framework.Test > > for junit.. > I didnt quite follow the instructions on how to go > about this when choosing not to taskdef junit task. > I appreciate some more help > > [sngdev03]:/sep/java > ant -diagnostics > ------- Ant diagnostics report ------- > Apache Ant version 1.6.2 compiled on August 5 2004 > > ------------------------------------------- > Implementation Version (JDK1.2+ only) > ------------------------------------------- > core tasks : null > optional tasks : 1.6.2 > > ------------------------------------------- > ANT_HOME/lib jar listing > ------------------------------------------- > ant.home: /usr/share/ant > ant-optional.jar (324076 bytes) > activation.jar (25776 bytes) > bcel.jar (405813 bytes) > ant.jar (645146 bytes) > commons-logging.jar (13931 bytes) > jakarta-regexp.jar (19405 bytes) > java_cup.jar (68499 bytes) > junit.jar (34562 bytes) > mail.jar (230473 bytes) > xalan.jar (1506168 bytes) > xerces.jar (1082968 bytes) > > ------------------------------------------- > Tasks availability > ------------------------------------------- > p4opened : Not Available > image : Missing dependency > com.sun.media.jai.codec.FileSeekableStream > sshexec : Missing dependency com.jcraft.jsch.UserInfo > scp : Missing dependency com.jcraft.jsch.UserInfo > cvsversion : Not Available > p4changes : Not Available > p4versionstring : Not Available > ddinit : Not Available > jdepend : Missing dependency jdepend.xmlui.JDepend > junit : Missing dependency junit.framework.Test > > > --- j_cumps <j_cumps@...> wrote: > > > If you exactly follow the instructions for Ant 1.6, > > it will work. > > > > faq: <style> or <junit> ignores my <classpath> - Ant > > 1.6.x version > > > > > http://ant.apache.org/faq.html#delegating-classloader-1.6 > > > > JUnit task is optional, but you don't have to > > taskdef it. > > Try if using JUnit 3.8 solves your problem. I think > > that full support > > for JUnit 4 came with Ant 1.7 > > > > Regards, Jan > > > > --- In junit@..., Srinivasan TK > > <tksri2000@> wrote: > > > > > > I am trying to use <junit> target in ANT 1.6 and > > have > > > class loader issues as described in this FAQ. > > > > > > http://ant.apache.org/faq.html#delegating-classloader > > > > > > I am not sure if <junit> is a optional task vor > > > version 1.6 of ant and I shuld be using <taskdef> > > to > > > define it. If yes..Not sure how to go about this. > > > Could someone please help . > > > > > > Sri > > > > > > > > > > > > > > ______________ > > > Never miss a thing. Make Yahoo your home page. > > > http://www.yahoo.com/r/hs > > > > > > > > > > > > > ______________ > Looking for last minute shopping deals? > Find them fast with Yahoo! Search. http://tools.search.yahoo.com/newsearch/category.php?category=shopping > |
|
|
Re: Testing a listenerIt would make more sense to me if you post the code. I can understand
code more than people's talk or language, i guess i am not much of people person :) . Dont get me wrong, i can understand what you are saying, but i can debug your code in a better way if you give it to me... Regards Vyas, Anirudh --- In junit@..., "Jason Davis" <mohadib@...> wrote: > > Hello, > I am trying to test a parser class I wrote. In my test method I > invoke the parser method I want to test with > a string of data. However , the method returns nothing. Its result is > sent to a listener. How can I test this method > based on the results it sends to the listener. I hope this makes sense :s > > Thank You, > jd > |
|
|
Re: Re: <junit> with ANT V 1.6Replacing junit.jar with JUnit version 3.8 doesnt seem
to help. I am using ant from a standard installation directory on a box. I know we need to remove/add to $ANT_HOME/lib directory if we decide to taskdef junit. I do not want to go down this path since I will have to get this change done on the standard instalation path and there are more than one teams pointing to this. Not sure how I can approach the two issues: 1)junit task not recognized in the ant file 2) How to work the changes to lib dir if we decide to taskdef junit task Please advise How do I come up with a solution that is port --- j_cumps <j_cumps@...> wrote: > > What version of junit.jar did you place in ant/lib? > > Try if replacing your junit.jar with JUnit version > 3.8 solves the > issue. > > Regards, Jan > > --- In junit@..., Srinivasan TK > <tksri2000@...> wrote: > > > > I ran diagnostics on the ant i am using. > > I see junit : Missing dependency > junit.framework.Test > > > > for junit.. > > I didnt quite follow the instructions on how to go > > about this when choosing not to taskdef junit > task. > > I appreciate some more help > > > > [sngdev03]:/sep/java > ant -diagnostics > > ------- Ant diagnostics report ------- > > Apache Ant version 1.6.2 compiled on August 5 2004 > > > > ------------------------------------------- > > Implementation Version (JDK1.2+ only) > > ------------------------------------------- > > core tasks : null > > optional tasks : 1.6.2 > > > > ------------------------------------------- > > ANT_HOME/lib jar listing > > ------------------------------------------- > > ant.home: /usr/share/ant > > ant-optional.jar (324076 bytes) > > activation.jar (25776 bytes) > > bcel.jar (405813 bytes) > > ant.jar (645146 bytes) > > commons-logging.jar (13931 bytes) > > jakarta-regexp.jar (19405 bytes) > > java_cup.jar (68499 bytes) > > junit.jar (34562 bytes) > > mail.jar (230473 bytes) > > xalan.jar (1506168 bytes) > > xerces.jar (1082968 bytes) > > > > ------------------------------------------- > > Tasks availability > > ------------------------------------------- > > p4opened : Not Available > > image : Missing dependency > > com.sun.media.jai.codec.FileSeekableStream > > sshexec : Missing dependency > com.jcraft.jsch.UserInfo > > scp : Missing dependency com.jcraft.jsch.UserInfo > > cvsversion : Not Available > > p4changes : Not Available > > p4versionstring : Not Available > > ddinit : Not Available > > jdepend : Missing dependency jdepend.xmlui.JDepend > > junit : Missing dependency junit.framework.Test > > > > > > --- j_cumps <j_cumps@...> wrote: > > > > > If you exactly follow the instructions for Ant > 1.6, > > > it will work. > > > > > > faq: <style> or <junit> ignores my <classpath> - > Ant > > > 1.6.x version > > > > > > > > > > > > > > > JUnit task is optional, but you don't have to > > > taskdef it. > > > Try if using JUnit 3.8 solves your problem. I > think > > > that full support > > > for JUnit 4 came with Ant 1.7 > > > > > > Regards, Jan > > > > > > --- In junit@..., Srinivasan TK > > > <tksri2000@> wrote: > > > > > > > > I am trying to use <junit> target in ANT 1.6 > and > > > have > > > > class loader issues as described in this FAQ. > > > > > > > > > > > > > > > > > > I am not sure if <junit> is a optional task > vor > > > > version 1.6 of ant and I shuld be using > <taskdef> > > > to > > > > define it. If yes..Not sure how to go about > this. > > > > Could someone please help . > > > > > > > > Sri > > > > > > > > > > > > > > > > > > > > > ______________ > > > > Never miss a thing. Make Yahoo your home > page. > > > > http://www.yahoo.com/r/hs > > > > > > > > > > > > > > > > > > > > > > > ______________ > > Looking for last minute shopping deals? > > Find them fast with Yahoo! Search. > http://tools.search.yahoo.com/newsearch/category.php?category=shopping > > > > > ____________________________________________________________________________________ Looking for last minute shopping deals? Find them fast with Yahoo! Search. http://tools.search.yahoo.com/newsearch/category.php?category=shopping |
|
|
Re: Testing a listenerOn Feb 2, 2008, at 15:37 , Jason Davis wrote:
> I am trying to test a parser class I wrote. In my test method I > invoke the parser method I want to test with > a string of data. However , the method returns nothing. Its result is > sent to a listener. How can I test this method > based on the results it sends to the listener. I hope this makes > sense :s > You have already received some answers, but here is a summary of the technique I use. I like to check that the event source is generating the right events at the right times by attaching a mock Listener and intercepting method calls. In the case of an XML SAX-style parser, you might have a test like this pseudo-Java test: testBeginTag: mock = mock(ParserListener.class); parser = Parser.withListener(mock.proxy()); mock.expects(once()).method("handleEvent").with(eq(new BeginTagEvent("html"))); parser.parse("<html>"); These tests help me know whether Parser sends the right event at the right time. I might have one more test for multiple listeners: testMultipleListeners: mocks = [mock(ParserListener.class), mock(ParserListener.class), mock(ParserListener.class)] parser = Parser.withManyListeners(mocks.collect { |each| each.proxy() }) event = new Event(); mocks.each { |each| each.expects(once()).method("handleEvent").with(same(event))) } parser.notifyAll(event); This test helps me know the mechanics of notifying listeners works. Finally, there is no "testing the listener": the listener is just a Java class, so there's nothing special you need to do to test it. Just invoke its handleEvent() method with various event objects and check what it does. I usually design my listeners as simple delegates to more model-like classes, so that handleEvent() acts as a Mediator. I hope this helps. ---- J. B. (Joe) Rainsberger :: http://www.jbrains.ca Your guide to software craftsmanship JUnit Recipes: Practical Methods for Programmer Testing 2005 Gordon Pask Award for contributions to Agile Software Practice |
|
|
Re: Re: <junit> with ANT V 1.6Sri,
I know it's much later, but did you find a solution to your problem? Thanks, David Saff On Sat, Feb 9, 2008 at 12:43 AM, Srinivasan TK <tksri2000@...> wrote: > Replacing junit.jar with JUnit version 3.8 doesnt seem > to help. > I am using ant from a standard installation directory > on a box. > I know we need to remove/add to $ANT_HOME/lib > directory if we decide to taskdef junit. > I do not want to go down this path since I will have > to get this change done on the standard instalation > path and there are more than one teams pointing to > this. > Not sure how I can approach the two issues: > 1)junit task not recognized in the ant file > 2) How to work the changes to lib dir if we decide to > taskdef junit task > > Please advise > > How do I come up with a solution that is port > --- j_cumps <j_cumps@...> wrote: > >> >> What version of junit.jar did you place in ant/lib? >> >> Try if replacing your junit.jar with JUnit version >> 3.8 solves the >> issue. >> >> Regards, Jan >> >> --- In junit@..., Srinivasan TK >> <tksri2000@...> wrote: >> > >> > I ran diagnostics on the ant i am using. >> > I see junit : Missing dependency >> junit.framework.Test >> > >> > for junit.. >> > I didnt quite follow the instructions on how to go >> > about this when choosing not to taskdef junit >> task. >> > I appreciate some more help >> > >> > [sngdev03]:/sep/java > ant -diagnostics >> > ------- Ant diagnostics report ------- >> > Apache Ant version 1.6.2 compiled on August 5 2004 >> > >> > ------------------------------------------- >> > Implementation Version (JDK1.2+ only) >> > ------------------------------------------- >> > core tasks : null >> > optional tasks : 1.6.2 >> > >> > ------------------------------------------- >> > ANT_HOME/lib jar listing >> > ------------------------------------------- >> > ant.home: /usr/share/ant >> > ant-optional.jar (324076 bytes) >> > activation.jar (25776 bytes) >> > bcel.jar (405813 bytes) >> > ant.jar (645146 bytes) >> > commons-logging.jar (13931 bytes) >> > jakarta-regexp.jar (19405 bytes) >> > java_cup.jar (68499 bytes) >> > junit.jar (34562 bytes) >> > mail.jar (230473 bytes) >> > xalan.jar (1506168 bytes) >> > xerces.jar (1082968 bytes) >> > >> > ------------------------------------------- >> > Tasks availability >> > ------------------------------------------- >> > p4opened : Not Available >> > image : Missing dependency >> > com.sun.media.jai.codec.FileSeekableStream >> > sshexec : Missing dependency >> com.jcraft.jsch.UserInfo >> > scp : Missing dependency com.jcraft.jsch.UserInfo >> > cvsversion : Not Available >> > p4changes : Not Available >> > p4versionstring : Not Available >> > ddinit : Not Available >> > jdepend : Missing dependency jdepend.xmlui.JDepend >> > junit : Missing dependency junit.framework.Test >> > >> > >> > --- j_cumps <j_cumps@...> wrote: >> > >> > > If you exactly follow the instructions for Ant >> 1.6, >> > > it will work. >> > > >> > > faq: <style> or <junit> ignores my <classpath> - >> Ant >> > > 1.6.x version >> > > >> > > >> > >> > http://ant.apache.org/faq.html#delegating-classloader-1.6 >> > > >> > > JUnit task is optional, but you don't have to >> > > taskdef it. >> > > Try if using JUnit 3.8 solves your problem. I >> think >> > > that full support >> > > for JUnit 4 came with Ant 1.7 >> > > >> > > Regards, Jan >> > > >> > > --- In junit@..., Srinivasan TK >> > > <tksri2000@> wrote: >> > > > >> > > > I am trying to use <junit> target in ANT 1.6 >> and >> > > have >> > > > class loader issues as described in this FAQ. >> > > > >> > > >> > >> > http://ant.apache.org/faq.html#delegating-classloader >> > > > >> > > > I am not sure if <junit> is a optional task >> vor >> > > > version 1.6 of ant and I shuld be using >> <taskdef> >> > > to >> > > > define it. If yes..Not sure how to go about >> this. >> > > > Could someone please help . >> > > > >> > > > Sri >> > > > >> > > > >> > > > >> > > >> > >> > ______________________________________________________________________ >> > > ______________ >> > > > Never miss a thing. Make Yahoo your home >> page. >> > > > http://www.yahoo.com/r/hs >> > > > >> > > >> > > >> > > >> > >> > >> > >> > >> > ______________________________________________________________________ >> ______________ >> > Looking for last minute shopping deals? >> > Find them fast with Yahoo! Search. >> > http://tools.search.yahoo.com/newsearch/category.php?category=shopping >> > >> >> >> > > > > ____________________________________________________________________________________ > Looking for last minute shopping deals? > Find them fast with Yahoo! Search. http://tools.search.yahoo.com/newsearch/category.php?category=shopping > > > > Yahoo! Groups Links > > > > |
| Free Forum Powered by Nabble | Forum Help |