如何在服务中模拟@autowired @Qualifier属性?

时间:2019-04-09 11:23:40

标签: spring junit mockito

我的服务具有这样的属性:

Columns("E:E").Select
    Selection.Replace What:=",", Replacement:=".", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=True
        Selection.Replace What:="m", Replacement:="", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=True
Range("E:E").NumberFormat = "#0.0""m"""

此属性来自上下文xml文件。

在单元测试中我什么也没变,可能是在单元测试中的正确行为。但是我可以嘲笑这个属性成为值吗?

例如:

@Autowired
@Qualifier("test")
private String test;

谢谢 问候

1 个答案:

答案 0 :(得分:1)

根据您的评论,可以将该字段设为公开。您可以稍后进行设置:

myMock = Mock(MyClass)
myMock.test = "foobar"

此外,您可以添加设置器,而将字段保留为私有。或者,您也可以从已接受的答案here中尝试@InjectMocks或Spring的ReflectionTestUtils