Spring beans redefinition in unit test environment

前端 未结 13 1914
醉话见心
醉话见心 2020-12-07 11:13

We are using Spring for my application purposes, and Spring Testing framework for unit tests. We have a small problem though: the application code loads a Spring application

13条回答
  •  攒了一身酷
    2020-12-07 11:48

    There are some very complicated and powerful solutions listed here.

    But there is a FAR, FAR simpler way to accomplish what Stas has asked, which doesn't involve modifying anything other than one line of code in the test method. It works for unit tests and Spring integration tests alike, for autowired dependencies, private and protected fields.

    Here it is:

    junitx.util.PrivateAccessor.setField(testSubject, "fieldName", mockObject);
    

提交回复
热议问题