我不明白Spock测试中Mock,Stub和Spy之间的区别,我在网上看过的教程并没有详细解释它们.
如何用Spock以一种很好的方式测试异常(例如数据表)?
示例:具有validateUser
可以使用不同消息抛出异常的方法,或者如果用户有效则无异常.
规范类本身:
class User { String userName }
class SomeSpec extends spock.lang.Specification {
...tests go here...
private validateUser(User user) {
if (!user) throw new Exception ('no user')
if (!user.userName) throw new Exception ('no userName')
}
}
Run Code Online (Sandbox Code Playgroud)
变式1
这一个是工作,但真正的意图是所有的混乱时,/然后标签和的多次呼吁validateUser(user)
.
def 'validate user - the long way - working but not nice'() {
when:
def user = new User(userName: 'tester')
validateUser(user)
then:
noExceptionThrown()
when:
user = new User(userName: null)
validateUser(user)
then:
def ex …
Run Code Online (Sandbox Code Playgroud) @IntegrationTest
使用Spock 运行集成测试(例如)的最佳方法是什么?我想引导整个Spring Boot应用程序并执行一些HTTP调用来测试整个功能.
我可以使用JUnit(首先运行app然后执行测试):
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = MyServer.class)
@WebAppConfiguration
@IntegrationTest
class MyTest {
RestTemplate template = new TestRestTemplate();
@Test
public void testDataRoutingWebSocketToHttp() {
def a = template.getForEntity("http://localhost:8080", String.class)
println a
}
}
Run Code Online (Sandbox Code Playgroud)
但是使用Spock,应用程序无法启动:
@SpringApplicationConfiguration(classes = MyServer.class)
@WebAppConfiguration
@IntegrationTest
class MyTestSpec extends Specification {
RestTemplate template = new TestRestTemplate();
def "Do my test"() {
setup:
def a = template.getForEntity("http://localhost:8080", String.class)
expect:
println a
}
}
Run Code Online (Sandbox Code Playgroud)
当然,对于Spock,我在Gradle构建文件中指定了正确的依赖项:
...
dependencies {
testCompile 'org.spockframework:spock-core:0.7-groovy-2.0'
testCompile 'org.spockframework:spock-spring:0.7-groovy-2.0'
}
...
Run Code Online (Sandbox Code Playgroud)
我错过了什么吗?
我环顾四周,尝试了不同的事情无济于事.互联网上的例子很少,恕我直言很简单.我的用例:
(下面的'itocNetworkHandler'是模拟)
when: "we're doing stuff"
StandardResponse response = cms.doCardStuff("123", "111", order)
....
then: "we get proper calls and response object"
1 * cms.itocNetworkHandler.doNetworkCall(
{ it instanceof ReplacementRequestRecord
}, StandardResponseRecord.class) >> record
Run Code Online (Sandbox Code Playgroud)
我想将参数('it')存储到mock上的"doNetworkCall"中.
我想要参数的原因是因为我正在测试的对象应该接受我的参数,做东西,创建一个新对象并将那个传递给我的模拟.我想确保创建的对象看起来像它应该的样子.
指针非常赞赏.
我很难在Groovy单元测试中理解有关Spock交互的内容.
我有以下类型:
public interface Bar {
public String getMessage();
}
public class Foo {
private Bar bar;
public void setBar(Bar bar) {
this.bar = bar;
}
public String getMessage() {
return bar.getMessage();
}
}
Run Code Online (Sandbox Code Playgroud)
然后我编写了以下Groovy/Spock测试:
class FooSpec extends Specification {
private Bar bar;
private Foo foo;
def setup() {
bar = Mock(Bar) { getMessage() >> "hello" }
foo = new Foo()
foo.bar = bar
}
def "say hello"() {
expect:
foo.message.equals("hello")
}
def "say goodbye"() {
setup:
bar.getMessage() >> "goodbye" …
Run Code Online (Sandbox Code Playgroud) 我是groovy的新手(在java上工作),尝试使用Spock框架编写一些测试用例.我需要使用"每个循环"将以下Java代码段转换为groovy代码段
List<String> myList = Arrays.asList("Hello", "World!", "How", "Are", "You");
for( String myObj : myList){
if(myObj==null) {
continue; // need to convert this part in groovy using each loop
}
System.out.println("My Object is "+ myObj);
}
Run Code Online (Sandbox Code Playgroud)
Groovy Snippet:
def myObj = ["Hello", "World!", "How", "Are", "You"]
myList.each{ myObj->
if(myObj==null){
//here I need to continue
}
println("My Object is " + myObj)
}
Run Code Online (Sandbox Code Playgroud) 我是Spock的新手,并仔细阅读他们的在线文档.我有一个测试用例,我需要验证我的fixture与非模拟协作者的交互不会产生异常:
class FizzSpec extends Specification {
def "no exception thrown when we hail buzz"() {
given:
Fizz fixture = new Fizz()
Buzz buzz = new Buzz("YES", true, "Garble barb") // A non-mock!
when:
fixture.hail(buzz)
// TODO: How to verify the hail didn't produce an exception?
// then:
// thrown() == null
}
}
Run Code Online (Sandbox Code Playgroud)
关于如何实现这一点的任何想法?
我有一个测试规范Spock
,我想将相同的测试应用于另一个替代方案,但一个方法除外.我扩展了原始规范,实现了与替代方案相关的一些自定义内容,并且真的想要排除一个测试方法的执行.
我试过了
@Ignore
def "Something must work in a certain way"() {
//No implementation
}
Run Code Online (Sandbox Code Playgroud)
但似乎无论如何都要调用该方法.
这可以实现吗?
在之前的项目中,我使用Spock测试框架对我的Java代码进行单元测试.我发现这非常有效,所以我试图将Spock测试添加到我当前的项目中,该项目使用Maven作为其构建工具(之前的项目使用了Gradle).虽然我可以让Maven编译我的Spock测试(使用groovy-eclipse-compiler
),但我无法让Maven运行测试.
我做了一个简单的例子来演示我的2个文件的问题:
pom.xml
src/test/java/ASpec.groovy
内容pom.xml
:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>my.group</groupId>
<artifactId>my-artifact</artifactId>
<version>0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>2.0.8</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-core</artifactId>
<version>0.7-groovy-2.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<compilerId>groovy-eclipse-compiler</compilerId>
</configuration>
<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-eclipse-compiler</artifactId>
<version>2.8.0-01</version>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-eclipse-batch</artifactId>
<version>2.1.8-01</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
Run Code Online (Sandbox Code Playgroud)
内容ASpec.groovy
:
import spock.lang.Specification
class ASpec extends Specification {
def "Test A"(){
// Always fail
expect: false …
Run Code Online (Sandbox Code Playgroud)