spock

Spock mock verify returns 0 invocations

血红的双手。 提交于 2019-12-06 13:42:16
Im working on a Spring Boot java service that contains a Camel Processor class as follows: public class MyProc implements Processor { @Autowired private LogService logService; public void process(Exchange e) { // exchange object processing logService.update(e) } } And I have the following Spock test: class MyProcTest extends Specification { @Shared def logService = Mock(LogService) @Shared def proc = new MyProc() def ctx = new DefaultCamelContext() def exch = new DefaultExchange(ctx) void setupSpec() { proc.logService = logService } def "log is updated when valid exchange is processed"() {

How to run spock test from command line?

走远了吗. 提交于 2019-12-06 11:15:38
I have checked this link: https://gist.github.com/ysb33r/5825457 It seems like that it can be run like this: groovyc *.groovy java -cp ~/.grapes/org.spockframework/spock-core/jars/spock-core-0.7-groovy-2.0.jar:$GROOVY_HOME/embeddable/groovy-all-2.1.4.jar:$GROOVY_HOME/lib/junit-4.11.jar:$GROOVY_HOME/lib/hamcrest-core-1.3.jar:.org.junit.runner.JUnitCore ExampleSpec And I have added all third part jars to CLASSPATH,so all imports from these libs found.But all my own classes can't be found,and error message like this: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:

Mocking a Superclass in Spock

我怕爱的太早我们不能终老 提交于 2019-12-06 08:35:25
问题 How can you unit test a class that has a superclass in Spock that invokes method calls form its superclass? Or how do you mock a superclass in Spock? Ex: class Bar { def method1(parm1){ //Method actions } } class Foo extends Bar { def method2(param1, param2) { //Method actions super.method1(param1) } } How can I mock behavior of class Bar ? 回答1: You might use your class Foo as a Spy. The spy will create an instance of your class Foo but gives you the possibility of mocking any public methods

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

喜你入骨 提交于 2019-12-06 06:17:43
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, changes are still committed to DB. As it is work project, I can't provide some real code snippets,

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

試著忘記壹切 提交于 2019-12-06 04:15:38
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 something"() { given: params.username = "USERNAME" params.password = "SECRET" params.toModel = "Model"

Unable to mock Grails Service method when unit testing Controller - MissingMethodException

故事扮演 提交于 2019-12-06 04:06:11
问题 Am getting the following error message when testing the controller - see below for code. How can I correct this? When I invoke the service method from the controller (run-app) and it works fine. Exception: groovy.lang.MissingMethodException: No signature of method: grails.test.GrailsMock.isOk() is applicable for argument types: (java.lang.String) values: [H] at ...VControllerSpec.test something(VControllerSpec.groovy:) class: VControllerSpec import grails.test.mixin.TestFor import spock.lang

How to inject property values into a Spock test?

南笙酒味 提交于 2019-12-06 03:26:00
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") } @Shared properties cannot be injected, but something like this should work (with Spring 3): @Value("#

Is it possible to log spock feature method names and clause labels?

徘徊边缘 提交于 2019-12-06 02:49:42
问题 I'd like to be able to log the spock feature names and clause labels when running some automated tests. This would help with debugging test issues when using a headless browser for automation, specifically phantomjs. Reason being, phantomjs does not always behave the same way as when using the chrome WebDriver. It would also be nice to have if this is even possible. def "Login logout test"(){ given: "Go to login page" ... when: "Submit username and password" ... then: "Dashboard page

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

房东的猫 提交于 2019-12-05 18:32:02
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 DomainNameInstance) { } How do I test that only the user with ROLE_A and ROLE_B can create and save and other cannot?

Extract specifications from Spock specs

爷,独闯天下 提交于 2019-12-05 18:22:55
Is there a way to obtain specifications (filtering the code) from my Spock tests printed in a file? For example, for the following spec: class CarSpec extends IntegrationSpec { def 'it should not retrieve deleted cars'() { given: 'a car' def car = new Car(uniqueName: 'carName') car.save() when: 'I delete the car' car.delete() then: 'it shouldn't find me the car on the DB' Car.find { uniqueName == 'carName' } == null } } should print something like: CarSpec it should not retrieve deleted cars given a car when I delete the car then it shouldn't find me the car on the DB You could use one of the