Commit 3806707c2f480c0bcdb20a035fcacbee874d1ff2
1 parent
1d77dd1a
test: Stubs für RoleViewController- und RoleService-Tests angelegt
Showing
3 changed files
with
152 additions
and
22 deletions
src/test/java/net/ziemers/swxercise/db/utils/JpaTestUtils.java
| @@ -74,6 +74,7 @@ public class JpaTestUtils { | @@ -74,6 +74,7 @@ public class JpaTestUtils { | ||
| 74 | return em; | 74 | return em; |
| 75 | } | 75 | } |
| 76 | 76 | ||
| 77 | + @SuppressWarnings("unused") | ||
| 77 | protected void clearEm() { | 78 | protected void clearEm() { |
| 78 | em = null; | 79 | em = null; |
| 79 | } | 80 | } |
| @@ -118,16 +119,17 @@ public class JpaTestUtils { | @@ -118,16 +119,17 @@ public class JpaTestUtils { | ||
| 118 | return emf; | 119 | return emf; |
| 119 | } | 120 | } |
| 120 | 121 | ||
| 121 | - public void txBegin() { | 122 | + protected void txBegin() { |
| 122 | tx = getEm().getTransaction(); | 123 | tx = getEm().getTransaction(); |
| 123 | tx.begin(); | 124 | tx.begin(); |
| 124 | } | 125 | } |
| 125 | 126 | ||
| 126 | - public void txCommit() { | 127 | + protected void txCommit() { |
| 127 | tx.commit(); | 128 | tx.commit(); |
| 128 | } | 129 | } |
| 129 | 130 | ||
| 130 | - public void txRollback() { | 131 | + @SuppressWarnings("unused") |
| 132 | + protected void txRollback() { | ||
| 131 | tx.rollback(); | 133 | tx.rollback(); |
| 132 | } | 134 | } |
| 133 | 135 | ||
| @@ -151,7 +153,8 @@ public class JpaTestUtils { | @@ -151,7 +153,8 @@ public class JpaTestUtils { | ||
| 151 | resourceAsStream = JpaTestUtils.class.getClassLoader().getResourceAsStream(NET_ZIEMERS_SWXERCISE_PKG + resourceName); | 153 | resourceAsStream = JpaTestUtils.class.getClassLoader().getResourceAsStream(NET_ZIEMERS_SWXERCISE_PKG + resourceName); |
| 152 | dataSet = flatXmlDataSetBuilder.build(resourceAsStream); | 154 | dataSet = flatXmlDataSetBuilder.build(resourceAsStream); |
| 153 | } catch (Exception e) { | 155 | } catch (Exception e) { |
| 154 | - throw e; | 156 | + logger.warn("Exception while reading test data definitions file named \"{}\". If your tests fail, check existence of file!", resourceName); |
| 157 | + // throw e; | ||
| 155 | } finally { | 158 | } finally { |
| 156 | if (dtdStream != null) { | 159 | if (dtdStream != null) { |
| 157 | dtdStream.close(); | 160 | dtdStream.close(); |
| @@ -171,20 +174,22 @@ public class JpaTestUtils { | @@ -171,20 +174,22 @@ public class JpaTestUtils { | ||
| 171 | * @throws Exception wenn dabei etwas schiefläuft | 174 | * @throws Exception wenn dabei etwas schiefläuft |
| 172 | */ | 175 | */ |
| 173 | protected void initDbWith(String resourceName) throws Exception { | 176 | protected void initDbWith(String resourceName) throws Exception { |
| 174 | - IDataSet dataset = readDataset(resourceName); | ||
| 175 | - try { | ||
| 176 | - SessionImpl delegate = (SessionImpl) this.getEm().getDelegate(); | ||
| 177 | - DatabaseConnection connection = new DatabaseConnection(delegate.connection()); | ||
| 178 | - IMetadataHandler mysqlHandler = new MySqlMetadataHandler(); | ||
| 179 | - IDataTypeFactory mySqlDataTypeFactory = new MySqlDataTypeFactory(); | ||
| 180 | - connection.getConfig().setProperty(DatabaseConfig.PROPERTY_METADATA_HANDLER, mysqlHandler); | ||
| 181 | - connection.getConfig().setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, mySqlDataTypeFactory); | ||
| 182 | - DatabaseOperation.INSERT.execute(connection, dataset); | ||
| 183 | - } catch (DatabaseUnitException qe) { | ||
| 184 | - logger.warn("Database entries already inserted. If your tests fail, check your sql inserts for constraint violations!"); | ||
| 185 | - logger.warn(qe.getMessage()); | ||
| 186 | - logger.warn(qe.getMessage(), qe); | ||
| 187 | - throw qe; | 177 | + final IDataSet dataset = readDataset(resourceName); |
| 178 | + if (dataset != null) { | ||
| 179 | + try { | ||
| 180 | + SessionImpl delegate = (SessionImpl) this.getEm().getDelegate(); | ||
| 181 | + DatabaseConnection connection = new DatabaseConnection(delegate.connection()); | ||
| 182 | + IMetadataHandler mysqlHandler = new MySqlMetadataHandler(); | ||
| 183 | + IDataTypeFactory mySqlDataTypeFactory = new MySqlDataTypeFactory(); | ||
| 184 | + connection.getConfig().setProperty(DatabaseConfig.PROPERTY_METADATA_HANDLER, mysqlHandler); | ||
| 185 | + connection.getConfig().setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, mySqlDataTypeFactory); | ||
| 186 | + DatabaseOperation.INSERT.execute(connection, dataset); | ||
| 187 | + } catch (DatabaseUnitException qe) { | ||
| 188 | + logger.warn("Database entries already inserted. If your tests fail, check your sql inserts for constraint violations!"); | ||
| 189 | + logger.warn(qe.getMessage()); | ||
| 190 | + logger.warn(qe.getMessage(), qe); | ||
| 191 | + throw qe; | ||
| 192 | + } | ||
| 188 | } | 193 | } |
| 189 | } | 194 | } |
| 190 | 195 | ||
| @@ -193,7 +198,7 @@ public class JpaTestUtils { | @@ -193,7 +198,7 @@ public class JpaTestUtils { | ||
| 193 | * | 198 | * |
| 194 | * @throws Exception Wirft eventuelle SQL- und I/O-Exceptions | 199 | * @throws Exception Wirft eventuelle SQL- und I/O-Exceptions |
| 195 | */ | 200 | */ |
| 196 | - public void cleanDb() throws Exception { | 201 | + protected void cleanDb() throws Exception { |
| 197 | String schema = getCurrentSchema(); | 202 | String schema = getCurrentSchema(); |
| 198 | List<String> tables = getTableNamesToBeEmptied(); | 203 | List<String> tables = getTableNamesToBeEmptied(); |
| 199 | getEm().getTransaction().begin(); | 204 | getEm().getTransaction().begin(); |
| @@ -219,7 +224,7 @@ public class JpaTestUtils { | @@ -219,7 +224,7 @@ public class JpaTestUtils { | ||
| 219 | return null; | 224 | return null; |
| 220 | } | 225 | } |
| 221 | 226 | ||
| 222 | - public List<String> getTableNamesToBeEmptied() throws Exception { | 227 | + private List<String> getTableNamesToBeEmptied() throws Exception { |
| 223 | return showTablesNotEmpty(getCurrentSchema()); | 228 | return showTablesNotEmpty(getCurrentSchema()); |
| 224 | } | 229 | } |
| 225 | 230 | ||
| @@ -240,7 +245,7 @@ public class JpaTestUtils { | @@ -240,7 +245,7 @@ public class JpaTestUtils { | ||
| 240 | * | 245 | * |
| 241 | * @return eine Liste der zu ignorierenden Tabellen | 246 | * @return eine Liste der zu ignorierenden Tabellen |
| 242 | */ | 247 | */ |
| 243 | - public List<String> getSkippingTables() { | 248 | + private List<String> getSkippingTables() { |
| 244 | return Arrays.asList( | 249 | return Arrays.asList( |
| 245 | "hibernate_sequence", | 250 | "hibernate_sequence", |
| 246 | "schema_version"); // FlywayDB Metatable | 251 | "schema_version"); // FlywayDB Metatable |
| @@ -255,7 +260,7 @@ public class JpaTestUtils { | @@ -255,7 +260,7 @@ public class JpaTestUtils { | ||
| 255 | return result; | 260 | return result; |
| 256 | } | 261 | } |
| 257 | 262 | ||
| 258 | - public String getTruncateStatement(String schema, String table) { | 263 | + private String getTruncateStatement(String schema, String table) { |
| 259 | return String.format("DELETE FROM %s.%s;", schema, table); | 264 | return String.format("DELETE FROM %s.%s;", schema, table); |
| 260 | } | 265 | } |
| 261 | 266 |
src/test/java/net/ziemers/swxercise/lg/user/service/RoleServiceTest.java
0 → 100644
| 1 | +package net.ziemers.swxercise.lg.user.service; | ||
| 2 | + | ||
| 3 | +import net.ziemers.swxercise.db.dao.user.UserDao; | ||
| 4 | +import net.ziemers.swxercise.db.utils.JpaTestUtils; | ||
| 5 | +import org.jglue.cdiunit.CdiRunner; | ||
| 6 | +import org.junit.Before; | ||
| 7 | +import org.junit.Test; | ||
| 8 | +import org.junit.runner.RunWith; | ||
| 9 | + | ||
| 10 | +import javax.inject.Inject; | ||
| 11 | + | ||
| 12 | +/** | ||
| 13 | + * Testing your Java CDI application with CDI-Unit couldn't be easier. | ||
| 14 | + * Just specify @RunWith(CdiRunner.class) on your JUnit test class | ||
| 15 | + * to enable injection directly into the test class. | ||
| 16 | + * | ||
| 17 | + * To test classes in isolation we shouldn't be using their dependencies. | ||
| 18 | + * Instead we should be using a mock. There are many mocking libraries out | ||
| 19 | + * there, however CDI-Unit has extra support for Mockito @Mock annotations. | ||
| 20 | + * | ||
| 21 | + * Quelle: http://jglue.org/cdi-unit-user-guide/ | ||
| 22 | + */ | ||
| 23 | +@RunWith(CdiRunner.class) | ||
| 24 | +public class RoleServiceTest extends JpaTestUtils { | ||
| 25 | + | ||
| 26 | + private static boolean dbInitialized; | ||
| 27 | + | ||
| 28 | + @Inject | ||
| 29 | + private UserDao userDao; | ||
| 30 | + | ||
| 31 | + @Inject | ||
| 32 | + private UserService underTest; | ||
| 33 | + | ||
| 34 | + @Before | ||
| 35 | + public void setUp() throws Exception { | ||
| 36 | + if (!dbInitialized) { | ||
| 37 | + cleanDb(); | ||
| 38 | + initDbWith("RoleServiceTestData.xml"); | ||
| 39 | + dbInitialized = true; | ||
| 40 | + } | ||
| 41 | + } | ||
| 42 | + | ||
| 43 | + @Test | ||
| 44 | + public void testSomething() { | ||
| 45 | + | ||
| 46 | + given(); | ||
| 47 | + | ||
| 48 | + when(); | ||
| 49 | + | ||
| 50 | + then(); | ||
| 51 | + } | ||
| 52 | + | ||
| 53 | + // given | ||
| 54 | + | ||
| 55 | + private RoleServiceTest given() { | ||
| 56 | + return this; | ||
| 57 | + } | ||
| 58 | + | ||
| 59 | + // when | ||
| 60 | + | ||
| 61 | + private RoleServiceTest when() { | ||
| 62 | + return this; | ||
| 63 | + } | ||
| 64 | + | ||
| 65 | + // then | ||
| 66 | + | ||
| 67 | + private RoleServiceTest then() { | ||
| 68 | + return this; | ||
| 69 | + } | ||
| 70 | + | ||
| 71 | +} |
src/test/java/net/ziemers/swxercise/ui/RoleViewControllerTest.java
0 → 100644
| 1 | +package net.ziemers.swxercise.ui; | ||
| 2 | + | ||
| 3 | +import net.ziemers.swxercise.lg.user.service.RoleService; | ||
| 4 | +import org.junit.Test; | ||
| 5 | +import org.junit.runner.RunWith; | ||
| 6 | +import org.mockito.InjectMocks; | ||
| 7 | +import org.mockito.Mock; | ||
| 8 | +import org.mockito.junit.MockitoJUnitRunner; | ||
| 9 | + | ||
| 10 | +@RunWith(MockitoJUnitRunner.class) | ||
| 11 | +public class RoleViewControllerTest { | ||
| 12 | + | ||
| 13 | + /* | ||
| 14 | + * You have the @InjectMocks annotation which tries to do constructor, | ||
| 15 | + * method and field dependency injection based on the type. | ||
| 16 | + */ | ||
| 17 | + @InjectMocks | ||
| 18 | + private RoleViewController underTest; | ||
| 19 | + | ||
| 20 | + /* | ||
| 21 | + * Mockito allows to configure the return values of its mocks via a fluent API. | ||
| 22 | + * Unspecified method calls return "empty" values | ||
| 23 | + */ | ||
| 24 | + @Mock | ||
| 25 | + private RoleService roleService; | ||
| 26 | + | ||
| 27 | + @Test public void testSomething() { | ||
| 28 | + | ||
| 29 | + given(); | ||
| 30 | + | ||
| 31 | + doing(); | ||
| 32 | + | ||
| 33 | + then(); | ||
| 34 | + } | ||
| 35 | + | ||
| 36 | + // given | ||
| 37 | + | ||
| 38 | + private RoleViewControllerTest given() { | ||
| 39 | + return this; | ||
| 40 | + } | ||
| 41 | + | ||
| 42 | + // doing (when) | ||
| 43 | + | ||
| 44 | + private RoleViewControllerTest doing() { | ||
| 45 | + return this; | ||
| 46 | + } | ||
| 47 | + | ||
| 48 | + // then | ||
| 49 | + | ||
| 50 | + private RoleViewControllerTest then() { | ||
| 51 | + return this; | ||
| 52 | + } | ||
| 53 | + | ||
| 54 | +} |