Commit 7a79c7613a97b8c6a4d726fa77eab718b68d9779

Authored by Thomas Ziemer
1 parent b9b94a9f

test: Vorlage zum UserServiceTest

README.md
@@ -3,13 +3,12 @@ @@ -3,13 +3,12 @@
3 ## Projekt-Voraussetzungen 3 ## Projekt-Voraussetzungen
4 4
5 - Versionsverwaltungswerkzeug "Git" 5 - Versionsverwaltungswerkzeug "Git"
6 -- installiertes Java 8 JDK (mit Dokumentation)  
7 -- eine Java-Entwicklungsumgebung ("Eclipse", "JetBrains IntelliJ IDEA", "NetBeans" o.ä.) 6 +- Installiertes Java 8 JDK (mit Dokumentation)
  7 +- Eine Java-Entwicklungsumgebung ("Eclipse", "JetBrains IntelliJ IDEA", "NetBeans" o.ä.)
8 - JEE-Application Server "JBoss WildFly Version 8.2.1-final" 8 - JEE-Application Server "JBoss WildFly Version 8.2.1-final"
9 -- Plugin "JBoss Tools" (nur für Eclipse)  
10 -- Build-System "maven"  
11 -- ein relationales Datenbankverwaltungssystem ("MySQL" empfohlen, oder "MariaDB", "PostgreSQL", "Oracle Express" etc.)  
12 -- ein REST-Client (empfohlen "Advanced Rest Client Application" für Chrome) 9 +- Build-System "maven" (optional)
  10 +- Ein relationales Datenbankverwaltungssystem mit JDBC-Anbindung ("MySQL" empfohlen, oder "MariaDB", "PostgreSQL", "Oracle Express" etc.)
  11 +- Ein REST-Client (empfohlen "Advanced Rest Client Application" für Chrome)
13 12
14 ## Java-Projekt herunterladen 13 ## Java-Projekt herunterladen
15 14
@@ -9,6 +9,8 @@ @@ -9,6 +9,8 @@
9 <url>http://maven.apache.org</url> 9 <url>http://maven.apache.org</url>
10 10
11 <properties> 11 <properties>
  12 + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  13 +
