Jmockit 入门

在单元测试中,经常需要进行一些mock操作。现在已经有了一些比较不错的框架在做这些事情,比如:EasyMck,他在大多数情况下运行良好,但是对于某些结构的设计却显得无能为力。

EasyMock等众多的mock框架仅能mock一些public,non static or final的方法,在大多数情况下这并没有什么问题,他可以处理大多数的问题,但是当测试的代码包含了一些静态方法,可能就让问题变得难以解决,有一种选 择即是重构他(过多的静态方法可能预示着这并不是一个很好的设计),但是当你使用外部引用库所提供的方法,问题又该如何解决呢?

JMockit是一个能帮我们解决以上问题的轻量级框架,他允许你动态的改变已有的方法,这主要基于java 1.5的Instrumentation框架,这样便可以使得JMockit能够适应几乎所有的设计。他允许你重定义private,static and final方法,甚至是no-arg constructors都能够并轻易的重定义。

在实际的项目中有些方法可以重定义而有些不行,为了更好的说明如何对方法进行重定义,下面有一个简单的类和对应测试代码的demo,他尽可能考虑到了几乎所有的情况,供大家方便的学习。(理解不透的地方,希望大家指正)

package jmockit;

public class ClassToMock {
	private String memberToSet = "";
    private static String staticMember;
    
    static {
        staticMember = "Static initialized";
    }
    
    public ClassToMock() {
        this.memberToSet = "Member set by original constructor";
    }
    public ClassToMock(String value){
    	this.memberToSet = value;
    }
    public String publicMethod() {
            return "Original public method";
    }
    protected String protectedMethod() {
            return "Original protected method";
    }
    String defaultMethod() {
            return "Original default method";
    }
    public String methodThatUsesPrivateMethod() {
            return privateMethod();
    }
    private String privateMethod() {
            return "Original private method";
    }
    public String getMemberToSet() {
            return memberToSet;
    }
    public String getStaticMember() {
            return staticMember;
    }
}

 
package jmockit;

import static org.junit.Assert.assertEquals;
import mockit.Mockit;

import org.junit.Before;
import org.junit.Test;

public class ClassToMockTest {

private ClassToMock mockedClass;

public static class Replacement {

static {
}

public Replacement() {
}

public Replacement(String test) {
}

public String publicMethod() {
return "Replaced public method";
}

public String protectedMethod() {
return "Replaced protected method";
}

public String defaultMethod() {
return "Replaced default method";
}

public String privateMethod() {
return "Replaced private method";
}
}

@Before
public void setUp() throws Exception {
Mockit.redefineMethods(ClassToMock.class, Replacement.class);
mockedClass = new ClassToMock("test");
}

protected void tearDown() throws Exception {
Mockit.restoreAllOriginalDefinitions();
}

/**
* Public methods can be replaced
*/
@Test
public void testReplacePublic() {
assertEquals("Replaced public method", mockedClass.publicMethod());
}

/**
* Protected methods can be replaced.
* The replacement method should be declared public however
*/
@Test
public void testReplaceProtected() {
assertEquals("Replaced protected method", mockedClass.protectedMethod());
}

/**
* Package accessable methods can be replaced
* The replacement method should be declared public however
*/
@Test
public void testReplaceDefault() {
assertEquals("Replaced default method", mockedClass.defaultMethod());
}

/**
* Private methods can be replaced
* The replacement method should be declared public however
*/
@Test
public void testReplacePrivate() {
assertEquals("Replaced private method", mockedClass
.methodThatUsesPrivateMethod());
}

/**
* Non-default constructors can be replaced
* Your mock definition must have a default constructor however
*/
@Test
public void testReplaceConstructor() {
assertEquals(null, mockedClass.getMemberToSet());
}

/**
* Default constructors <b>can't</b> be replaced
*/
@Test
public void testReplaceDefaultConstructor() {
mockedClass = new ClassToMock();
assertEquals("Member set by original constructor", mockedClass
.getMemberToSet());
}
/**
* Static initializers <b>can't</b> be replaced
*/
@Test
public void testReplaceStaticBlock() {
assertEquals("Static initialized", mockedClass.getStaticMember());
}
}
 注意要导包。
 官方文档:https://jmockit.dev.java.net/tutorial.html
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JMockit is a Java library that provides support for mocking and testing. The @Qualifier annotation is used in JMockit to identify a specific instance of a bean to be used in a test. In Spring, the @Qualifier annotation is used in a similar way to identify a specific bean to be injected into a component. However, in JMockit, the @Qualifier annotation is used in conjunction with other annotations to specify which instance of a mock or spy object to use in a test. For example, consider a scenario where we have two implementations of a service interface and we want to mock one of them for testing. We can use the @Qualifier annotation to identify the bean to be mocked and the @Mocked annotation to create a mock object of that bean. ``` public interface MyService { String getName(); } @Service("fooService") public class FooService implements MyService { @Override public String getName() { return "Foo"; } } @Service("barService") public class BarService implements MyService { @Override public String getName() { return "Bar"; } } public class MyServiceTest { @Test public void testGetName(@Mocked @Qualifier("fooService") MyService fooService, @Mocked @Qualifier("barService") MyService barService) { new Expectations() {{ fooService.getName(); result = "Mocked Foo"; barService.getName(); result = "Mocked Bar"; }}; // Use the mocked instances of fooService and barService in the test // ... } } ``` In the above example, we have two implementations of the MyService interface, FooService and BarService, and we want to mock FooService for testing. We use the @Qualifier("fooService") annotation to identify the bean to be mocked and the @Mocked annotation to create a mock object of that bean. We also create a mock object of the BarService bean using the @Mocked and @Qualifier("barService") annotations. We can then use these mocked instances of the beans in our test.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值