Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the difference between mock and stub in spock

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail what is the difference between mock and stub in spock. The content of the article is of high quality, so the editor will share it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

Test piles with stub

If we need to test method A, but method E cannot be called yet, or it hasn't been developed yet. In this scenario, you can use stub test piles. The stub test post can simulate one or more false return values for the E method. When we test, we only need to call the E method of the stub object, and the return value after the call is specified when we generate the stub object. As follows:

Def "Stub Test pile" () {given: "Construction Test pile" CalculateInterface calculateService = Stub (CalculateInterface) calculateService.plusPlus (_) > 1 when: int x = calculateService.plusPlus (12) int y = calculateService.plusPlus (3) then: X = = 1 y = = 1}

In the above code, calculateService.plusPlus (_) > > 1 specifies a return value of 1 for an unimplemented plusPlus () method, and the test code can call this method directly.

The common formats of this statement are:

Subscriber.receive (_) > > "ok" | generate return value | | Parameter | return value generated by method object: / / different parameters generate different return values subscriber.receive ("message1") > > "ok" subscriber.receive ("message2") > > "fail" > generate return values by calculation

In this way, a closure is generated when the format of the return value is generated

/ / 1. Use method parameters to calculate subscriber.receive (_) > {args-> args [0] .size () > 3? "ok": "fail"} / / 2 Use other parameters subscriber.receive (_) > {String message-> message.size () > 3? "ok": "fail"} if you want to call the method and throw an exception subscriber.receive (_) > > {throw new InternalError ("ouch")} chain generated return value subscriber.receive (_) > > ["ok", "fail", "ok"] > > {throw new InternalError ()} > > "ok"

In the above code, the first three calls to the method return "ok", "fail", and "ok" respectively, the fourth time throws an exception, and the fifth and subsequent calls return "ok".

The above is the use scenario of the stub test pile in spock, which can be summarized as follows: the stub test pile creates a false return value to the callee (method / module) so that it does not affect the caller's test.

Test piles with mock

The mock test pile simulates the results of a test. As shown in the following figure, class A calls a method of classes B and C:

If you want to test A's method, but we can't call B to test the result, you can use the mock test pile to generate a B's mock object. When verifying the results, you can use the mock object of B instead of B. This result is usually a call to the B and C methods or a change in state.

Def subscriber = Mock (Subscriber) / / 1. Create a mock object def "should send messages subscriber" () {when: publisher.send ("hello") / 2. Publisher sends a "hello" then: 1 * subscriber.receive ("hello") / / 3. Subscriber receives a "hello" 1 * subscriber.messageCount = = 1} that's all about the difference between mock and stub in spock. I hope the above content can be of some help to you and learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 240

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report