spock

Grails 2.4.4 Testing for permission where spring security code is being used

*爱你&永不变心* 提交于 2019-12-07 12:12:27
问题 I am using spock for may application testing and using Grails 2.4.4. I have done domain, controller, and service unit testing. But in controller sections I am stuck with the role wise access. For authentication I am using Spring Security Core Plugin. Below is my sample code. @Secured(["IS_AUTHENTICATED_FULLY"]) def index(Integer max) { } @Secured(["ROLE_A","ROLE_B"]) def create() { respond new DomainName(params) } @Transactional @Secured(["ROLE_A","ROLE_B"]) def save(DomainName

Spock with Mockito testing Kotlin classes

▼魔方 西西 提交于 2019-12-07 10:06:37
问题 I have some tests written in Spock which covers my Java code. Now I migrate to Kotlin and the problem is i cannot mock final classes so i decide to use Mockito plugin described here: https://github.com/mockito/mockito/wiki/What%27s-new-in-Mockito-2#unmockable The question is do I need to replace every '_' '>>' with Mockitos' any(), anyString(), when(), then() etc? I tried to just mock final classes using Mockito but it seems not work. If i have to replace what the advantage is for using Spock

Spock mock returns wrong value

只愿长相守 提交于 2019-12-07 09:59:48
问题 I have got a strange use case where spock mock is returning the correct value when I do not check for two calls on the mocked class in the 'then:' section, but it is returning 0 when I include two checks in the 'then:' section. This is the mock: mockDao.readCounter(_, _, _, _, _) >> dbValue and here is the 'then:' section that fails: 1 * mockDao.readCounter(_, _, _, _, _) // updateCounters is called with: sum = dbValue + value 1 * mockDao.updateCounter(namespace, date, key, min, shardID,

spring boot 1.4, spock and application.properties

耗尽温柔 提交于 2019-12-07 06:49:26
问题 I am trying to write some tests for my Spring Boot 1.4.0 with Spock and my application-test-properties file is not being picked up. I have this in my gradle: dependencies { compile('org.springframework.boot:spring-boot-starter-data-jpa') compile('org.springframework.boot:spring-boot-starter-security') compile('org.springframework.boot:spring-boot-starter-web') compile 'org.codehaus.groovy:groovy-all:2.4.1' testCompile('org.springframework.boot:spring-boot-starter-test') testCompile('org

Intelli-J can't run spock tests? (Unable to attach test reporter…)

百般思念 提交于 2019-12-07 04:01:22
问题 I'm trying to use the grails spock plugin with 1.3.7. I can run tests from the terminal via grails test-app :spock , but when I try to run ControllerSpec tests from within Intelli-J, I get unable to attach test reporter to test framework or test framework quit unexpectedly . Has anyone run grails spock test successfully through Intelli-J? 回答1: Yes I have and I have no problems with it. The only thing to remember is that when you right-click your Spec class, to select the right intelliJ runner

grails spock test mock CommonsMultipartFile

浪子不回头ぞ 提交于 2019-12-07 03:10:33
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 { void "test someServiceMethod"() { given: MockMultipartHttpServletRequest request = new

mock out return of a method base on the number of invocation only in spock

冷暖自知 提交于 2019-12-07 02:23:53
问题 Is it possible to mock out the return value of a method in spock based on the nth time it was called? Note that I don't want to specify the parameters passed in because it does not matter for a specific test case. For example, for the first call it should return x, for the second call it should return y. 回答1: Yes it is possible. someObject.someMethod(*_) >>> [ 'x', 'y' ] It will return x on first invocation and y on second invocation of the method. Example: void "test something"() { given:

Spock - extracting interactions to method

五迷三道 提交于 2019-12-06 21:25:36
问题 The spock documentation points out that you can extract assertions of then block to other method and add assert keyword before each assertion. I would also like to extract interactions to helper method . I tried wrapping interactions with interaction closure block but that did not work. Is it possible? If it is how to achieve it? 回答1: Turns out you can. You have to wrap helper method call with interaction: then: interaction { helperMethod() } and then you can put interactions in a helper

Spock How to mock Autowired class' function call within a method

a 夏天 提交于 2019-12-06 15:00:09
I have a class that I want to test in that looks like this: package com.something; import org.springframework.beans.factory.annotation.Autowired; public class ClassToTest implements InterfaceToTest{ @Autowired AnotherService serviceA; @Override public List<String> methodToTest(List<String> randomVar){ ... String stringA = serviceA.someFunction(randomVar); ... } } How can I mock the results from the call to serviceA.someFunction(randomVar) to return any String of my choice when testing with spock? package com.something; import spock.lang.Shared import spock.lang.Specification class TestClass

Debugging Java code tested with Spock and JMockit

无人久伴 提交于 2019-12-06 14:30:59
问题 I'm using Spock to write tests for a Java application. The test includes a JMockit MockUp for the class. When debugging a test (using IntelliJ) I would like to be able to step into the Java code. I realize using F5 to step into the code won't work, because of all the Groovy magic with reflection that goes on. Unfortunately, even if I set a breakpoint in the Java code, it still will not be hit, even though it runs through the code. Here is the code to test: public class TestClass { private