Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Writing many tests to test one method

 
Swerrgy Smith
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Suppose I want to test the method doSomething() of the class Biz; I create a test class BizTest.
If I want to write many test methods to test doSomething(), how should I name these methods? I am thinking of some thing like: testDoSomething_1(), testDoSomething_2() ... but it will look like a automatically generated test (and not a test written by a person); furthermore, it will not tell the nature of each test (e.g. what the test method testDoSomething_1() is supposed to do).

What do you think about that? Or should I write only 1 method testDoSomething() and put a lot of assertxxxx inside?

Thanks for your advice.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should name your test methods based on what you're attempting to assert. For example, suppose your class Biz has a method called calculateTotal(). You might name your test methods as such:



Test methods should be as well defined as your methods being tested. testCalculateTotal_xxx is a bad idea.
 
Swerrgy Smith
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gregg Bolinger wrote:You should name your test methods based on what you're attempting to assert. For example, suppose your class Biz has a method called calculateTotal(). You might name your test methods as such:



Test methods should be as well defined as your methods being tested. testCalculateTotal_xxx is a bad idea.


Thanks for your excellent advice, Gregg.
 
Junilu Lacar
Bartender
Posts: 7587
53
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're using JUnit 4 you can make your test names even better by removing "test". I use a convention based on Neal Ford's suggestion for test names (Slide 44, http://www.slideshare.net/guestebde/10-ways-to-improve-your-code-neal-ford):


With a little effort to arrange and name your test methods with some consistency, the test report can read very nicely
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic