spock

Mock static method with GroovyMock or similar in Spock

…衆ロ難τιáo~ 提交于 2019-11-27 03:53:53
问题 First-timer here, apologies if I've missed anything. I'm hoping to get around a call to a static method using Spock. Feedback would be great With groovy mocks, I thought I'd be able to get past the static call but haven't found it. For background, I'm in the process of retrofitting tests in legacy java. Refactoring is prohibited. I'm using spock-0.7 with groovy-1.8. The call to the static method is chained with an instance call in this form: public class ClassUnderTest{ public void

Grails In Action-06.Controlling application flow

末鹿安然 提交于 2019-11-27 03:11:22
第六章主要讲解使用controller实现页面之间的跳转。在系统中实现了Post信息提交及显示功能。主要的知识点有: 访问路径控制: http://url:port/app/controller/action/params addPost方法:timeline页面的form将表单提交给addPost方法,addPost方法返回 .操作符:list .prop ?:操作符:表达式?如果true:如果false ${flash.massage} spock单元测试:controller页面重定向测试 spock单元测试:@spock.lang.Unroll spock单元测试:"Testing id of #suppliedId redirects to #expectedUrl" spock集成测试 默认访问index,重定向给timeline/params,如果没有params,默认给一个. <!-- lang: groovy --> ...... def index() { if (!params.id) params.id = "jeff" redirect action: 'timeline', params: params } def timeline() { def user = User.findByLoginId(params.id) if (!user) {

利用 Spock 为Java程序编写单元测试

我与影子孤独终老i 提交于 2019-11-27 03:11:08
Spock是一个用于Java或者Groovy程序的测试框架。利用Groovy的语言特性,Spock可以更加快速的编写单元测试,也可以使单元测试更加清晰、简洁。 更详细的介绍以及用法可以在官方文档上看到,下面进行一些简单的介绍和实例演示。 官方文档地址: http://spockframework.org/spock/docs/1.1-rc-3/index.html Spock 基础介绍 简单示例 import spock.lang.* class MyFirstSpec extends Specification { def "let's try this!"() { expect: Math.max(1, 2) == 3 } }​ 可以看到,利用Spock测试,需要首先继承Specification类,然后用def定义测试方法,方法名可以用字符串来表示,可以更好的描述被测试的方法。 想快速体验一下Spock,可以试试Spock Web Console,地址: https://meetspock.appspot.com/ 类变量 在测试类中定义的类变量,在每个测试方法开始前,都会被初始化一次 def action = new Action() 如果想要在测试方法之前共享类变量,需要在变量上加上注解 @Shared @Shared action = new Action()

Difference between Mock / Stub / Spy in Spock test framework

帅比萌擦擦* 提交于 2019-11-26 18:48:37
问题 I don't understand the difference between Mock, Stub, and Spy in Spock testing and the tutorials I have been looking at online don't explain them in detail. 回答1: Attention: I am going to oversimplify and maybe even slightly falsify in the upcoming paragraphs. For more detailed info see Martin Fowler's website. A mock is a dummy class replacing a real one, returning something like null or 0 for each method call. You use a mock if you need a dummy instance of a complex class which would

使用Spock框架进行单元测试

佐手、 提交于 2019-11-26 16:34:11
1. 关于单元测试 很多人一谈到单元测试就会想到xUnit框架。对于一些java新人来说,会用jUnit就是会写单元测试,高级点的会捣鼓一下testng,然后就认为自己掌握了单元测试。 而实际上,很多人不怎么会写单元测试,甚至不知道单元测试究竟是干什么的。写单元测试要比写代码要难上许多,而这里说的难度跟框架没什么关系。 所以,在开始介绍spock之前,需要先抛开框架,谈谈单元测试本身的事情。在理解了单元测试之后才能更清楚spock框架是什么,以及它否能够更优雅的解决你的问题。 1.1.1. 单元测试是什么 写代码免不了要做测试,测试有很多种,对于java来说,最初级的就是写个main函数运行一下看看结果,高级的可以用各种高大上的复杂的测试系统。每种测试都有它的关注点,比如测试功能是不是正确,或者运行状态稳不稳定,或者能承受多少负载压力,等等。 那么所谓的单元测试是什么?这里直接引用维基百科上的词条说明: 单元测试(又称为模块测试, Unit Testing)是针对程序模块(软件设计的最小单位)来进行正确性检验的测试工作。程序单元是应用的最小可测试部件。在过程化编程中,一个单元就是单个程序、函数、过程等;对于面向对象编程,最小单元就是方法,包括基类(超类)、抽象类、或者派生类(子类)中的方法。 所以,我眼中的“合格的”单元测试需要满足几个条件: 测试的是一个代码单元内部的逻辑

Integration Test with Spring Boot and Spock

微笑、不失礼 提交于 2019-11-26 15:28:04
问题 What is the best way to run an integration test (e.g., @IntegrationTest) with Spock? I would like to bootstrap the whole Spring Boot application and execute some HTTP calls to test the whole functionality. I can do it with JUnit (first the app runs and then the tests execute): @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = MyServer.class) @WebAppConfiguration @IntegrationTest class MyTest { RestTemplate template = new TestRestTemplate(); @Test public void