???JMockit??дjava???????
???????????? ???????[ 2014/1/6 14:28:42 ] ????????Mock ??????? java
????(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;
}
};
}
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11