spock

Groovy Singleton and testing issue (with Spock)

倖福魔咒の 提交于 2019-12-23 22:10:23
问题 There's a discusson here about testing and singletons... but that is about Java patterns. My question is specifically about the Groovy @Singleton (annotation) way of implementing this pattern. This seems like another bit of Groovy Goodness. But I have a bit of a problem when testing (with Spock) using a class which has this annotation. If any of the state of this instance changes during a test (from the pristine, just-constructed state), as far as my experiments indicate this will then carry

NullpointerException: cannot get property on null object

你离开我真会死。 提交于 2019-12-23 19:34:01
问题 Working on IDEA and trying to launch the following code: package com.myCompany.routing.spring import com.dropbox.core.DbxRequestConfig import grails.util.Holders import spock.lang.Specification class DropboxSpringConfigSpec extends Specification { def grailsApplication=Holders.grailsApplication def "It instantiates and configures the dropboxRequestConfig component"() { given: def ctx = grailsApplication.mainContext //do stuff... } } I get the following error: java.lang.NullPointerException:

grails spock test mock CommonsMultipartFile

一个人想着一个人 提交于 2019-12-23 02:21:39
问题 I've following old method written in code, which is for accessing request object in service class such as: def someServiceMethod() { .... def webUtils = WebUtils.retrieveGrailsWebRequest() def request = webUtils.getCurrentRequest() MultipartHttpServletRequest mpr = (MultipartHttpServletRequest) request CommonsMultipartFile file = (CommonsMultipartFile) mpr.getFile("file") .... } This is my unit test code for serivce class. @TestFor(SomeService) class SomeServiceSpec extends Specification {

Geb & Spock - If/Then/Else logic - How to check for a record and do one thing if it exists but continue on if it doesn't

蓝咒 提交于 2019-12-23 01:42:03
问题 I'm testing the creation and then removal of a record on my site using Geb/Spock. However I can't create the record if it already exists and so I check for the existence of the record and remove it if it exists in the beginning of the test. The problem occurs when the record does not exist, causing the test to fail. Is there a way to incorporate some if/then/else logic so the test will continue if it does not find the record in the beginning and remove it if it does find it? Edit for example

Grails Controller Unit Test doesn't render page to response.text

匆匆过客 提交于 2019-12-23 00:49:51
问题 My env configs: Java 1.7u51, Grails 2.3.7 I'm trying to assert response.text in Controller Test but it always brings "". What's happening? This is my UserController class UserController { def index() { flash.errors = "$params.secret" render view: "index", model: [model: params.toModel, text: params.username] } } This is /user/index.gsp file ${text} This is my Specification @TestFor(UserController) class UserControllerSpec extends Specification { def setup() { } def cleanup() { } void "test

Grails 3.0.9: Spock integration tests' @Rollback annotation is not working

时间秒杀一切 提交于 2019-12-22 11:32:58
问题 I'm currently working on some integration (or functional, I'm not really a QA but back-end dev, so I might be sloppy with terms) REST tests for our project, we are using Grails 3.0.9, Spock Framework 1.0-Groovy-2.4 and PostgreSQL DB for testing. Also we have separate DB for testing purposes, it is still crucial that changes are rolled back after each test. I have looked through Grails testing doc and was trying to use @Rollback annotation as described in examples - and it is just not working,

How to inject property values into a Spock test?

社会主义新天地 提交于 2019-12-22 09:59:29
问题 When using a Spock test, i have hardcoded some properties hardcoded into the spock test. The example is a JDBC url. I tried the @Value annotation together with a propertie file, but this seems not to work as my test has no stereotype. Are there any other solutions to inject property values? @ContextConfiguration(locations = "classpath*:applicationContext-test.xml") class RepositoryTest extends Specification { @Shared sql = Sql.newInstance("jdbc:sqlserver:// - room - for - properties") } 回答1:

Does Spock have Test Event Listeners

Deadly 提交于 2019-12-22 08:16:41
问题 Does spock has any Test event listener like how TestNg has ITestListener . ? So that I can have access, when the test cases failed etc. 回答1: Spock does have listeners. Unfortunately the official documentation, which is otherwise excellent, has "TODO" under Writing Custom Extensions : http://spockframework.github.io/spock/docs/1.0/extensions.html. Update: The official docs have been updated to include helpful information about custom extensions: http://spockframework.org/spock/docs/1.1

Grails Spock unit test requires to mock transaction manager

核能气质少年 提交于 2019-12-22 05:57:37
问题 In Grails 3.1.12, I want to unit test a service: @Transactional class PlanService { List<Plan> getPlans(Map params) { def currentUser = (User)springSecurityService.getCurrentUser() return Plan.findAllByCompany(currentUser.employer, params) } } Like this: @TestFor(PlanService) @Mock([Plan, User, Company]) class PlanServiceSpec extends Specification { void "Retrieve plan from the current user"() { setup: // create and save entities here when: "the plans are retrieved" def params = null def

Injecting a mock service into a domain class for a Grails unit test?

孤者浪人 提交于 2019-12-22 04:47:33
问题 I am writing some Spock spec based unit tests under Grails 2.1.1. I'm having trouble getting springSecurityService injected into my domain object that is used by my unit. This is what I have so far, @Mock([SecUser]) @TestFor(FooService) class FooServiceSpec extends Specification { def "test some stuff"() { given: def mockSecUserService = Mock(SecUserService) mockSecUserService.emailValid(_) >> { true } mockSecUserService.checkUsername(_) >> { null } service.secUserService = mockSecUserService