JMockit 101 – JMockit 101

最后修改: 2016年 7月 6日

中文/混合/英文(键盘快捷键:t)

1. Introduction

1.介绍

With this article, we’ll be starting a new series centered around the mocking toolkit JMockit.

通过这篇文章,我们将围绕嘲讽工具包JMockit开始一个新的系列。

In this first installment we’ll talk about what JMockit is, its characteristics and how mocks are created and used with it.

在第一部分中,我们将讨论什么是JMockit,它的特点以及如何用它创建和使用模拟。

Later articles will focus on and go deeper into its capabilities.

以后的文章将重点关注并深入研究其能力。

2. JMockit

2.JMockit

2.1. Introduction

2.1.简介

First of all, let’s talk about what JMockit is: a Java framework for mocking objects in tests (you can use it for both JUnit and TestNG ones).

首先,我们来谈谈什么是JMockit:一个用于在测试中模拟对象的Java框架(你可以将其用于JUnitTestNG的)。

It uses Java’s instrumentation APIs to modify the classes’ bytecode during runtime in order to dynamically alter their behavior. Some of its strong points are its expressibility and its out-of-the-box ability to mock static and private methods.

它使用Java的工具化API在运行期间修改类的字节码,以便动态地改变它们的行为。它的一些强项是它的可表达性和它开箱即用的模拟静态和私有方法的能力。

Maybe you’re new to JMockit, but it’s definitely not due to it being new. JMockit’s development started in June 2006 and its first stable release dates to December 2012, so it’s been around for a time now (current version is 1.24 at the time of writing the article).

也许你是第一次接触JMockit,但这绝对不是因为它是新的。JMockit的开发始于2006年6月,它的第一个稳定版本可以追溯到2012年12月,所以它已经存在一段时间了(在写这篇文章时,当前版本是1.24)。

2.2. Maven Dependency

2.2.Maven的依赖性

First, we’ll need to add the jmockit dependency to our project:

首先,我们需要将jmockit依赖性添加到我们的项目中。

<dependency> 
    <groupId>org.jmockit</groupId> 
    <artifactId>jmockit</artifactId> 
    <version>1.41</version>
</dependency>

2.3. The Expressibility of JMockit

2.3.JMockit的可表达性

As told before, one of the strongest points of JMockit is its expressibility. In order to create mocks and define their behavior, instead of calling methods from the mocking API, you just need to define them directly.

如前所述,JMockit最强的一点是其可表达性。为了创建模拟并定义它们的行为,你只需要直接定义它们,而不是从模拟API中调用方法。

This means that you won’t do things like:

这意味着你不会做这样的事情。

API.expect(mockInstance.method()).andThenReturn(value).times(2);

Instead, expect things like:

相反,期待的是这样的事情。

new Expectation() {
    mockInstance.method(); 
    result = value; 
    times = 2;
}

It might seem that it is more code, but you could simply put all three lines just on one. The really important part is that you don’t end up with a big “train” of chained method calls. Instead, you end up with a definition of how you want the mock to behave when called.

这似乎是更多的代码,但你可以简单地把这三行都放在一个地方。真正重要的部分是,你不会以一列连环方法调用的大 “火车 “而告终。相反,你最终得到的是一个关于你希望mock被调用时的行为的定义。

If you take into account that on the result = value part you could return anything (fixed values, dynamically generated values, exceptions, etc), the expressiveness of JMockit gets even more evident.

如果你考虑到在result = value部分你可以返回任何东西(固定值、动态生成的值、异常等),JMockit的表现力就更加明显了。

2.4. The Record-Replay-Verify Model

2.4.记录-重放-验证模式

Tests using JMockit are divided into three differentiated stages: record, replay and verify.

使用JMockit的测试分为三个不同的阶段:记录、回放和验证。

  1. On the record phase, during test preparation and before the invocations to the methods we want to be executed, we will define the expected behavior for all tests to be used during the next stage.
  2. The replay phase is the one in which the code under test is executed. The invocations of mocked methods/constructors previously recorded on the previous stage will now be replayed.
  3. Lastly, on the verify phase, we will assert that the result of the test was the one we expected (and that mocks behaved and were used according to what was defined in the record phase).

With a code example, a wireframe for a test would look something like this:

以代码为例,一个测试的线框看起来会是这样的。

@Test
public void testWireframe() {
   // preparation code not specific to JMockit, if any

   new Expectations() {{ 
       // define expected behaviour for mocks
   }};

   // execute code-under-test

   new Verifications() {{ 
       // verify mocks
   }};

   // assertions
}

3. Creating Mocks

