简体   繁体   中英

Unit testing Spring throws BeanNotOfRequiredTypeException

Running a test class throws the following exception:

BeanNotOfRequiredTypeException: Bean named 'myServiceImpl' is expected to be of type 'MyServiceImpl' but was actually of type 'com.sun.proxy.$Proxy139'

This error gets thrown only with unit tests, the program itself works.

My Interface

public interface MyService {

    public String testMethod();

}

My Implementation

@Service
public class MyServiceImpl implements MyService{

    @Autowired
    private TransactionRepository transactionRepo;

    @Autowired
    private AccountRepository accountRepository;

    @Autowired
    private BankAccountStatementFactory baStatementFactory;

    public String myMethod() {
        return "Run My Method";
    }

}

My Unit Test

@RunWith(SpringRunner.class)
@SpringBootTest
public class DeleteMeTest{


    @Mock
    private TransactionRepository transactionRepo;

    @Mock
    private AccountRepository accountRepository;

    @Mock
    private BankAccountStatementFactory baStatementFactory;

    @InjectMocks
    @Resource
    MyServiceImpl myService;


    @org.junit.Before
    public void setUp() throws Exception {
        // Initialize mocks created above
        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void test() {
        myService.myMethod();
        System.out.println("My Unit Test");
    }

}

Running this test class throws the following exception:

BeanNotOfRequiredTypeException: Bean named 'myServiceImpl' is expected to be of type 'MyServiceImpl' but was actually of type 'com.sun.proxy.$Proxy139'

A solution here is to inject the Interface, not the implementation into the unit test but this will not allow me to inject mocks.

Thats because an implementation is required with the @InjectMocks annotation. When I try to inject mocks into the interface I get the following exception:

Cannot instantiate @InjectMocks field named 'myService'! Cause: the type 'MyService' is an interface.

Just to be clear, all this worked in the beginning and went bad once I repackaged my classes. That could be the cause but not 100% sure.

Any hints on what might cause this BeanNotOfRequiredTypeException ?

Thanks!

Since it's a unit test, you don't need Spring IMHO.

Simply initialize the tested class this way:

@InjectMocks
MyServiceImpl myService = new MyServiceImpl();

You can also remove these annotations:

@RunWith(SpringRunner.class)
@SpringBootTest

If you really need to use Spring (the reason for using Spring in the unit test is not clear from your post), you can try to unproxy the bean:

Have a separate declaration for the proxy and the bean:

@Resource
MyServiceImpl proxy;

@InjectMocks
MyServiceImpl myService;

Then initialize them in setUp() :

@org.junit.Before
public void setUp() throws Exception {
    // Initialize mocks created above
    myService = (MyServiceImpl)((TargetSource) proxy).getTarget();
    MockitoAnnotations.initMocks(this);

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM