Created
December 23, 2013 12:23
-
-
Save jameselsey/8096211 to your computer and use it in GitHub Desktop.
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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