我正在尝试测试一种简单的服务方法.该方法主要只返回一个条件查询的结果,如果它返回一个结果,我想测试它(取决于查询的内容).
问题是,我不知道如何正确地对应相应的测试.我试图通过spock实现它,但是对任何其他测试方式做同样的事情也失败了.
可以告诉我如何修改测试以使其适用于手头的任务吗?
(顺便说一下,如果可能的话,我想把它作为单元测试.)
EventService方法
public HashSet<Event> listEventsForDate(Date date, int offset, int max) {
date.clearTime()
def c = Event.createCriteria()
def results = c {
and {
le("startDate", date+1) // starts tonight at midnight or prior?
ge("endDate", date) // ends today or later?
}
maxResults(max)
order("startDate", "desc")
}
return results
}
Run Code Online (Sandbox Code Playgroud)
Spock规范
package myapp
import grails.plugin.spock.*
import spock.lang.*
class EventServiceSpec extends Specification {
def event
def eventService = new EventService()
def setup() {
event = new Event() …Run Code Online (Sandbox Code Playgroud) 以前我使用Robot Framework来自动化应用程序测试,但是新客户要求关注Spock + GEB.我从来没有使用它,但我需要快速比较其中两个工具并做出选择.请帮助我了解它们之间的区别以及每种方法的优点/缺点.
我的Android应用程序是多模块项目:
include (android-app/kotlin-android)':application', (pure kotlin)':presentation', (pure kotlin)':domain', (android-library/kotin-android)':dataproviders'
Run Code Online (Sandbox Code Playgroud)
模块:应用程序和:数据提供程序与Spock一起正常运行,测试运行和完成没有问题.但是:表示和:纯kotlin模块的域有spock框架的问题.有我的简单例子:
MostPopularPresenterTest.groovy
class MostPopularPresenterTest extends Specification {
def "exampleTest"(){
when:
int i = 1
then:
i == 1
}
}
Run Code Online (Sandbox Code Playgroud)
此测试以错误结束:
未找到类:"pl.hypeapp.presentation.mostpopular.MostPopularPresenterTest"空测试套件.
但是,用Java/Junit4编写的测试传递良好并且没有抛出任何错误:
MostPopularPresenterTest2.java
public class MostPopularPresenterTest2 {
@Test
public void test(){
assertEquals(1, 1);
}
}
Run Code Online (Sandbox Code Playgroud)
我正在使用Android Studio 3.0 Canary 5您可以在github上查看我的build.gradle文件:
有人可以帮助我解决我的问题吗?
编辑:运行/.gradlew测试测试正在运行.
我有EncouragementService.groovy以下方法
class EncouragementService {
def stripePaymentService
def encourageUsers(List<User> users){
if(null != users && users.size()>0){
for(User user : users){
//logic
stripePaymentService.encourage(user)
//
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
要在JAVA Universe中测试上面的代码,使用JUnit我首先要在安装程序中创建两个或三个用户.将用户列表传递给encourageUsers(...)方法,并根据结果检查我想要的内容.
我怎样才能在grails中实现同样的目标,
import com.github.jmkgreen.morphia.Datastore;
@TestFor(EncouragementService)
class EncouragementServiceSpec {
def morphiaService = new MorphiaService()
void testEncourageUsers() {
List<User> users = createUsers();
encouragementService.(users)
//
}
def createUsers(){
Datastore datastore = morphiaService.dataStoreInstance()
def user = new User()
user.setName("Prayag Upd")
//
datastore.save(user)
[user]
}
}
Run Code Online (Sandbox Code Playgroud)
我正在使用spock:0.7
plugins {
test(":spock:0.7") { exclude "spock-grails-support" } …Run Code Online (Sandbox Code Playgroud) 我使用Spock框架测试我的应用程序,测试是用Groovy编写的.
作为一些方法评估的结果,我有一个对象列表.我想测试这个列表是否与我期望的列表相同.我编码了以下内容:
def expectedResults = [ ... ] //the list I expect to see
def isEqual = true;
when:
def realResults = getRealResultsMethod() //get real results in a list here
expectedResults.each {isEqual &= realResults.contains(it)}
then:
isEqual
0 * errorHandler.handleError(_) //by the way assert that my errorHandler is never called
Run Code Online (Sandbox Code Playgroud)
这是我第一次使用Groovy的经历,所以可能是我错过了什么?
PS
令我困惑的是Groovy和Spock中的'equals'运算符.给定Java ArrayList或Java数组,equals运算符只是标识运算符:equals is ==.在Groovy中,据我所知,默认等于运算符实际上是等于(在此处形成:http://groovy.codehaus.org/Differences+from+Java).但是Groovy List或Set的'等于'是什么?
UPDATE
更确切地说.我想找出两个列表有相同的对象,两个列表都没有额外的对象,顺序无关紧要.例如:
list=[1,5,8]
list1=[5,1,8]
list2=[1,5,8,9]
println(list == list1) //should be equal, if we use == not equal
println(list == list2) …Run Code Online (Sandbox Code Playgroud) 有 | 是 | 这么多 | Spock | spec如何使用其标签的示例,例如:
// when -> then label combo
def "test something"() {
when:
// blah
then:
// blah blah
}
Run Code Online (Sandbox Code Playgroud)
这样的标签组合如下:
when - > thengiven- > when- >thenexpectgiven - > expect但我无处可以找到有关这些标签的合法 /有意义组合的文档.例如,我可以:
def "do something"() {
when:
// blah
expect:
// blah
}
Run Code Online (Sandbox Code Playgroud)
我可否?我不知道.关于什么:
def "do something else"() {
when:
// blah
then:
// blah
expect:
// blah
where:
// blah
}
Run Code Online (Sandbox Code Playgroud)
我可否?再说一次,我不知道.但我想知道.
使用JUnit,Groovy,Spock和Maven时,我遇到了并行执行JUnit测试的问题.执行它们时,我在测试成功通过后得到以下内容:
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 18.362s
[INFO] Finished at: Wed Mar 20 15:14:25 CET 2013
[INFO] Final Memory: 16M/221M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.14:test (default-test) on project spock-webdriver: ExecutionException; nested exception is java.util.concurrent.ExecutionException: java.lang.RuntimeException: There was an error in the forked process
[ERROR] java.lang.NoSuchMethodError: org.apache.maven.surefire.common.junit4.JUnit4RunListener.rethrowAnyTestMechanismFailures(Lorg/junit/runner/Result;)V
[ERROR] at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:129)
[ERROR] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[ERROR] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
[ERROR] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[ERROR] at java.lang.reflect.Method.invoke(Method.java:601)
[ERROR] at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray2(ReflectionUtils.java:208)
[ERROR] at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:158)
[ERROR] at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:86)
[ERROR] …Run Code Online (Sandbox Code Playgroud) 我有一些使用Generics编写的Java代码.这是一个简单的版本:
// In Java
public interface Testable {
void test();
}
public class TestableImpl implements Testable {
@Override
public void test(){
System.out.println("hello");
}
}
public class Test {
public <T extends Testable> void runTest(Collection<T> ts){
System.out.println("Collection<T>");
for(T t: ts)
t.test();
}
public void runTest(Object o){
System.out.println("Object");
System.out.println(o);
}
}
// in Groovy - this is how I have to use the code
Test test = new Test()
test.runTest([new TestableImpl(), new TestableImpl()])
test.runTest([1,2,3]) //exception here
Run Code Online (Sandbox Code Playgroud)
我很惊讶第二个方法调用被调度到错误的方法(在我的Javish理解中是错误的).而是调用Object重载,调用Collectiongets.
我使用的是Groovy …
我们已将我们的应用程序从Grails 2.4.4升级到2.5.0.一切正常,但有些测试用例失败,其中包含一些私有方法.
import org.junit.Before
class UserServiceSpec extends IntegrationSpec {
@Before
def setup() {
// Some initialization
}
// being used in other test methods
private void getParams() {
// some code
}
void testUserUniqueEmali() {
when: "this runs"
// code
then: "hello"
// code
}
}
Run Code Online (Sandbox Code Playgroud)
抛出异常:
java.lang.IllegalAccessException: Class org.spockframework.util.ReflectionUtil can not access a member of class com.test.UserServiceSpec with modifiers "private"
at org.spockframework.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:153)
at org.spockframework.runtime.model.MethodInfo.invoke(MethodInfo.java:84)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at …Run Code Online (Sandbox Code Playgroud) 我在Intellij IDEA中运行了9个JUnit(实际上是Spockframework)测试.大约需要3秒钟.
我想利用所有核心,因此我切换测试配置fork模式 - 类.
Edit configurations > Fork mode > class
这会导致构建时间增加到8秒.尝试使用fork模式方法使它成为22秒.测试运行程序过程看起来像是按顺序运行而不是并行运行.
关于为什么不分支测试按预期工作的任何想法?