public void deletePostsWithTag(Tag tag) { for (Post post : blogService.getAllPosts()) { if (post.getTags().contains(tag)) { blogService.deletePost(post.getId()); } } }
public class FakeBlogService implements BlogService { private final Set<Post> posts = new HashSet<Post>(); // Store posts in memory public void addPost(Post post) { posts.add(post); } public void deletePost(int id) { for (Post post : posts) { if (post.getId() == id) { posts.remove(post); return; } } throw new PostNotFoundException("No post with ID " + id); } public Set<Post> getAllPosts() { return posts; } }
See also http://martinfowler.com/articles/mocksArentStubs.html, which explains the difference between Dummies, Fakes, Stubs and Mocks.
That article is a very good read, I think it should be required reading for anyone that writes unit tests. There will also be another Testing on the Toilet post coming up soon that discusses the different types of test doubles.
The comments you read and contribute here belong only to the person who posted them. We reserve the right to remove off-topic comments.
See also http://martinfowler.com/articles/mocksArentStubs.html, which explains the difference between Dummies, Fakes, Stubs and Mocks.
ReplyDeleteThat article is a very good read, I think it should be required reading for anyone that writes unit tests.
DeleteThere will also be another Testing on the Toilet post coming up soon that discusses the different types of test doubles.