当前位置:网站首页>Practical guide for Spock unit testing framework 8 - Common mock encapsulation into base classes

Practical guide for Spock unit testing framework 8 - Common mock encapsulation into base classes

2020-12-06 23:02:02 Java old K

This is an introduction to the actual use of Spock How to extract some commonly used test methods in the process of , Encapsulate as a base class

BaseSpock

In the previous articles, I explained Spock combination power mock Implementing static methods mock When the function , Example code often uses LogUtils And other tool class static methods to log , Then we can put LogUtils Class mock The code is extracted into a public class , Then our test class inherits our own public class implementation

For example, we call the public class BaseSpock.groovy file , Then the subclass that inherits it has the simulation LogUtils The function of static methods , Instead of testing each class individually mock LogUtils Log function

The code is as follows :

/**
 * Spock Base class 
 * @Author: www.javakk.com
 * @Description:  official account :Java The old K
 */
@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(Sputnik.class)
@PrepareForTest([LogUtils.class])
@SuppressStaticInitializationFor(["com.javakk.spock.util.LogUtils"])
class BaseSpock extends Specification{

    void setup() {
        println "Spock setup"
        // mock Drop some of the commonly used classes in projects , Like logging 
        PowerMockito.mockStatic(LogUtils.class)
        ......
    }
}

BaseSpock It's packaged by us spock Base class , It inherited Specification, stay setUp Method internally LogUtils the mock

BaseSpock It can be placed in a public project or as jar Method reference of

image

It can also be placed in src/main/groovy/ The following is called as a public class

And then I used LogUtils Unit tests of log classes can inherit BaseSpock Base class

class UserServiceStaticTest extends BaseSpock {

In this way, the original unit test will no longer be implemented mock LogUtils Log class static method function

If you're not LogUtils These common classes require mock Outside , It also needs to be mock Other static methods , Use the spock combination power mock It's easy to use , Write something like this :

@PrepareForTest([IDNumberUtils.class])
class UserServiceStaticTest extends BaseSpock {

The current unit test class needs mock IDNumberUtils class , have access to @PrepareForTest annotation , You can use the base class mock LogUtils Log function , You can also add new static methods to your test classes mock function

There are several points to pay attention to :

BaseSpock The type is groovy file , This is because Spock The built-in test engine will check whether the subclass inheriting it is groovy Files of type , So if you want to encapsulate something like BaseSpock Base class of , File suffix cannot be .java Of , Must be .groovy The type of

be-all Spock A singleton class cannot have its own constructor , Because the examples of unit tests are made by Spock Create and manage

Spock The running order of unit test code is :

setupSpec()setup()cleanup()cleanupSpec()

  • setupSpec Be similar to Junit Of @beforeClass
  • setup Be similar to Junit Of @before
  • cleanup Be similar to Junit Of @after
  • cleanupSpec Be similar to Junit Of @afterClass

版权声明
本文为[Java old K]所创,转载请带上原文链接,感谢
https://chowdera.com/2020/12/202012062300595517.html