Naming unit test overload methods in Java

What is the most acceptable way to name the unit test method when an object has overloads. Consider the following methods:

doSomething(); doSomething(String); 

What would you call the appropriate testing methods? Would this be the most acceptable way?

 testDoSomething(); testDoSomethingString(); 
+6
source share
4 answers

Do everything that makes things more readable for you and your employees, if any. I think it depends on what your other tests are for this class, but based on these two methods, here is what I would do:

Testing methods that test doSomething ():

  • doSomething_void_success (it will be some kind of test that checks the successful path)
  • doSomething_void_fail (it will be some kind of test that checks the successful path)
  • doSomething_void_someOtherTest

Testing methods that test doSomething (String):

  • doSomething_String_success
  • doSomething_String_fail
  • doSomething_String_someOtherTest

I no longer use the test prefix because JUnit 4 does not require it. I just use the @Test annotation

+5
source

There is no single “most acceptable way” - choose what you (r team) feel are most readable and clean.

I personally no longer use the test prefix, as this is not necessary with JUnit 4, and this impairs readability and searchability. I try to name my testing methods after they test the scripts. Which in your simplified case may be

 doSomethingSuccessfully(); ... failsToDoSomethingWithAString(); ... doSomethingWithAStringAndFail(); 
+4
source

I think this applies to your conventions.

I prefer to use underscores, as this allows a cleaner representation of the test methods.

I also use the test prefix, although others have indicated that it is not required. I usually do this to separate actual tests from some helper methods that may be in the test class.

So in your case, I would do

 test_doSomething test_doSomething_String 
0
source

Use your preferred behaviors to name your testing methods, for example:

 /** * * @return * @should say hello, and nothing more that that */ String sayHello(); } 

Would create a test method like this:

 @Test public void sayHello_shouldSayHelloAndNothingMoreThatThat() throws Exception { //TODO auto-generated Assert.fail("Not yet implemented"); } 

I developed a plugin for IntelliJ IDEA (an Eclipse version exists) to create these testing methods for you, you can find it here:

http://plugins.intellij.net/plugin/?idea&id=5847

0
source

Source: https://habr.com/ru/post/892342/


All Articles