Closed x3rAx closed 6 years ago
Hi @x3rAx,
thanks for the nice and informative issue. I can reproduce your issue using IntelliJ. I have no Android Studio. The interesting thing is, though, that it works correctly in Eclipse, executing the same code snippet ...
This is quite strange but I will take some more time to debug it further.
How urgent is it on your side?
Cheers, Andreas
Hi @aaschmid,
it's just for a small project I'm contributing to in my free time. So it's not particularly urgent for me. As long as I can run them successfully I'm fine :)
I was just wondering what was going on there when I saw the duplicate test results and at first I thought it was my fault. But after some investigation I was certain that it was a bug and I thought, someone should mention it somewhere ;)
Cheers, Björn
Same issue occurs executing the test case in Gradle but only for JUnit4. Wrote same tests for junit-jupiter
and junit-jupiter-params
modules as well which work correctly ...
Debugging it further it seems to be a compiler issue caused by generic type parameter on the interface which strangely generates two methods instead of expected one:
public void ...testToBeImplemented(java.lang.Integer)
public void ...testToBeImplemented(java.lang.Object)
Both methods are returned by getTestClass().getAnnotatedMethods(Test.class)
. As the Eclipse compiler doesn't generate two methods, this behavior does not occur using Eclipse.
The problem are known and the phenomenon is called "Bridge methods" caused due to type erasure, see also https://docs.oracle.com/javase/tutorial/java/generics/bridgeMethods.html.
This happens not in Eclipse, see https://stackoverflow.com/questions/44136834/interface-method%C2%B4s-annotations-are-inherited-in-java-7-but-not-in-java-8
Fixed it for new in junit-dataprovider junit4
module (even if JUnit4 should normally fix that itself).
Why does it work with JUnit5?
method.isSynthetic()
such that bridge. More about synthetic methods: https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.7.8
Hi,
I've just started using
junit-dataprovider
and I'm also relatively new to Java / Java unit testing so please forgive me if I'm doing something completely wrong here.Let me first provide some context:
I've created an interface that certain types of my tests should implement. This is because I have an abstract class and some child classes that should all share the functionality/interface of the abstract class. Therefore I have to write some tests multiple times for each child class. That's why an interface for my test classes comes in handy because I'm forced to implement all that test and can't forget some of them.
junit: v4.12 junit-dataprovider: v1.13.1 IDE:
The issue:
I have noticed, that tests that are defined in the interface (as described above) and that are using the
@UseDataProvider
annotation, are run twiceExample:
The output would then be something like:
As can be seen here, the
testSomething
which overrides the interface method, is run only once. Also theanotherTestWithData
is only run once per provided value.But the
testSomethingWithData
runs two times for each value.