3.创建Mocks

3.1. JMockit’s Annotations

3.1.JMockit的注释

When using JMockit, the easiest way to use mocks, is to use annotations. There are three for creating mocks (@Mocked, @Injectable and @Capturing) and one to specify the class under testing (@Tested).

当使用JMockit时,使用mocks的最简单方法是使用注解。有三个用于创建模拟(@Mocked, @Injectable @Capturing),一个用于指定测试中的类(@Tested)。

When using the @Mocked annotation on a field, it will create mocked instances of each and every new object of that particular class.

当在一个字段上使用@Mocked注解时,它将为该特定类的每一个新对象创建模拟的实例。

On the other hand, with the @Injectable annotation, only one mocked instance will be created.

另一方面,使用@Injectable注解,将只创建一个模拟的实例。

The last annotation, @Capturing will behave like @Mocked, but will extend its reach to every subclass extending or implementing the annotated field’s type.

最后一个注解,@Capturing将像@Mocked,一样行事,但会将其范围扩大到每个扩展或实现注解字段类型的子类。

3.2. Passing Arguments to Tests

3.2.向测试传递参数

When using JMockit is possible to pass mocks as test parameters. This is quite useful for creating a mock just for that one test in particular, like some complex model object that needs a specific behavior just for one test for instance. It would be something like this:

当使用JMockit时,可以将模拟作为测试参数传递。这对于创建一个特别的测试的模拟是非常有用的,比如说一些复杂的模型对象需要一个特定的行为,而这只是为了一个测试。它将会是这样的。

@RunWith(JMockit.class)
public class TestPassingArguments {
   
   @Injectable
   private Foo mockForEveryTest;

   @Tested
   private Bar bar;

   @Test
   public void testExample(@Mocked Xyz mockForJustThisTest) {
       new Expectations() {{
           mockForEveryTest.someMethod("foo");
           mockForJustThisTest.someOtherMethod();
       }};

       bar.codeUnderTest();
   }
}

This way of creating a mock by passing it as a parameter, instead of having to call some API method, again shows us the expressibility we’re talking about since the beginning.

这种通过传递参数来创建模拟的方式,而不是必须调用一些API方法,再次向我们展示了我们从一开始就在谈论的可表达性。

3.3. Complete Example

3.3.完整的例子

To end this article, we’ll be including a complete example of a test using JMockit.

在本文的最后,我们将包括一个使用JMockit进行测试的完整例子。

In this example, we’ll be testing a Performer class that uses Collaborator in its perform() method. This perform() method, receives a Model object as a parameter from which it will use its getInfo() that returns a String, this String will be passed to the collaborate() method from Collaborator that will return true for this particular test, and this value will be passed to the receive() method from Collaborator.

在这个例子中,我们将测试一个Performer类,它在其perform()方法中使用Collaborator。这个perform()方法接收一个Model对象作为参数,它将使用其getInfo()返回一个字符串。这个字符串将被传递给Collaboratorcollaborate()方法,该方法将为这个特定的测试返回true,并且这个值将被传递给Collaboratorreceive()方法。

So, the tested classes will look like this:

因此,经过测试的班级将看起来像这样。

public class Model {
    public String getInfo(){
        return "info";
    }
}

public class Collaborator {
    public boolean collaborate(String string){
        return false;
    }
    public void receive(boolean bool){
        // NOOP
    }
}

public class Performer {
    private Collaborator collaborator;
	
    public void perform(Model model) {
        boolean value = collaborator.collaborate(model.getInfo());
        collaborator.receive(value);
    }
}

And the test’s code will end up being like:

而测试的代码最后会变成这样。

@RunWith(JMockit.class)
public class PerformerTest {

    @Injectable
    private Collaborator collaborator;

    @Tested
    private Performer performer;

    @Test
    public void testThePerformMethod(@Mocked Model model) {
        new Expectations() {{
    	    model.getInfo();result = "bar";
    	    collaborator.collaborate("bar"); result = true;
        }};
        performer.perform(model);
        new Verifications() {{
    	    collaborator.receive(true);
        }};
    }
}

4. Conclusion

4.结论

With this, we’ll wrap up our practical intro to JMockit. If you want to learn more about JMockit, stay tuned for future articles.

至此,我们就结束了对JMockit的实用介绍。如果你想了解更多关于JMockit的信息,请继续关注以后的文章。

The full implementation of this tutorial can be found on the GitHub project.

本教程的完整实现可以在GitHub项目中找到。

4.1. Articles in the Series

4.1.该系列的文章

All articles of the series:

该系列的所有文章。