|
View:
New views
5 Messages
—
Rating Filter:
Alert me
|
|
|
how to test two methods with the same name, but different paremetersHi,
Say I need to test the following two methods: public UserObj getUser(String username) {...} public UserObj getUser(int userID) {...} The specs say that to test a method, I need to prepend the method name with "test". So to test a method someMethod(...) it would be public void testSomeMethod() {...}. What about for the above case? I can't have two testGetUser() methods in junit ... Is there a way to differentiate? Cheers! |
|
|
Re: how to test two methods with the same name, but different paremetersYou can actually use any method name you like: the "test<methodName>" format
is just a convention, but a very useful one since it allows all your tests be picked up automatucically and included in your test suite. So what you can do is append some parameter or other state information to the end of the test method name: testGetUserUsername and testGetUserUserId On Thu, Jun 12, 2008 at 11:41 AM, Nicole <nall4@...> wrote: > Hi, > > Say I need to test the following two methods: > > public UserObj getUser(String username) {...} > public UserObj getUser(int userID) {...} > > The specs say that to test a method, I need to prepend the method name > with "test". > So to test a method someMethod(...) it would be public void > testSomeMethod() {...}. > What about for the above case? > I can't have two testGetUser() methods in junit ... Is there a way to > differentiate? > > Cheers! > > > -- -- Brendan Hills brendan.hills@... [Non-text portions of this message have been removed] |
|
|
Re: how to test two methods with the same name, but different paremeters2008/6/12 Nicole <nall4@...>:
> The specs say that to test a method, I need to prepend the method name > with "test". > So to test a method someMethod(...) it would be public void > testSomeMethod() {...}. > What about for the above case? > I can't have two testGetUser() methods in junit ... Is there a way to > differentiate? A test method in your test-case needs to start with the word "test" but does not need to be named after a method in your test class. You can call your test methods anything you want, as long as the name starts with "test". In fact, it's not a great idea to name tests after methods in the class being tested. If you do so, the name of the test gives no useful information beyond what is already in the class under test. In the example you give, the reader already knows that your class contains a method called getUser(String) and getUser(int). A test called "testGetUser" is merely repeating that information and not describing what is being tested. If you organise and name tests by the methods in the class under test, then each test will need to test many different things: the happy path, failure cases, different boundary conditions, etc. This will make the test large, and so hard to read and hard to diagnose when it fails. Also you will not easily be able to test how different methods relate to one another. So, it's better to name a test after the aspect of the class' behaviour that the test is exercising. For example: testFindsExistingUserByName, testGetsExistingUserById, testReturnsNullIfUserNameNotFound, testThrowsExceptionIfUserIdDoesNotExist, testStoresNewUsers, etc. --Nat |
|
|
Re: how to test two methods with the same name, but different paremetersOn 2008-06-11, at 23:19 , Brendan Hills wrote: > You can actually use any method name you like: the > "test<methodName>" format > is just a convention, but a very useful one since it allows all your > tests > be picked up automatucically and included in your test suite. > > So what you can do is append some parameter or other state > information to > the end of the test method name: > > testGetUserUsername > and > testGetUserUserId > To Nicole, there is absolutely no rule that you must name a test "testX()" if it tests method "X()". I have used that naming convention in the past in the absence of a better name for my tests, but only as a convention, and never as a constraint. Once you see that naming your tests according to this rule makes certain things difficult, I invite you to explore new naming conventions. I think you'll discover new ideas about designing tests and naming methods. Take care. ---- 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: how to test two methods with the same name, but different paremetersHi @all
sorry to come late the discussion. usally I use a similar naming convention that consists of the three test parts - method name or action to test (e.g. testGetUsername... testLoginUser...) - parameters or state before test (e. g. testGetUsernameByName..., testLoginUserOnDisconnectedSystem...) - reaction or state after test (e.g. testGetUsernameByUnknownUserIdThrowsException testLoginUserOnConnectedSystemCreatesCredential) Martin > -----Original Message----- > From: junit@... [mailto:junit@...] On > Behalf Of J. B. Rainsberger > Sent: Samstag, 14. Juni 2008 05:20 > To: junit@... > Subject: Re: [junit] how to test two methods with the same > name, but different paremeters > > > On 2008-06-11, at 23:19 , Brendan Hills wrote: > > > You can actually use any method name you like: the > "test<methodName>" > > format is just a convention, but a very useful one since it > allows all > > your tests be picked up automatucically and included in your test > > suite. > > > > So what you can do is append some parameter or other state > information > > to the end of the test method name: > > > > testGetUserUsername > > and > > testGetUserUserId > > > I would prefer testGetUserByUsername() and testGetUserByUserId(). > > To Nicole, there is absolutely no rule that you must name a > test "testX()" if it tests method "X()". I have used that > naming convention in the past in the absence of a better name > for my tests, but only as a convention, and never as a > constraint. Once you see that naming your tests according to > this rule makes certain things difficult, I invite you to > explore new naming conventions. I think you'll discover new > ideas about designing tests and naming methods. > > Take care. > ---- > J. B. (Joe) Rainsberger :: http://www.jbrains.ca > <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 > > > > > |
| Free Forum Powered by Nabble | Forum Help |