Commit 522685dee8d69dd11fb23dc701c69670501f4837
1 parent
d80fc452
test: CDI-Runner eingebaut und refactoring betrieben
Showing
5 changed files
with
39 additions
and
7 deletions
README.md
| ... | ... | @@ -31,12 +31,16 @@ Dann kann/muss das Kennwort geändert werden, beispielsweise in 'root': |
| 31 | 31 | |
| 32 | 32 | > ALTER USER 'root'@'localhost' IDENTIFIED BY ‘root‘; |
| 33 | 33 | |
| 34 | -Dann muss eine projektspezifische Datenbank erstellt und mit den nötigen Rechten versehen werden: | |
| 34 | +Dann müssen die beiden projektspezifischen Datenbanken (für Test und "Produktion") erstellt und mit den nötigen Rechten versehen werden: | |
| 35 | 35 | |
| 36 | 36 | > create database swxercise; |
| 37 | 37 | |
| 38 | +> create database swxercise_test; | |
| 39 | + | |
| 38 | 40 | > grant all privileges on swxercise.* to 'root'@'localhost' identified by 'root'; |
| 39 | 41 | |
| 42 | +> grant all privileges on swxercise_test.* to 'root'@'localhost' identified by 'root'; | |
| 43 | + | |
| 40 | 44 | ## MySQL-Datenbank im WildFly einbinden |
| 41 | 45 | |
| 42 | 46 | Zunächst muss der JBoss WildFly Application Server installiert werden. Dies ist betriebssystemspezifisch. | ... | ... |
pom.xml
| ... | ... | @@ -19,6 +19,7 @@ |
| 19 | 19 | <mockito-version>2.7.22</mockito-version> |
| 20 | 20 | <dbunit-version>2.5.1</dbunit-version> |
| 21 | 21 | <slf4j-version>1.7.25</slf4j-version> |
| 22 | + <jglue-version>3.1.4</jglue-version> | |
| 22 | 23 | <jackson-version>2.8.8.1</jackson-version> |
| 23 | 24 | |
| 24 | 25 | <!-- Substituiert die Platzhalter in der "persistence.xml" --> |
| ... | ... | @@ -67,8 +68,7 @@ |
| 67 | 68 | <groupId>org.dbunit</groupId> |
| 68 | 69 | <artifactId>dbunit</artifactId> |
| 69 | 70 | <version>${dbunit-version}</version> |
| 70 | - <!--scope>test</scope--> | |
| 71 | - <!-- TODO Wie koennen wir es erreichen, dass dies nur noch im Testkontext abhängig ist? --> | |
| 71 | + <scope>test</scope> | |
| 72 | 72 | </dependency> |
| 73 | 73 | <dependency> |
| 74 | 74 | <groupId>org.hibernate</groupId> |
| ... | ... | @@ -80,6 +80,13 @@ |
| 80 | 80 | <artifactId>mysql-connector-java</artifactId> |
| 81 | 81 | <version>${mysql-version}</version> |
| 82 | 82 | </dependency> |
| 83 | + <dependency> | |
| 84 | + <groupId>org.jglue.cdi-unit</groupId> | |
| 85 | + <artifactId>cdi-unit</artifactId> | |
| 86 | + <version>${jglue-version}</version> | |
| 87 | + <scope>test</scope> | |
| 88 | + </dependency> | |
| 89 | + <!-- Sorgt für eine schnellere Verwaltung der JSON-Umwandlung im REST-Kontext --> | |
| 83 | 90 | <!-- dependency> |
| 84 | 91 | <groupId>com.fasterxml.jackson.core</groupId> |
| 85 | 92 | <artifactId>jackson-databind</artifactId> | ... | ... |
src/main/java/net/ziemers/swxercise/db/utils/JpaTestUtils.java renamed to src/test/java/net/ziemers/swxercise/db/utils/JpaTestUtils.java
src/test/java/net/ziemers/swxercise/lg/user/service/UserServiceTest.java
| ... | ... | @@ -5,9 +5,21 @@ import javax.inject.Inject; |
| 5 | 5 | import net.ziemers.swxercise.db.utils.JpaTestUtils; |
| 6 | 6 | import net.ziemers.swxercise.lg.user.dto.UserDto; |
| 7 | 7 | |
| 8 | +import org.jglue.cdiunit.CdiRunner; | |
| 8 | 9 | import org.junit.Before; |
| 9 | 10 | import org.junit.Test; |
| 10 | - | |
| 11 | +import org.junit.runner.RunWith; | |
| 12 | + | |
| 13 | +/* | |
| 14 | + * Testing your Java CDI application with CDI-Unit couldn't be easier. | |
| 15 | + * Just specify @RunWith(CdiRunner.class) on your JUnit test class | |
| 16 | + * to enable injection directly into the test class. | |
| 17 | + * | |
| 18 | + * To test classes in isolation we shouldn't be using their dependencies. | |
| 19 | + * Instead we should be using a mock. There are many mocking libraries out | |
| 20 | + * there, however CDI-Unit has extra support for Mockito @Mock annotations. | |
| 21 | + */ | |
| 22 | +@RunWith(CdiRunner.class) | |
| 11 | 23 | public class UserServiceTest extends JpaTestUtils { |
| 12 | 24 | |
| 13 | 25 | private static boolean dbInitialized; | ... | ... |
src/test/java/net/ziemers/swxercise/ui/UserViewControllerTest.java
| ... | ... | @@ -15,9 +15,17 @@ import static org.mockito.Mockito.*; |
| 15 | 15 | @RunWith(MockitoJUnitRunner.class) |
| 16 | 16 | public class UserViewControllerTest { |
| 17 | 17 | |
| 18 | + /* | |
| 19 | + * You have the @InjectMocks annotation which tries to do constructor, | |
| 20 | + * method and field dependency injection based on the type. | |
| 21 | + */ | |
| 18 | 22 | @InjectMocks |
| 19 | 23 | private UserViewController underTest; |
| 20 | 24 | |
| 25 | + /* | |
| 26 | + * Mockito allows to configure the return values of its mocks via a fluent API. | |
| 27 | + * Unspecified method calls return "empty" values | |
| 28 | + */ | |
| 21 | 29 | @Mock |
| 22 | 30 | private UserService userService; |
| 23 | 31 | |
| ... | ... | @@ -112,10 +120,11 @@ public class UserViewControllerTest { |
| 112 | 120 | } |
| 113 | 121 | |
| 114 | 122 | private void loginUser() { |
| 115 | - // wir simulieren den Erfolg des userService (der hier nicht getestet werden soll), | |
| 116 | - // sofern das korrekte userDto an ihn übergeben wurde | |
| 123 | + /* | |
| 124 | + * The when().thenReturn() method chain is used to specify | |
| 125 | + * a return value for a method call with pre-defined parameters. | |
| 126 | + */ | |
| 117 | 127 | when(userService.loginUser(userDto)).thenReturn(true); |
| 118 | - //when(userService.getSessionUser()).thenReturn(user); | |
| 119 | 128 | |
| 120 | 129 | actual = underTest.loginUser(userDto); |
| 121 | 130 | } | ... | ... |