[英]How to mock a method that returns a mock object which is cast
How to mock a method that returns a mock object which is cast. 如何模拟返回返回的模拟对象的方法。
I have a method which returns a mock object which is cast from an Object
into SomeClass
then calls a method on SomeClass
. 我有一个方法,该方法返回模拟对象,该Object
从Object
转换为SomeClass
然后在SomeClass
上调用方法。
The problem is the method getBean("SomeClass")
returns an Object
and not SomeClass
so mocking 问题是方法getBean("SomeClass")
返回一个Object
而不是SomeClass
所以嘲笑
when(mockApplicationContext.getBean("SomeClass")).thenReturn(mockSomeClass);
dosent work as the return type is wrong and mocking 由于返回类型错误且嘲笑,因此无法正常工作
when(mockApplicationContext.getBean("SomeClass")).thenReturn(mockObject);
dosent work as the method called on SomeClass
dosent exist and I cant mock SomeClass
. dosent工作作为呼吁方法SomeClass
dosent存在,我不能假装SomeClass
。
How can this be mocked? 怎么可以嘲笑呢?
Method to test. 测试方法。
public void anyMethod() {
// code omitted
( (SomeClass) getApplicationContext().getBean("SomeClass") ).someMethod(anArgument);
// ...
}
Test Method. 测试方法。
@Test
public void testAnyMethod() {
// ...
SomeClass mockSomeClass = mock(SomeClass.class);
when(mockSomeClass.someMethod(anArgument)).thenReturn(someResult);
ApplicationContext mockApplicationContext = mock(ApplicationContext.class);
when(mockApplicationContext.getBean("SomeClass")).thenReturn(mockSomeClass);
PowerMockito.doReturn(mockApplicationContext).when(GetContext.class, "getApplicationContext");
// ...
}
If you change your method implementation slightly to use the type-safe methods: 如果您稍微改变方法的实现以使用类型安全的方法:
public void anyMethod() {
// code omitted
getApplicationContext().getBean("SomeClass", SomeClass.class).someMethod(anArgument);
// ...
}
then your test class changes only a little to: 那么您的测试班级只更改了一点:
@Test
public void testAnyMethod() {
// ...
SomeClass mockSomeClass = mock(SomeClass.class);
when(mockSomeClass).someMethod(anArgument).thenReturn(someResult);
// The problem is that .getBean("SomeClass") returns Object.class which is then cast
// to SomeClass in anyMethod. If I return an Object then someMethod(anArgument) dosent exist
ApplicationContext mockApplicationContext = mock(ApplicationContext.class);
when(mockApplicationContext.getBean("SomeClass", SomeClass.class)).thenReturn(mockSomeClass);
// ...
}
Here is a simple example of doing what you want - you need to specify the behaviour of the mocked class during your set up: 这是做您想做的一个简单示例-您需要在设置过程中指定模拟类的行为:
ApplicationContext ctx = mock(ApplicationContext.class);
List<String> mockList = mock(List.class);
when(ctx.getBean("XYZ")).thenReturn(mockList);
when(mockList.get(0)).thenReturn("ABC"); // Setting up the behaviour!
// ctx.getBean("XYZ").get(0); // can't call List methods here
// Cast and call in one
System.out.println(((List<String>) ctx.getBean("XYZ")).get(0)); // Prints "ABC"
// .. or cast into a new reference
List<String> list = (List<String>) ctx.getBean("XYZ");
System.out.println(list.get(0)); // Also prints "ABC"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.