????(1).?????????Mock?????
?????????????EasyMock??PowerMock????????????????????
????1.??????????????
????2.??????á?
????3.???????????????á?
????????????????????????JMockit??????????
?????Mock???????????£?
public class MyObject {
public String hello(String name){
return "Hello " + name;
}
}
???????JMockit??д???????????£?
@Mocked  //??@Mocked??????????????????jmockit???mock
MyObject obj;
@Test
public void testHello() {
new NonStrictExpectations() {//????????????
{
obj.hello("Zhangsan");
returns("Hello Zhangsan");
//?????????result = "Hello Zhangsan";
}
};
assertEquals("Hello Zhangsan"?? obj.hello("Zhangsan"));//???ò??????
new Verifications() {//??????Mock?????????
{
obj.hello("Hello Zhangsan");
times = 1;
}
};
}
????JMockit???????????????????????????????Expectations??????в??????в???????????????????????
??????Expectations???????Expectations???NonStrictExpectations???壬??????EasyMock??PowerMock?е?Strict Mock???????Mock??
??????Expectations????????mock????????????????? Expectations???ж??????????ε??÷???????????????????????????????????Verifications?飻
????????NonStrictExpectations?????????????Щ??????????????????????????Verifications?顣
???????????????????????????????????t?????????д????????????????£?
@Test
public void testHello() {
final MyObject obj = new MyObject();
new NonStrictExpectations(obj) {//????????????
{
obj.hello("Zhangsan");
returns("Hello Zhangsan");
//?????????result = "Hello Zhangsan";
}
};
assertEquals("Hello Zhangsan"?? obj.hello("Zhangsan"));//???ò??????
new Verifications() {//??????Mock?????????
{
obj.hello("Hello Zhangsan");
times = 1;
}
};
}
??????????????
@Test
public void testMockStaticMethod() {
new NonStrictExpectations(ClassMocked.class) {
{
ClassMocked.getDouble(1);//???????ò??????ClassMocked.getDouble(anyDouble);
result = 3;
}
};
assertEquals(3?? ClassMocked.getDouble(1));
new Verifications() {
{
ClassMocked.getDouble(1);
times = 1;
}
};
}