???JMockit??дjava???????
???????????? ???????[ 2014/1/6 14:28:42 ] ????????Mock ??????? java
?????????з?????
???????ClassMocked???е?getTripleString(int)????????????????е?multiply3(int)????????????????????·????Mock??
@Test
public void testMockPrivateMethod() throws Exception {
final ClassMocked obj = new ClassMocked();
new NonStrictExpectations(obj) {
{
this.invoke(obj?? "multiply3"?? 1);//?????з??????????????????this.invoke(null?? "multiply3")
result = 4;
}
};
String actual = obj.getTripleString(1);
assertEquals("4"?? actual);
new Verifications() {
{
this.invoke(obj?? "multiply3"?? 1);
times = 1;
}
};
}
????????Mock???????????????
???????????EasyMock??PowerMock??Mock?????????JDK/CGLIB????????????????????????л????????????????java??????????У?????????е????????????????????е???????????Mock?????????????????????????????????????Щ?????????????????????????????????Mock????????????в?????JMockit????????Mocked??????????????????????????£?
?????????????
public class ClassMocked {
private String name = "name_init";
public String getName() {
return name;
}
private static String className="Class3Mocked_init";
public static String getClassName(){
return className;
}
}
???????JMockit????????????
@Test
public void testMockPrivateProperty() throws IOException {
final ClassMocked obj = new ClassMocked();
new NonStrictExpectations(obj) {
{
this.setField(obj?? "name"?? "name has bean change!");
}
};
assertEquals("name has bean change!"?? obj.getName());
}
???????JMockit???t??????????
@Test
public void testMockPrivateStaticProperty() throws IOException {
new NonStrictExpectations(Class3Mocked.class) {
{
this.setField(ClassMocked.class?? "className"?? "className has bean change!");
}
};
assertEquals("className has bean change!"?? ClassMocked.getClassName());
}
????(2).????????Mock?????
????JMockit???????????Mock?????????EasyMock??PowerMock?????????????????????????????????????????JMockit?????????Mock????????д???????????????????????????????????в????????????????????JMockit????????Mock??
?????????????????£?
public class StateMocked {
public static int getDouble(int i){
return i*2;
}
public int getTriple(int i){
return i*3;
}
}
??????д????????????
@Test
public void testMockNormalMethodContent() throws IOException {
final StateMocked obj = new StateMocked();
new NonStrictExpectations(obj) {
{
new MockUp<StateMocked>() {//???MockUp?????????????????
@Mock
public int getTriple(int i) {
return i * 30;
}
};
}
};
assertEquals(30?? obj.getTriple(1));
assertEquals(60?? obj.getTriple(2));
Mockit.tearDownMocks();
}
????????????????????
?????????д?????????????????????????????????????????????????????????????????????@Mock????????????£?
public class StaticMocked {
@Mock
public static int getDouble(int i){
return i*20;
}
}
??????????????£?
@Test
public void testDynamicMockStaticMethodContent() throws IOException {
Mockit.setUpMock(StateMocked.class?? StaticMocked.class);
assertEquals(20?? StateMocked.getDouble(1));
assertEquals(40?? StateMocked.getDouble(2));
Mockit.tearDownMocks();
}
????JMockit??PowerMock???????????????
????????PowerMock??????????????????@RunWith(PowerMockRunner.class)?????????????PowerMock????е?????????JMockit????????@RunWith????????????????????л?????PowerMock??JMockit???JMockit??????????????????????鯔?????????????????л???PowerMock??JMockit??
????????JMockit???????????JVM classpath????Junit???λ???????????Maven???????????JMockit????Junit??棬???????????JMockit?????????
???????JMockit??????????????
????????JMockit???JavaSE5?е?java.lang.instrument??????????????????????????????????????????????????????????????JMockit coverage???У????????Eclemma?????maven+sonar????????????????????????????·?????
????(1).Eclemma????????
?????????????Eclemma????????????????????????Eclemma????????????????н??????????????-javaagent??????????????£?
??????Eclipse????????Coverage as ->Coverage Configurations??????Junit??JVM???????£?
????-javaagent:"${settings.localRepository}"/com/googlecode/jmockit/jmockit-coverage/0.999.24/jmockit-coverage-0.999.24.jar
????????${settings.localRepository}??maven??????????磺
????-javaagent:D:userdataadministrator.m2
epositorycomgooglecodejmockitjmockitcoverage