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

How to use Test pile in spock Framework

2025-03-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly introduces how to use the test pile in the spock framework, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

Use the scene when:int a = 12 int b = xxxService.squre (a); int c = xxxService.plus (a, b); then:c = = 100

If we need to test the above code, but are told that the squre method has not been developed yet, or is in the process of being modified, we cannot get the correct results using it now.

At this point, you can use the test pile: create one or more false return values for the squre () method, so that we can test the later plus () method normally.

The specific explanation of the test pile Stub and Mock is explained in the following code comments.

Using the instance package com.yawn.spockimport com.yawn.spock.service.CalculateInterfaceimport com.yawn.spock.service.CalculateServiceimport spock.lang.Specification/** Stub has something in common with Mock * (1) * both Stub and Mock objects are used to simulate external dependencies so that we can control them. * if the program, system or object under test, we call it A. In the process of testing A, * A needs to interact with program, system, or object B, so Stub/Mock is used to simulate the behavior of B to interact with A. * (2) the difference * Stub, that is, "pile", has been said for a long time, mainly in the process of integration testing, * when integrating from top to bottom, as a substitute for the program below. The function, like its name, is to be able to find it when needed. It's like a roll call, "here". * Mock, which mainly refers to the puppet of a program, that is, a fake program, * can respond according to the wishes of the tester and return the information needed by the tested object. * that is, you want the wind to get the wind, the rain to get the rain, and the value you want to return. * * in general, stub completely simulates an external dependency to provide the test data needed for testing. * the mock object is used to determine whether the test can pass, that is, to verify that the interaction between dependent objects in the test is as expected. * * @ author yawn * 14:52 on 2019-6-10 * / class MockSpec extends Specification {def "mock 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} def "mock test pile gets different values" () {given: "Construction test pile" CalculateService calculateService = Stub (CalculateService) calculateService.plusPlus (_) > [1 2, 3] when: int x = calculateService.plusPlus (12) int y = calculateService.plusPlus (3) int z = calculateService.plusPlus (2) then: X = = 1 y = = 2 z = = 3}}

The corresponding zombie instance can be obtained by using the Stub and Mock methods, and we can define the return value of its method for this zombie instance.

CalculateService.plusPlus (_) > > 1 / / defines a return value

CalculateService.plusPlus (_) > [1,2,3] / / defines multiple return values, one of which is taken as the return value in sequence for each execution.

Thank you for reading this article carefully. I hope the article "how to use Test piles in spock Framework" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

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: 0

*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