spock

Spock - Mock private field

只愿长相守 提交于 2021-02-11 15:06:41
问题 I have to mock private field: public class A{ private final B b; public A(){ this.b = new B(new OtherService) } public test(){ int i = b.test() if(i == 0) b.test1() else b.test2() } } and I have to create unit tests for methods in this kind of class and I have to mock class B is it even possible here? 回答1: Please read my remarks here and here or in many other questions of this kind I answered concerning dependency injection (DI). The lack thereof in your tightly coupled application design is

How to test spring batch job within @Transactional SpringBootTest test case?

早过忘川 提交于 2021-02-11 14:46:25
问题 I just can't seem to win today... Is there a way to read from a OneToMany relationship in a Spock SpringBootTest integration test, without annotating the test as @Transactional or adding the unrealistic spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true ? OR, is there a way to launch a Spring-Batch Job from within a @Transactional test case? Let me elaborate... I'm trying to get a simple Spring Boot Integration test working for my Spring Batch reporting process, which reads from

2018 ACM-ICPC 中国大学生程序设计竞赛线上赛

此生再无相见时 提交于 2021-02-11 06:43:12
2018 ACM-ICPC 中国大学生程序设计竞赛线上赛 [TOC] A. Death is end 留坑 B.Goldbach 题意 每次给一个偶数$n(n<2<2^{63})$,找出任意两个和为$n$的素数。 分析 从n的$\frac{1}{2}$往两边判素数,使用Miller Rabin随机性素数测试方法。 (ps:自己写了一个虽然过了,但是会被Carmichael数卡掉,还没搞懂板子上的算法怎么搞定Carmichael数的,<font color=red>留坑</font>) <details><summary> 代码 </summary> ```cpp #include <iostream> #include <cstdio> #include <queue> #include <cstring> #include <vector> #include <map> #include <algorithm> using namespace std; typedef long long ll; const ll MOD=1e9+7; const int maxn=1000500; struct Miller_Rabin { int prime[5]={2,3,5,233,331}; ll qmul(ll x,ll y,ll mod){ ll ans=(x*y-(ll)(

Grails 4.0.1 - Spock unit test cases fail because of spaces

点点圈 提交于 2021-02-08 11:20:29
问题 I am testing i18n formatting using unit tests in my Grails 4.0.1 application but seeing strange results. To me, this specs condition should be satisfied, but it is not. I don't suppose anyone has had a similar experience that can be explained, or maybe it's simply a bug? Any help would be appreciated. 回答1: Just as an idea: Can you iterate over both expected and actual values and convert the characters one by one to the ascii so that you'll probably see the difference for( c in ​'123 456,78 $'

How to test a function which instantiate a variable from external call in Spock

走远了吗. 提交于 2021-01-29 09:22:28
问题 Hi I want to test below function using Spock framework private Connection getDBConnection() throws SQLException { DBClient client = DBService.getClient() String[] connInfo = client.getHistoryDBConnectInfo() String url = connInfo[1]; String user = connInfo[2]; String pwd = connInfo[3]; Connection conn; if(!StringUtils.containsIgnoreCase(url,"Authentication=ActiveDirectoryMsi")) conn = DriverManager.getConnection(url, user, pwd); else conn= DriverManager.getConnection(url); return conn; } Here

Unexpected behaviour when Spock datatable's header contains variable named like variable in the other feature method

允我心安 提交于 2021-01-27 20:40:29
问题 I have a problem with data-driven tests in Spock. When datatable header contains variable (in this case 'b') named like variable in the other feature method, the following exception is thrown: org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '3' with class 'java.lang.Integer' to class 'java.util.UUID' Example specification class: class ExampleSpec extends Specification { def 'example feature with variable named like datatable header'() { given: UUID b = UUID

How to make multiple requests with different data in Grails Integration tests

♀尐吖头ヾ 提交于 2021-01-27 04:06:53
问题 I'm writing a Grails 2.2.1 integration test using the Spock plugin, in which I am trying to post two sets of data to the same controller endpoint: when: "The user adds this product to the inventory" def postData = [productId: 123] controller.request.JSON = postData controller.addToInventory() and: "Then they add another" def secondPostData = [productId: 456] controller.request.JSON = secondPostData controller.addToInventory() then: "The size of the inventory should be 2" new JSONObject(

Spock mock private variable

烈酒焚心 提交于 2021-01-03 09:54:22
问题 I'm wondering how I can mock some private variable in a class with Groovy/Spock. Let's say we have this code: public class Car { private Engine engine; public void drive(){ System.out.println("test"); if (engine.isState()) { // Do something } else { // Do something } } } In Mockito I can write: @Mock private Engine engine; @InjectMocks private Car car = new Car(); @Test public void drive() { when(engine.isState()).thenReturn(true); car.drive(); } But I don't know how to do the same in Spock.

Generics with Spock Stub

谁都会走 提交于 2020-12-31 04:43:39
问题 I cannot make compile Spock stub for generic class. The signature of constructor is following: SomeClass(SerSup<Cap> capSup, String foo, String bar); I need to stub the first argument. The following are my failed attempts. First try: def someClass = new SomeClass(Stub(SerSup<Cap>), "foo", "bar") Error: Groovyc: unexpected token: > Status bar: ',' or ')' expected Another try: def someClass = new someClass(Stub(Cup) as SerSup<Cup>, "foo" ,"bar") groovy.lang.MissingMethodException: No signature

Generics with Spock Stub

故事扮演 提交于 2020-12-31 04:41:45
问题 I cannot make compile Spock stub for generic class. The signature of constructor is following: SomeClass(SerSup<Cap> capSup, String foo, String bar); I need to stub the first argument. The following are my failed attempts. First try: def someClass = new SomeClass(Stub(SerSup<Cap>), "foo", "bar") Error: Groovyc: unexpected token: > Status bar: ',' or ')' expected Another try: def someClass = new someClass(Stub(Cup) as SerSup<Cup>, "foo" ,"bar") groovy.lang.MissingMethodException: No signature