`
nanjingjiangbiao_T
  • 浏览: 2596473 次
  • 来自: 深圳
文章分类
社区版块
存档分类
最新评论

use jmockit to mock your static method

 
阅读更多
http://spritesun.blogbus.com/logs/83396826.html

Mockup always comes to us when we try to break system dependency. The conventional mockup frameworks only allowed us to mock instance methods. So the static methods usually become to the death of testability.

Of course, we still have some way to make it become testable.

1. If the static methods invoke only happens in few classes, we could create an adaptor to isolate them. The implementation of adaptor is just simply delegation.

2. Unfortunately, sometimes your legacy system maybe full of static methods calling, they scatter in different tiers.

It indeed a bad smell, but before you refactor them, you may need to make them testable firstly. Now, jmockit comes to us.

"Tests can easily be written that will mock final classes, static methods, constructors, and so on. There are no limitations."

-- jmockit

The following example shows its ability to mock static methods.

//src file

public class ServiceFactory {

public static String getAService() {

return "real service";

}

}

//test file

import mockit.Mock;

import mockit.Mockit;

import static org.junit.Assert.assertEquals;

import org.junit.Test;


public class ServiceFactoryTest {

@Test

public void should_return_mock_service() {

Mockit.setUpMock(ServiceFactory.class, ServiceFactoryStub.class);

assertEquals("mock service", ServiceFactory.getAService());

}


private static class ServiceFactoryStub {

@Mock

public static String getAService() {

return "mock service";

}

}

}

Caution:

Make sure jmockit.jar appears before any JUnit jar file in the classpath. Otherwise, it will shows warning:

"WARNING: JMockit was initialized on demand, which may cause certain tests to fail;
please check the documentation for better ways to get it initialized."

分享到:
评论

相关推荐

    JMOCKIT mock 静态方法 单元测试

    可以用来Mock JAVA代码中的静态方法

    jmockit测试例子

    2. Unfortunately, sometimes your legacy system maybe full of static methods calling, they scatter in different tiers. It indeed a bad smell, but before you refactor them, you may need to make them ...

    JMockit教程.pdf

    正如其名,在单元测试时,CUT(Code Under Test ,被测试代码)可能会依赖其他代码,可以使用JMockit API来mock被依赖的代码,从而进行隔离测试。 JMockit有两套API,mock API用来进行mock测试;mock-up API用来...

    jmockit总结

    jmockit资料,关于jmockit的具体使用方式,以及不同方法的详解。

    Unit Testing C_C++ Code_ When to Mock.pdf

    Useful questions to determine whether or not to mock - Is the real collaborator a source of risk for the stability of my tests? - Is it difficult to initialize the real collaborator? - Is it ...

    jmockit-1.7.jar

    jmockit-1.7.jar JMockit最新工具包

    spring-mock.jar

    Classes contained in spring-mock.jar: org.springframework.mock.jndi.ExpectedLookupTemplate.class org.springframework.mock.jndi.SimpleNamingContext.class org.springframework.mock.jndi....

    swagger-to-mock:Swagger3(OpenAPI 3)的模拟数据生成器CLI

    swagger-to-mock < YOUR> 将在每个API响应中生成JSON文件。 如果您指定了示例,则JSON数据值应该是您的招摇中的示例值。 否则, swagger-to-mock遵循并生成任意值。 如果没有诸如format规则,则值应在下面。 ...

    开源项目-goware-httpmock.zip

    开源项目-goware-httpmock.zip,httpmock - a simple way to mock 3rd party services in your tests

    基于python的mock测试数据练习

    mock练习

    Google_Mock_启蒙篇.pdf

    Google_Mock_启蒙篇 当你写一个原型戒是测试的时候,直接去依赖真实的对象通常是丌可行的戒是丌明智的。 Mock 对象实现不真实对象有着相同的接口,但你可以去指定 Mock 对象在运行时它做什么

    test.jmockit-0.998.jar包

    单元测试 mock jmockit包 JMockit 是一个轻量级的mock框架是用以帮助开发人员编写测试程序的一组工具和API

    Jmockit用法示例

    Jmockit用法示例,对各种函数(公有、私有、静态、final、无参数、带参数、接口)进行mock的方法

    mockServer,mock服务端

    简单的mockServer、模拟后台生成数据、启动后像后台一样直接调用接口

    Node.js-简单的数据mock服务器

    easy to mock data by http server(简单的数据mock服务器)

    mock在ut中的使用

    mock

    JMockit学习分享

    对JMockit 测试工具精华的提炼和分析,关注到mock单元测试最核心的概念和思路。

    Testing with F#(PACKT,2015)

    You will learn techniques used to mock database contexts, test stored procedures, use migrations to test databases in isolation, control a web browser with Canopy, and test web services with F# ...

    ApiMock操作文档

    ApiMock操作文档

    mock 测试.pptx

    mock测试实战

Global site tag (gtag.js) - Google Analytics