问题描述
我有一种方法,我想创建一些单元测试(此方法会翘曲
applicationContext.getEnvironment()。getProperty(key,class)
用于领事集成)
长话短说:
from flask import Flask,request,redirect
from twilio.twiml.messaging_response import MessagingResponse
app = Flask(__name__)
@app.route("/sms",methods=['GET','POST'])
def incoming_sms():
"""Send a dynamic reply to an incoming text message"""
# Get the message the user sent our Twilio number
body = request.values.get('Body',None)
# Start our TwiML response
resp = MessagingResponse()
# Determine the right reply for this message
if body == 'hello':
resp.message("Hi!")
elif body == 'bye':
resp.message("Goodbye")
return str(resp)
if __name__ == "__main__":
app.run(debug=True)
在使用DynamicProperties类测试其他某些类时,如下所示:
public class DynamicProperties {
public <T> T getEnvironmentProperty(String key,Class<T> cls) {
return cls.cast(applicationContext.getEnvironment().getProperty(key,cls));
}
}
KEY_A KEY_B是公共静态最终字符串 我收到以下错误:
@Test
void testA() {
//before
when(dynamicProperties.getEnvironmentProperty(eq(KEY_A),Boolean.class)).thenReturn(true);
when(dynamicProperties.getEnvironmentProperty(eq(KEY_B),Long.class)).thenReturn(0l);
//when
archivedSensorsService.testMethod();
//than
verify(...)
}
尝试以下操作时:
This exception may occur if matchers are combined with raw values:
//incorrect:
someMethod(anyObject(),"raw String");
When using matchers,all arguments have to be provided by matchers.
For example:
//correct:
someMethod(anyObject(),eq("String by matcher"));
出现以下错误:
@Test
void testA() {
//before
when(dynamicProperties.getEnvironmentProperty(eq(KEY_A),anyObject())).thenReturn(true);
when(dynamicProperties.getEnvironmentProperty(eq(KEY_B),anyObject())).thenReturn(0l);
//when
archivedSensorsService.testMethod();
//than
verify(...)
}
有什么建议吗?
解决方法
该问题是由于混合使用模拟匹配器和不使用参数匹配器而引起的。如果对模拟的方法参数之一使用参数匹配器,则必须对所有参数使用匹配器。您可以阅读更多here。
我用解决方案创建了一个简单的project on GitHub-如果愿意,您可以检查一下,但这是代码段:
@Test
void withoutEq() {
DynamicProperties dynamicProperties = mock(DynamicProperties.class);
when(dynamicProperties.getEnvironmentProperty(KEY_A,Boolean.class))
.thenReturn(true);
when(dynamicProperties.getEnvironmentProperty(KEY_B,Long.class))
.thenReturn(1L);
assertAll(
() -> assertTrue(dynamicProperties.getEnvironmentProperty(KEY_A,Boolean.class)),() -> assertEquals(1,dynamicProperties.getEnvironmentProperty(KEY_B,Long.class))
);
}
@Test
void withEq() {
DynamicProperties dynamicProperties = mock(DynamicProperties.class);
when(dynamicProperties.getEnvironmentProperty(eq(KEY_A),eq(Boolean.class)))
.thenReturn(true);
when(dynamicProperties.getEnvironmentProperty(eq(KEY_B),eq(Long.class)))
.thenReturn(1L);
assertAll(
() -> assertTrue(dynamicProperties.getEnvironmentProperty(KEY_A,Long.class))
);
}
如您所见-方法之一将eq
匹配器用作方法的两个参数,而另一个则不使用。两项测试均通过。
以您的情况
when(dynamicProperties.getEnvironmentProperty(eq(KEY_A),anyObject())).thenReturn(true);
此模拟没有引起“匹配器与原始值组合”错误,因为eq
和anyObject
都是参数匹配器,但是
when(dynamicProperties.getEnvironmentProperty(eq(KEY_A),Boolean.class)).thenReturn(true);
具有一个匹配器(eq
)和一个简单的对象(Boolean.class
而没有eq
)。