How to mock spring introduced classes using JMockit

My code is:

class A extends X { @Autowired B b; @Override method() { //do something b.callMethodInB; //do something } } class B extends X { @Autowired C c; @Override method() { //do something c.callMethodInC; //do something } } 

I need to test method() in A So how to taunt B I am using Junit4 and Jmockit.

+6
source share
2 answers

Try something like this:

 import org.junit.*; import mockit.*; public class ATest { @Tested A a; @Injectable B b; @Test public void testMethod() { a.method(); new Verifications() {{ b.callMethodInB(); }}; } } 

JMockit automatically creates A with an injected instance of B (from fake field B ), setting it to field A in the test class. This is independent of the DI framework used (Spring).

+8
source

Since Mocking Frameworks usually depends on DI (Dependency Injection), which basically means that you need to pass the tricked object to the method signature, I'm not sure if this is possible.

But look here , this can give you directions on how to do this.

0
source

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


All Articles