Spring JUnit: How to Mock autowired component in autowired component

前端 未结 6 1522
花落未央
花落未央 2020-12-02 12:20

I\'ve got a Spring component I\'d like to test and this component has an autowired attribute which I need to change for the purpose of unit testing. The problem is, that the

6条回答
  •  悲哀的现实
    2020-12-02 12:32

    I created blog post on the topic. It contains also link to Github repository with working example.

    The trick is using test configuration, where you override original spring bean with fake one. You can use @Primary and @Profile annotations for this trick.

提交回复
热议问题