12 <mvn-compiler-plugin-version>3.3</mvn-compiler-plugin-version> 14 <mvn-compiler-plugin-version>3.3</mvn-compiler-plugin-version>
13 <jee-version>7.0</jee-version> 15 <jee-version>7.0</jee-version>
14 <java-version>1.8</java-version> 16 <java-version>1.8</java-version>
src/test/java/net/ziemers/swxercise/lg/testdata/testdatabuilder/user/ProfileTestDataBuilder.java
@@ -7,11 +7,11 @@ import javax.persistence.EntityManager; @@ -7,11 +7,11 @@ import javax.persistence.EntityManager;
7 7
8 public class ProfileTestDataBuilder extends AbstractTestDataBuilder { 8 public class ProfileTestDataBuilder extends AbstractTestDataBuilder {
9 9
10 - private String username = "tziemer"; 10 + private String username = "hbloed";
11 11
12 private String password = "secret"; 12 private String password = "secret";
13 13
14 - private String mailaddress = "tziemer@ziemers.net"; 14 + private String mailaddress = "hbloed@ziemers.net";
15 15
16 public ProfileTestDataBuilder(final EntityManager em) { 16 public ProfileTestDataBuilder(final EntityManager em) {
17 super(em); 17 super(em);
src/test/java/net/ziemers/swxercise/lg/testdata/testdatabuilder/user/UserDtoTestDataBuilder.java 0 → 100644
  1 +package net.ziemers.swxercise.lg.testdata.testdatabuilder.user;
  2 +
  3 +import net.ziemers.swxercise.lg.testdata.testdatabuilder.AbstractTestDataBuilder;
  4 +import net.ziemers.swxercise.lg.user.dto.UserDto;
  5 +
  6 +import javax.persistence.EntityManager;
  7 +
  8 +public class UserDtoTestDataBuilder extends AbstractTestDataBuilder<UserDto> {
  9 +
  10 + private String username = "hein";
  11 +
  12 + private String password = "secret";
  13 +
  14 + private String firstname = "Hein";
  15 +
  16 + private String lastname = "Blöd";
  17 +
  18 + private String mailaddress;
  19 +
  20 + @Override
  21 + public UserDto build() {
  22 + return new UserDto()
  23 + .withUsername(username)
  24 + .withPassword(password)
  25 + .withFirstname(firstname)
  26 + .withLastname(lastname)
  27 + .withMailaddress(mailaddress);
  28 + }
  29 +
  30 + public UserDtoTestDataBuilder withUsername(final String username) {
  31 + this.username = username;
  32 + return this;
  33 + }
  34 +
  35 + public UserDtoTestDataBuilder withPassword(final String password) {
  36 + this.password = password;
  37 + return this;
  38 + }
  39 +
  40 + public UserDtoTestDataBuilder withFirstname(final String firstname) {
  41 + this.firstname = firstname;
  42 + return this;
  43 + }
  44 +
  45 + public UserDtoTestDataBuilder withLastname(final String lastname) {
  46 + this.lastname = lastname;
  47 + return this;
  48 + }
  49 +
  50 + public UserDtoTestDataBuilder withMailaddr(final String mailaddress) {
  51 + this.mailaddress = mailaddress;
  52 + return this;
  53 + }
  54 +
  55 +}
src/test/java/net/ziemers/swxercise/lg/testdata/testdatabuilder/user/UserTestDataBuilder.java
@@ -11,7 +11,7 @@ public class UserTestDataBuilder extends AbstractTestDataBuilder&lt;User&gt; { @@ -11,7 +11,7 @@ public class UserTestDataBuilder extends AbstractTestDataBuilder&lt;User&gt; {
11 11
12 private String firstname = "Hein"; 12 private String firstname = "Hein";
13 13
14 - private String lastname = "Bloed"; 14 + private String lastname = "Blöd";
15 15
16 private Profile profile; 16 private Profile profile;
17 17
@@ -22,6 +22,10 @@ public class UserTestDataBuilder extends AbstractTestDataBuilder&lt;User&gt; { @@ -22,6 +22,10 @@ public class UserTestDataBuilder extends AbstractTestDataBuilder&lt;User&gt; {
22 profile = new ProfileTestDataBuilder(em).build(); 22 profile = new ProfileTestDataBuilder(em).build();
23 } 23 }
24 24
  25 + public UserTestDataBuilder() {
  26 + this(null);
  27 + }
  28 +
25 @Override 29 @Override
26 public User build() { 30 public User build() {
27 return new User(firstname, lastname) 31 return new User(firstname, lastname)
src/test/java/net/ziemers/swxercise/lg/user/service/UserServiceTest.java
@@ -2,9 +2,14 @@ package net.ziemers.swxercise.lg.user.service; @@ -2,9 +2,14 @@ package net.ziemers.swxercise.lg.user.service;
2 2
3 import javax.inject.Inject; 3 import javax.inject.Inject;
4 4
  5 +import net.ziemers.swxercise.db.dao.user.UserDao;
5 import net.ziemers.swxercise.db.utils.JpaTestUtils; 6 import net.ziemers.swxercise.db.utils.JpaTestUtils;
  7 +import net.ziemers.swxercise.lg.model.user.User;
  8 +import net.ziemers.swxercise.lg.testdata.testdatabuilder.user.UserDtoTestDataBuilder;
6 import net.ziemers.swxercise.lg.user.dto.UserDto; 9 import net.ziemers.swxercise.lg.user.dto.UserDto;
7 10
  11 +import static org.junit.Assert.*;
  12 +
8 import org.jglue.cdiunit.CdiRunner; 13 import org.jglue.cdiunit.CdiRunner;
9 import org.junit.Before; 14 import org.junit.Before;
10 import org.junit.Test; 15 import org.junit.Test;
@@ -22,13 +27,20 @@ import org.junit.runner.RunWith; @@ -22,13 +27,20 @@ import org.junit.runner.RunWith;
22 @RunWith(CdiRunner.class) 27 @RunWith(CdiRunner.class)
23 public class UserServiceTest extends JpaTestUtils { 28 public class UserServiceTest extends JpaTestUtils {
24 29
  30 + private static String USERNAME_TEST = "username_test";
  31 +
25 private static boolean dbInitialized; 32 private static boolean dbInitialized;
26 33
  34 + private UserDto userDto;
  35 +
  36 + @Inject
  37 + private UserDao userDao;
  38 +
27 @Inject 39 @Inject
28 private UserService underTest; 40 private UserService underTest;
29 41
30 @Before 42 @Before
31 - public void setup() throws Exception { 43 + public void setUp() throws Exception {
32 if (!dbInitialized) { 44 if (!dbInitialized) {
33 cleanDb(); 45 cleanDb();
34 //initDbWith("UserService.xml"); 46 //initDbWith("UserService.xml");
@@ -37,18 +49,54 @@ public class UserServiceTest extends JpaTestUtils { @@ -37,18 +49,54 @@ public class UserServiceTest extends JpaTestUtils {
37 } 49 }
38 50
39 @Test 51 @Test
40 - public void testService() {  
41 - txBegin(); 52 + public void testCreateUserReturnsSuccess() {
42 53
43 - final UserDto dto = new UserDto()  
44 - .withFirstname("Hein")  
45 - .withLastname("Blöd")  
46 - .withUsername("tziemer")  
47 - .withPassword("secret"); 54 + given()
  55 + .userDto();
48 56
49 - underTest.createUser(dto); 57 + when()
  58 + .createUser();
50 59
  60 + then()
  61 + .assertCreateSuccess();
  62 + }
  63 +
  64 + // given
  65 +
  66 + private UserServiceTest given() {
  67 + return this;
  68 + }
  69 +
  70 + private UserServiceTest userDto() {
  71 + userDto = new UserDtoTestDataBuilder()
  72 + .withUsername(USERNAME_TEST)
  73 + .build();
  74 + return this;
  75 + }
  76 +
  77 + // when
  78 +
  79 + private UserServiceTest when() {
  80 + return this;
  81 + }
  82 +
  83 + private UserServiceTest createUser() {
  84 + txBegin();
  85 + underTest.createUser(userDto);
51 txCommit(); 86 txCommit();
  87 +
  88 + return this;
  89 + }
  90 +
  91 + // then
  92 +
  93 + private UserServiceTest then() {
  94 + return this;
  95 + }
  96 +
  97 + private void assertCreateSuccess() {
  98 + final User user = userDao.findByUsername(USERNAME_TEST);
  99 + assertNotNull(user);
52 } 100 }
53 101
54 } 102 }
src/test/java/net/ziemers/swxercise/ui/UserViewControllerTest.java
1 package net.ziemers.swxercise.ui; 1 package net.ziemers.swxercise.ui;
2 2
3 import net.ziemers.swxercise.lg.model.user.User; 3 import net.ziemers.swxercise.lg.model.user.User;
  4 +import net.ziemers.swxercise.lg.testdata.testdatabuilder.user.UserDtoTestDataBuilder;
  5 +import net.ziemers.swxercise.lg.testdata.testdatabuilder.user.UserTestDataBuilder;
4 import net.ziemers.swxercise.lg.user.dto.UserDto; 6 import net.ziemers.swxercise.lg.user.dto.UserDto;
5 import net.ziemers.swxercise.lg.user.service.UserService; 7 import net.ziemers.swxercise.lg.user.service.UserService;
6 import org.junit.Test; 8 import org.junit.Test;
@@ -101,15 +103,12 @@ public class UserViewControllerTest { @@ -101,15 +103,12 @@ public class UserViewControllerTest {
101 } 103 }
102 104
103 private UserViewControllerTest userDto() { 105 private UserViewControllerTest userDto() {
104 - userDto = new UserDto()  
105 - .withUsername("hbloed")  
106 - .withPassword("secret");  
107 - 106 + userDto = new UserDtoTestDataBuilder().build();
108 return this; 107 return this;
109 } 108 }
110 109
111 private UserViewControllerTest user() { 110 private UserViewControllerTest user() {
112 - user = new User("Hein", "Blöd"); 111 + user = new UserTestDataBuilder().build();
113 return this; 112 return this;
114 } 113 }
115 114