我有这样的代码片段:
@RunWith(PowerMockRunner.class)
@PrepareForTest({Thread.class})
public class AllMeasuresDataTest {
@Before
public void setUp() throws Exception {
}
@Test
public void testGetMeasures() {
AllMeasuresData measure = new AllMeasuresData();
assertEquals(measure.getMeasures(), null);
HashMap<String, Measure> map = new HashMap<String, Measure>();
measure.setMeasures(map);
assertEquals(measure.getMeasures(), map);
measure.setMeasures(null);
assertEquals(measure.getMeasures(), null);
}
@Test
public void testAllMeasuresData() throws IOException {
ClassLoader loader = PowerMockito.mock(ClassLoader.class);
Thread threadMock = PowerMockito.mock(Thread.class);
Vector<URL> vec = new Vector<URL>();
Mockito.when(loader.getResources("measure")).thenReturn(vec.elements());
Mockito.when(threadMock.getContextClassLoader()).thenReturn(loader);
PowerMockito.mockStatic(Thread.class);
Mockito.when(Thread.currentThread()).thenReturn(threadMock);
...
}
}
Run Code Online (Sandbox Code Playgroud)
在运行此测试时,我得到了:
java.lang.LinkageError: loader constraint violation: loader (instance of org/powermock/core/classloader/MockClassLoader) previously initiated loading …Run Code Online (Sandbox Code Playgroud) 是否可以通过泛型传递接口的类型?
界面:
public interface AsyncCallback<T>
Run Code Online (Sandbox Code Playgroud)
在我的测试方法中:
Mockito.any(AsyncCallback.class)
Run Code Online (Sandbox Code Playgroud)
把<ResponseX>后面或.class没有工作.
我一直试图使用Mockito模拟一个使用vararg参数的方法:
interface A {
B b(int x, int y, C... c);
}
A a = mock(A.class);
B b = mock(B.class);
when(a.b(anyInt(), anyInt(), any(C[].class))).thenReturn(b);
assertEquals(b, a.b(1, 2));
Run Code Online (Sandbox Code Playgroud)
这不起作用,但是如果我这样做:
when(a.b(anyInt(), anyInt())).thenReturn(b);
assertEquals(b, a.b(1, 2));
Run Code Online (Sandbox Code Playgroud)
尽管我在删除方法时完全省略了varargs参数,但这仍然有效.
有线索吗?
有没有办法匹配以下示例例程的任何类参数?
class A {
public B method(Class<? extends A> a) {}
}
Run Code Online (Sandbox Code Playgroud)
无论传递哪个类,我怎么能总是返回?以下尝试仅适用于匹配的特定情况.new B()methodA
A a = new A();
B b = new B();
when(a.method(eq(A.class))).thenReturn(b);
Run Code Online (Sandbox Code Playgroud)
编辑:一个解决方案是
(Class<?>) any(Class.class)
Run Code Online (Sandbox Code Playgroud) 我正在尝试让我的一个模拟对象在调用特定方法时抛出一个已检查的Exception.我正在尝试以下方法.
@Test(expectedExceptions = SomeException.class)
public void throwCheckedException() {
List<String> list = mock(List.class);
when(list.get(0)).thenThrow(new SomeException());
String test = list.get(0);
}
public class SomeException extends Exception {
}
Run Code Online (Sandbox Code Playgroud)
但是,这会产生以下错误.
org.testng.TestException:
Expected exception com.testing.MockitoCheckedExceptions$SomeException but got org.mockito.exceptions.base.MockitoException:
Checked exception is invalid for this method!
Invalid: com.testing.MockitoCheckedExceptions$SomeException
Run Code Online (Sandbox Code Playgroud)
看看Mockito文档,他们只使用RuntimeException,是否不可能使用Mockito从模拟对象中抛出已检查的异常?
什么是使用Mockito间谍的用例?
在我看来,使用callRealMethod可以使用mock处理每个间谍用例.
我可以看到的一个区别是,如果你想让大多数方法调用都是真实的,它会节省一些代码行来使用模拟和间谍.这是它还是我错过了更大的图景?
我在运行测试时遇到异常.我正在使用Mockito进行嘲弄.Mockito图书馆提到的提示没有帮助.
org.mockito.exceptions.misusing.UnfinishedStubbingException:
Unfinished stubbing detected here:
-> at com.a.b.DomainTestFactory.myTest(DomainTestFactory.java:355)
E.g. thenReturn() may be missing.
Examples of correct stubbing:
when(mock.isOk()).thenReturn(true);
when(mock.isOk()).thenThrow(exception);
doThrow(exception).when(mock).someVoidMethod();
Hints:
1. missing thenReturn()
2. you are trying to stub a final method, you naughty developer!
at a.b.DomainTestFactory.myTest(DomainTestFactory.java:276)
..........
Run Code Online (Sandbox Code Playgroud)
来自DomainTestFactory的测试代码.当我运行以下测试时,我看到了异常
@Test
public myTest(){
MyMainModel mainModel = Mockito.mock(MyMainModel.class);
Mockito.when(mainModel.getList()).thenReturn(getSomeList()); --> Line 355
}
private List<SomeModel> getSomeList() {
SomeModel model = Mockito.mock(SomeModel.class);
Mockito.when(model.getName()).thenReturn("SomeName"); --> Line 276
Mockito.when(model.getAddress()).thenReturn("Address");
return Arrays.asList(model);
}
public class SomeModel extends SomeInputModel{
protected String address;
protected List<SomeClass> properties; …Run Code Online (Sandbox Code Playgroud) 我们一直在使用Mock for python.
现在,我们有一种情况,我们想要模拟一个函数
def foo(self, my_param):
#do something here, assign something to my_result
return my_result
Run Code Online (Sandbox Code Playgroud)
通常,模拟这个的方法是(假设foo是对象的一部分)
self.foo = MagicMock(return_value="mocked!")
Run Code Online (Sandbox Code Playgroud)
甚至,如果我打电话给foo()几次我可以使用
self.foo = MagicMock(side_effect=["mocked once", "mocked twice!"])
Run Code Online (Sandbox Code Playgroud)
现在,我面临的情况是,当输入参数具有特定值时,我想返回固定值.所以如果让我们说"my_param"等于"某事"那么我想要返回"my_cool_mock"
这似乎可以在mock上用于python
when(dummy).foo("something").thenReturn("my_cool_mock")
Run Code Online (Sandbox Code Playgroud)
我一直在寻找如何与Mock达成同样的目标并没有成功?
有任何想法吗?
我正在尝试验证在DAO中调用(void)方法 - 我正在使用一个提交点,该提交点将结果列表发送到该点,重置列表并继续.假设我在列表中有4个东西,并且我的提交点为1,我希望"send"方法被调用4次.我可以通过编写来验证该方法是否被调用一次
Mockito.verify(mock).send()
它通过..但我想验证它被调用的次数.我想是的
Mockito.verify(mock.send(), times(4))
就足够了,但它说参数不正确验证.
顺便说一句,如果我 Mockito.verify(mock).send()改为 Mockito.verify(mock.send())或者 Mockito.verify((mock).send())我得到同样的错误.对此的想法?
我是开发新手,特别是单元测试.我想我的要求很简单,但我很想知道别人的想法.
假设我有两个类似的 -
public class First {
Second second ;
public First(){
second = new Second();
}
public String doSecond(){
return second.doSecond();
}
}
class Second {
public String doSecond(){
return "Do Something";
}
}
Run Code Online (Sandbox Code Playgroud)
假设我正在为测试First.doSecond()方法编写单元测试.但是,假设,我想像Mock这样的Second.doSecond()类.我正在使用Mockito来做这件事.
public void testFirst(){
Second sec = mock(Second.class);
when(sec.doSecond()).thenReturn("Stubbed Second");
First first = new First();
assertEquals("Stubbed Second", first.doSecond());
}
Run Code Online (Sandbox Code Playgroud)
我看到模拟没有生效,断言失败了.有没有办法模拟我想测试的类的成员变量.?