From 6a1e24553c9b56a2edbf3c40bf909004faa17e29 Mon Sep 17 00:00:00 2001 From: Gustavo Martin Morcuende Date: Thu, 5 Nov 2015 19:54:20 +0100 Subject: [PATCH] TDD: check for exceptions --- TDD/src/main/java/de/example/services/impl/UsersServiceImpl.java | 4 ++++ TDD/src/test/java/de/example/services/UsersServiceTest.java | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/TDD/src/main/java/de/example/services/impl/UsersServiceImpl.java b/TDD/src/main/java/de/example/services/impl/UsersServiceImpl.java index 7743cd2..d0a3a39 100644 --- a/TDD/src/main/java/de/example/services/impl/UsersServiceImpl.java +++ b/TDD/src/main/java/de/example/services/impl/UsersServiceImpl.java @@ -1,5 +1,7 @@ package de.example.services.impl; +import java.util.Objects; + import de.example.model.mapper.UsersMapper; import de.example.model.resource.UserResource; import de.example.services.UsersService; @@ -20,6 +22,8 @@ public class UsersServiceImpl implements UsersService { @Override public void create(final UserResource user) { + Objects.requireNonNull(user, "user"); + this.spiedMethod(user); usersMapper.create(user); diff --git a/TDD/src/test/java/de/example/services/UsersServiceTest.java b/TDD/src/test/java/de/example/services/UsersServiceTest.java index ad8e697..57ee670 100644 --- a/TDD/src/test/java/de/example/services/UsersServiceTest.java +++ b/TDD/src/test/java/de/example/services/UsersServiceTest.java @@ -58,6 +58,12 @@ public class UsersServiceTest { assertEquals(expectedUser, newUser); } + @Test(expected=NullPointerException.class) + public void givenNoUserDataWhenCreateNewUserThenThrowNullPointerException() { + // Act + usersService.create(null); + } + @Test public void givenNewUserDataWhenCreateNewUserThenCreateUserWithArgumentCaptor() { // Arrange -- 2.1.4