Mock static method in JUnit 5 using Mockito

You need to use mockito-inline version 3.4.0 or higher and remove mockito-core from your dependencies (mockito-inline depends on mockito-core so it will be retrieved automatically). That way, you will be using mockito static mocking correctly, but it wouldn’t solve the exception that you posted. To fix it, you need to remove all dependencies of powermock … Read more

Powermock and Spring cause ConversionException when injecting EntityManager in test

I had a similar issue and it was suggested to me to create a Resources.java class that Produced an Entity Manager. @Stateful public class Resources implements Serializable { @PersistenceContext (type = PersistenceContextType.TRANSACTION) private EntityManager entityManager; @Produces public EntityManager getEntityManager() { return entityManager; } } I’m still not exactly sure why this solution worked for me… … Read more

PowerMock & Java 11

After one year of no releases, things are really moving in PowerMock. PowerMock 2.0.0-RC1 was released. And with PowerMockito 2.0.0-RC1 + @PowerMockIgnore({“com.sun.org.apache.xerces.*”, “javax.xml.*”, “org.xml.*”, “org.w3c.*”}) The tests work under Java 11.

Is it possible to verify a mock method running in different thread in Mockito?

It is very likely that the Runnable hasn’t been executed yet by the asyncTaskExecutor when you verify the invocation, resulting in a verification error in your unit test. The best way to fix this is to join on the generated thread and wait for execution before verifying the invocations. If you cannot get the instance … Read more

Powermock – java.lang.IllegalStateException: Failed to transform class

Powermock 1.6.3 uses javassist 3.15.2-GA which does not support certain types. Using 3.18.2-GA javassist worked for me. You may want to override dependency in your project. <dependency> <groupId>org.javassist</groupId> <artifactId>javassist</artifactId> <version>3.18.2-GA</version> </dependency> You may face another problem for which the solution lies here Mockito + PowerMock LinkageError while mocking system class Hope this helps.

Mocking extension function in Kotlin

I think MockK can help you. It supports mocking extension functions too. You can use it to mock object-wide extensions: data class Obj(val value: Int) class Ext { fun Obj.extensionFunc() = value + 5 } with(mockk<Ext>()) { every { Obj(5).extensionFunc() } returns 11 assertEquals(11, Obj(5).extensionFunc()) verify { Obj(5).extensionFunc() } } If you extension is a … Read more