我有一些像这样的Java东西:
public interface EventBus{
void fireEvent(GwtEvent<?> event);
}
public class SaveCommentEvent extends GwtEvent<?>{
private finalComment oldComment;
private final Comment newComment;
public SaveCommentEvent(Comment oldComment,Comment newComment){
this.oldComment=oldComment;
this.newComment=newComment;
}
public Comment getOldComment(){...}
public Comment getNewComment(){...}
}
Run Code Online (Sandbox Code Playgroud)
并测试代码如下:
def "...."(){
EventBus eventBus=Mock()
Comment oldComment=Mock()
Comment newCommnet=Mock()
when:
eventBus.fireEvent(new SaveCommentEvent(oldComment,newComment))
then:
1*eventBus.fireEvent(
{
it.source.getClass()==SaveCommentEvent;
it.oldComment==oldComment;
it.newComment==newComment
}
)
}
Run Code Online (Sandbox Code Playgroud)
我想验证eventBus.fireEvent(..)
获取与类型的事件称为一次SaveCommentEvent
和施工参数oldComment
和newComment
.
代码运行没有错误,但问题是:
更改封闭后的东西
{
it.source.getClass()==SaveCommentEvent;
it.oldComment==oldComment; //old==old
it.newComment==newComment //new==new
}
Run Code Online (Sandbox Code Playgroud)
至
{
it.source.getClass()==Other_Class_Literal;
it.oldComment==newComment; //old==new
it.newComment==oldComment //new==old …
Run Code Online (Sandbox Code Playgroud) 使用Mockito示例页面中最基本的示例,我能够在JUnit中成功运行.
但是,当我在Spock中运行相同的测试时,它会失败.
JUnit/Java版本(通过):
import org.junit.Test;
import java.util.List;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
public class SimpleJunitTest
{
@Test
public void basicMockTest()
{
List mockedList = mock(List.class);
//using mock object
mockedList.add("one");
mockedList.clear();
//verification
verify(mockedList).add("one");
verify(mockedList).clear();
}
}
Run Code Online (Sandbox Code Playgroud)
Spock/Groovy版本(失败):
import static org.mockito.Mockito.mock
import static org.mockito.Mockito.verify
class SimpleSpockTest extends spock.lang.Specification
{
def "Basic Mock Test"()
{
given:
//mock creation
List mockedList = mock(List.class);
when:
//using mock object
mockedList.add("one");
mockedList.clear();
then:
//verification
verify(mockedList).add("one");
verify(mockedList).clear();
}
}
Run Code Online (Sandbox Code Playgroud)
这是我在Spock测试失败时得到的错误:
Condition not satisfied:
verify(mockedList).add("one")
| …
Run Code Online (Sandbox Code Playgroud) 没有太多补充,整个问题在标题中.
考虑Spock规范中使用的这两个Foo类实例.
@Shared Foo foo1 = new Foo()
static Foo foo2 = new Foo()
Run Code Online (Sandbox Code Playgroud)
总的来说,我知道@Shared
注释背后的想法,但我想最好使用语言功能,在这种情况下将是static
字段.
是否有任何特定的案例,其中一个人应该优先于另一个,或者它是一个品味问题?
有没有办法设置在Spock规范中执行测试的顺序?
例如:
class MySpec extends IntegrationSpec {
def 'test A'...
def 'test B'...
}
Run Code Online (Sandbox Code Playgroud)
我想'test A'总是在'test B'之前执行
这是因为我正在使用Geb和Spock进行一些功能测试,并且数据不会在测试之间回滚.
我在then子句中有一个带循环的测试:
result.each {
it.name.contains("foo")
it.entity.subEntity == "bar"
}
for (String obj : result2) {
obj.name.contains("foo")
obj.entity.subEntity == "bar"
}
Run Code Online (Sandbox Code Playgroud)
最近我意识到循环没有真正测试过.无论我是否有foo或bar或其他任何东西,测试总是绿色的:)我发现,循环必须以不同的方式进行测试,例如使用'every'?但只是将'each'更改为'every'抛出异常:
result.every {
it.name.contains("foo")
it.entity.subEntity == "bar"
}
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Spec expression: 1: expecting '}', found '==' @ line 1, column 61.
s("foo") it.entity.rootEntity == "bar" }
Run Code Online (Sandbox Code Playgroud)
我应该如何在测试中正确使用循环?我正在使用spock 0.7-groovy-2.0
我用Spock测试Java代码.我测试这段代码:
try {
Set<String> availableActions = getSthAction()
List<String> goodActions = getGoodAction()
if (!CollectionUtils.containsAny(availableActions ,goodActions )){
throw new CustomException();
}
} catch (AnotherCustomExceptio e) {
throw new CustomException(e.getMessage());
}
Run Code Online (Sandbox Code Playgroud)
我写了测试:
def "some test"() {
given:
bean.methodName(_) >> {throw new AnotherCustomExceptio ("Sth wrong")}
def order = new Order();
when:
validator.validate(order )
then:
final CustomException exception = thrown()
}
Run Code Online (Sandbox Code Playgroud)
它失败了,因为AnotherCustomExceptio
抛出了.但在try{}catch
块中我捕获此异常并抛出一个CustomException
所以我期望我的方法将抛出CustomException
而不是AnotherCustomExceptio
.我该如何测试?
在JUnit 3中,我可以得到当前运行的测试的名称,如下所示:
public class MyTest extends TestCase {
public void testSomething() {
assertThat(getName(), is("testSomething"));
}
}
Run Code Online (Sandbox Code Playgroud)
我怎么在spock中这样做?我想将测试名称用作共享资源中的密钥,以便测试不会相互干扰.
我想用mock模拟一些流畅的界面,这基本上是一个邮件生成器:
this.builder()
.from(from)
.to(to)
.cc(cc)
.bcc(bcc)
.template(templateId, templateParameter)
.send();
Run Code Online (Sandbox Code Playgroud)
使用Spock进行模拟时,需要进行大量的设置:
def builder = Mock(Builder)
builder.from(_) >> builder
builder.to(_) >> builder
Run Code Online (Sandbox Code Playgroud)
当你想根据用例测试与模拟的某些交互时,它变得更加麻烦.所以我在这里基本上有两个问题:
有没有办法用较少的代码指定一个流畅的接口的模拟,例如:
def builder = Mock(Builder)builder./(from|to|cc|bcc |template)/(*) >> builder
或类似于Mockito的Deep Stubs的东西(参见http://docs.mockito.googlecode.com/hg/org/mockito/Mockito.html#RETURNS_DEEP_STUBS)
scalatest和spock有何不同?每个的附加价值是多少?哪个更适合行为驱动开发(BDD)?请问您能就此事分享一些想法吗?
我想开始BDD,我想在两者之间选择一个,因此我想做出一个有根据的决定.因此,我首先得到最多的信息,特别是考虑到我是一名java程序员,scala似乎有一个重要的学习曲线.
任何建议或想法或经验回报都是受欢迎的.
非常感谢
我们将Spock测试与Spring的@ContextConfiguration结合起来,这样我们就可以在spring上下文中构建bean,然后使用Spock进行实际测试.我们想把spock mocks注入我们的春豆.对于Mockito,有一个扩展,允许您执行以下操作:
<mockito:mock id="accountService" class="org.kubek2k.account.DefaultAccountService" />
Run Code Online (Sandbox Code Playgroud)
然后将这个模拟引用到其他spring bean.Spock似乎没有这样的扩展.如果您知道如何在Specification类之外创建Mocks,那么再次构建它可能不会太费力.创建我所知道的Spock模拟的唯一方法是:
T Mock(Class<T> type)
Run Code Online (Sandbox Code Playgroud)
在规范中.Spock中是否有一些API可以在不在Specification类中时创建Mocks,所以我可以为Spring上下文创建Spock模拟?