Gist for my spock blog post: http://www.jameselsey.co.uk/blogs/techblog/why-all-java-devs-should-at-least-consider-groovy-and-spock-for-testing Place source files in src/main and run `gradle idea` to create an intelliJ project file
apply plugin: 'groovy' | |
apply plugin: 'idea' | |
repositories { | |
mavenCentral() | |
} | |
dependencies { | |
groovy "org.codehaus.groovy:groovy-all:1.8.6" | |
testCompile "org.spockframework:spock-core:0.6-groovy-1.8" | |
} |
package com.jameselsey.demo.spocktutorial.domain; | |
public class User { | |
private int id; | |
private String name; | |
private int age; | |
public int getId() { | |
return id; | |
} | |
public void setId(int id) { | |
this.id = id; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public int getAge() { | |
return age; | |
} | |
public void setAge(int age) { | |
this.age = age; | |
} | |
} |
package com.jameselsey.demo.spocktutorial.dao; | |
import com.jameselsey.demo.spocktutorial.domain.User; | |
public interface UserDao { | |
public User get(int id); | |
public User findByName(String name); | |
public void createUser(User user); | |
} |
package com.jameselsey.demo.spocktutorial.service; | |
import com.jameselsey.demo.spocktutorial.dao.UserDao; | |
import com.jameselsey.demo.spocktutorial.domain.User; | |
public class UserService { | |
private UserDao userDao; | |
public UserService(UserDao userDao) { | |
this.userDao = userDao; | |
} | |
public User findUser(int id){ | |
return userDao.get(id); | |
} | |
public void createUser(User user){ | |
User existing = userDao.findByName(user.getName()); | |
if(existing == null){ | |
userDao.createUser(user); | |
} else{ | |
throw new RuntimeException(String.format("User with name %s already exists!", user.getName())); | |
} | |
} | |
} |
package com.jameselsey.demo.spocktutorial.service | |
import com.jameselsey.demo.spocktutorial.dao.UserDao | |
import com.jameselsey.demo.spocktutorial.domain.User | |
import spock.lang.Specification | |
class UserServiceTest extends Specification { | |
UserService service | |
UserDao dao = Mock(UserDao) | |
def setup(){ | |
service = new UserService(dao) | |
} | |
def "it gets a user by id"(){ | |
given: | |
def id = 1 | |
when: | |
def result = service.findUser(id) | |
then: | |
1 * dao.get(id) >> new User(id:id, name:"James", age:27) | |
result.id == 1 | |
result.name == "James" | |
result.age == 27 | |
} | |
def "it saves a new user"(){ | |
given: | |
def user = new User(id: 1, name: 'James', age:27) | |
when: | |
service.createUser(user) | |
then: | |
1 * dao.findByName(user.name) >> null | |
then: | |
1 * dao.createUser(user) | |
} | |
def "it fails to create a user because one already exists with that name"(){ | |
given: | |
def user = new User(id: 1, name: 'James', age:27) | |
when: | |
service.createUser(user) | |
then: | |
1 * dao.findByName(user.name) >> user | |
then: | |
0 * dao.createUser(user) | |
then: | |
def exception = thrown(RuntimeException) | |
exception.message == "User with name ${user.name} already exists!" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment