Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Naming unit test methods of overloads in Java

What is the most accepted way to name a unit test method when the target has overloads. Consider these methods:

doSomething();
doSomething(String);

How would you name the corresponding test methods? Would this be the most accepted way?

testDoSomething();
testDoSomethingString();
like image 623
hpique Avatar asked Jul 08 '11 16:07

hpique


People also ask

How do you name a unit test method?

A unit test method name is the first thing, anyone, trying to understand your code will read. It is important to write descriptive method names that help readers quickly identify the purpose of a test case. These method names communicate what the code does. A name should be concise, unambiguous, and consistent.

How do you call a method overloaded?

println("Integer "+i); } void method(int i){ System. out. println("in "+i); } public static void main(String a[]){ OverLoad m= new OverLoad(); m. method(2); //it calls method(int i) why? } }

How can you tell overloaded methods apart?

Overloaded methods are differentiated by the number and the type of the arguments passed into the method. In the code sample, draw(String s) and draw(int i) are distinct and unique methods because they require different argument types.


1 Answers

Do whatever makes things more readable for you and your co-workers, if any. I think it depends on what your other tests for that class are, but based on those two methods, here is what I would do:

Test methods that test doSomething():

  • doSomething_void_success (this would be some test that tests a successful path)
  • doSomething_void_fail (this would be some test that tests an incorrect path)
  • doSomething_void_someOtherTest

Test methods that test doSomething(String):

  • doSomething_String_success
  • doSomething_String_fail
  • doSomething_String_someOtherTest

I don't use the test prefix anymore, as JUnit 4 doesn't require it. I just use the @Test annotation

like image 166
dgrant Avatar answered Sep 25 '22 03:09

dgrant