spock

Using Spock to mock private static final variables in Java

心已入冬 提交于 2019-12-04 05:01:27
I'm trying to write some Spock tests with Groovy to test some Java code (specifically a servlet Filter). I have some private static and private static final variables that I would like to mock, but I can't determine if there is a way to do this. I know metaClass is available for methods, is there anything similar for variables? For instance, I have: public class MyFilter implements Filter { private static WebResource RESOURCE; private static final String CACHE_KEY = "key-to-be-used-for-cache"; ... actual methods, etc ... } I've tried using Mock(MyFilter) , as well as using Java reflection to

How to get name of currently running test in spock?

本小妞迷上赌 提交于 2019-12-04 02:47:15
问题 In JUnit 3, I could get the name of the currently running test like this: public class MyTest extends TestCase { public void testSomething() { assertThat(getName(), is("testSomething")); } } How do I do this in spock? I would like to use the test name as a key in a shared resource so that tests don't interfere with each other. 回答1: One solution is to leverage JUnit's TestName rule: import org.junit.Rule import org.junit.rules.TestName class MySpec extends Specification { @Rule TestName name =

Can TestNG see my Spock (JUnit) test results?

一曲冷凌霜 提交于 2019-12-04 01:29:05
问题 Need to get TestNG to run my Spock Tests as TestNG is used throughout the rest of the system. Since TestNG supports running JUnit tests, I tried this: <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" > <suite name="suiteTestName" verbose="1" junit="true"> <test name="myTestName"> <classes> <class name="mypackage.artifact.SomeArtifactSpecification" /> </classes> </test> </suite> So this actually ran the Groovy 'SomeArtifactSpecification' Spock Unit Test perfectly. I could see some I/O

Can Spock Mock a Java constructor

删除回忆录丶 提交于 2019-12-04 00:57:22
问题 Trying to broaden the appeal of Spock at work and run into this issue. Actually trying to write Unit Tests for a Groovy class, but one that calls out to Java. A static method calls a private constructor. The code looks like: private MyConfigurator(String zkConnectionString){ solrZkClient = new SolrZkClient(zkConnectionString, 30000, 30000, new OnReconnect() { @Override public void command() { . . . } }); } "SolrZkClient" is from third party (Apache) Java library. Since it tries to connect to

How do you get the sessionFactory in a Grails Geb/Spock test case?

别说谁变了你拦得住时间么 提交于 2019-12-03 21:29:04
I think I need to flush the hibernate session in a GebSpec test, and so I want to get the sessionFactory. It looks like it should be injected but when I do something like this:- class MySpec extends GebSpec { def sessionFactory ... def "test session"(){ ....do some setup then: assert sessionFactory != null } it fails with sessionFactory being null. The short answer to my question is - why do you want to do that, it's a functional test and it may be remote from the running apps JVM. The why is because I want to check that domain objects have been updated when web stuff happens. Luke Daley

Using spock data table for filling objects

ε祈祈猫儿з 提交于 2019-12-03 17:27:35
I am using Spock for the first time. Since we are using a complex domain-model it would be handy to have a mechanism, which allows me to create full objects from data given by spock tables. I do not want to give all values each time, I just want to set the values in defined in datable. So there should be defined default values somewhere. Yes, I know I could write on my own, but maybe there is an out-of-the-box solution. Example class A { String name int age } spock table id | givenA | ... 1 | [name: "Michael"] | ... 2 | [name: "Thomas", age: 45 ] | ... => A.name = "Michael", A.age = defined

Inject dependencies in Grails Spock Specification test

风流意气都作罢 提交于 2019-12-03 16:51:55
问题 I need to get the dependencies injected in my domain objects in my tests. This tests are placed in the test/integration directory and extends from spock.lang.Specification . How can I achieve this? Note: I've seen this post How to inject spring beans into spock test, but it is not related with grails. Edit: The dependency I want to get injected is springSecurityService in my SecUser subclass called Player . The method that is failing is the encodePassword() , which is called in the

Difficulty running grails tests in IntelliJ : Illegal use of nonvirtual function call

这一生的挚爱 提交于 2019-12-03 14:18:59
Lately I've been trying to run my spock tests in IntelliJ (which used to work beautifully and had great debugging / specific test re-running on failure features) and in the past few months I've began getting the following error: | Error Error executing script TestApp: (class: com/company/MyServiceSpec, method: super$2$oldImpl signature: (Ljava/lang/Object;)Ljava/lang/Object;) Illegal use of nonvirtual function call (Use --stacktrace to see the full trace) It's a spock test that runs just fine from the command line, individually (by specifying the class) or in the entire test-app series.

Selecting specific tests to run in gradle

折月煮酒 提交于 2019-12-03 12:14:49
I'm trying to fix our messy failing test runs, and, unfortunately, I'm very new to gradle. We currently have testng, junit, and I'd like to add some spock tests to the mix as well. I'm not quite sure how gradle determines which tests to run when I type "gradle test". How can I prevent the testng &/or junit tests from running? How can I get gradle to start running my spock tests? Peter Niederwieser By default, the test task runs all JUnit tests it can find, which includes any Spock tests. To make it run TestNG tests instead, configure the task as follows: test { useTestNG() } If you have both

How to test Grails service using Spock?

主宰稳场 提交于 2019-12-03 11:54:00
I have a EncouragementService.groovy with following method class EncouragementService { def stripePaymentService def encourageUsers(List<User> users){ if(null != users && users.size()>0){ for(User user : users){ //logic stripePaymentService.encourage(user) // } } } } To test above code in JAVA universe, using JUnit I would first create two or three users in setup. Pass the list of users to encourageUsers(...) method and check whatever I want with the result. How can I achieve the same thing here in grails, import com.github.jmkgreen.morphia.Datastore; @TestFor(EncouragementService) class