patricK
patricK

Reputation: 1105

JUnit test failing because of the method name

Why the name of a test method may influence other tests?

I have a suite with 2 classes of tests, and when I change a method name of class1, my test in class2 is ok (green).

I noticed that both classes have a method with the same name, but the test that is failing is neither of these. However if I rename any of them, all tests are ok.

Is it okay to have 2 methods with the same name in different classes, but in the same suite? And the fact that another test fails randomly is just a coincidence?

ps: the order of tests runned is changed after I rename that method. ps2: sorry for my bad English.

That picture can explain better my question: enter image description here

Upvotes: 8

Views: 2705

Answers (1)

Chriss
Chriss

Reputation: 5629

There is no bug in JUnit! Our team experienced similar results, which are caused by inproper resource management. You can try to rename your failing test so they are executed first. They should turn green now, that's mostly a sign that a resource is accidently shared between tests. In that case you can try to free the resource in the tear down (@After). Here is a little checklist to find the cause:

  • Are there Thread's that survive a test?
  • Are all Executors shutdown and terminated?
  • Are files or streams still open after a test?
  • Are all fields in the Test-class cleared/reinitialised after a test?
  • Avoid using static references or singletons
  • Don't free resource in your test method, only in the tear down method. Otherwise an exception could make this piece of code unreachable.

Upvotes: 10

Related Questions