I was wondering if JMockit is compatible with JUnit parameterized tests or JUnitParams, because I haven't found a way to make them work together so far, since you can only specify one JUnit runner, and both JMockit, JUnitParams and Parameterized require you to use their own runner.
Asked
Active
Viewed 1,083 times
1 Answers
2
JMockit does not require you to use an own runner. Using an own runner is just one of the possible ways to make sure JMockit got initialized properly before your tests run. You can also add JMockit as Java agent via commandline parameters, depend on classpath ordering (having JMockit in the classpath before JUnit) or call the JMockit initialization method manually before the actual tests start if you have such a place where you can call it, e.g. if you use an own JUnit runner.

Vampire
- 35,631
- 4
- 76
- 102
-
`mockit.internal.startup.Startup.initializeIfPossible();` – Vampire Jul 24 '18 at 13:44
-
Thanks! This is what I was using. However in my question https://stackoverflow.com/questions/51499088/jmockit-with-parameterized-junit-4 someone suggested the inner class way. Can't get it working... – LppEdd Jul 24 '18 at 13:45
-
I'm not sure what you mean by "inner class way", but using `@Before` is most probably too late. JMockit registers an agent to transform loaded classes on the fly, this way you can mock anyhting, even private methods, static stuff or constructors which you often cannot in mocking frameworks that use the proxy approach. But for that to work JMockit must be initialized in time. Could be that `@Before` is simply too late to initialize it. You could write an own JUnit runner that first initializes JMockit with mentioned method and then forwards to the original parametrized runner, or you can use – Vampire Jul 24 '18 at 14:06
-
the Java agent way I mentioned in the answer. Alternatively you could consider a different test framework, as parameterized tests with JUnit 4 are a PITA anyway. I'm not sure how good JUnit 5 is at this. I'm usually now use Spock for writing tests, it is amazing, especially when it comes to data-driven tests / parametrized tests and it is fully compatible to JUnit 4 (next version will be JUnit 5 compatible). Every tool supporting JUnit 4 supports Spock and you can even mix JUnit 4 and Spock tests in one project, e. g. for a slow migration or persistently. – Vampire Jul 24 '18 at 14:08
-
JUnit 5 is a certainly better, but I'm on Java 6 and I still haven't understood if it's compatible (seems not). I'll give Spock a look, thanks a lot (are you using Groovy or Java?) – LppEdd Jul 24 '18 at 14:25
-
Spock is Groovy, there is no choice. But you can test Java code or Groovy code, or actually any JVM based code. Just your test have to be written in Groovy. – Vampire Jul 24 '18 at 17:02