Use @Sql instead of persit()
All checks were successful
Java CI with Maven / build (push) Successful in 45s

This commit is contained in:
Sebastian Böckelmann 2023-11-10 20:58:10 +01:00
parent 21015ff06d
commit 2b9e821cf2
3 changed files with 71 additions and 81 deletions

View File

@ -4,11 +4,10 @@
<option name="autoReloadType" value="SELECTIVE" /> <option name="autoReloadType" value="SELECTIVE" />
</component> </component>
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="3a869f59-290a-4ab2-b036-a878ce801bc4" name="Changes" comment="Test TaskRepository"> <list default="true" id="3a869f59-290a-4ab2-b036-a878ce801bc4" name="Changes" comment="Initialize Testing of Testservice">
<change afterPath="$PROJECT_DIR$/src/test/java/core/taskgroups/TaskgroupServiceTest.java" afterDir="false" /> <change afterPath="$PROJECT_DIR$/src/test/resources/userRepisotoryTestEntries.sql" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pom.xml" beforeDir="false" afterPath="$PROJECT_DIR$/pom.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/src/test/java/core/users/UserRepositoryTests.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/test/java/core/users/UserRepositoryTests.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/test/resources/application.properties" beforeDir="false" afterPath="$PROJECT_DIR$/src/test/resources/application.properties" afterDir="false" />
</list> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" /> <option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -87,81 +86,82 @@
<recent name="$PROJECT_DIR$/src/test/java/core/taskgroups" /> <recent name="$PROJECT_DIR$/src/test/java/core/taskgroups" />
</key> </key>
</component> </component>
<component name="RunManager" selected="JUnit.TaskgroupServiceTest.getTaskgroupByIDAndUsername"> <component name="RunManager" selected="JUnit.UserRepositoryTests.test_countUsers">
<configuration name="TaskRepositoryTest.findAllActive" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true"> <configuration name="UserRepositoryTests.test_countUsers" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<module name="demo" /> <module name="demo" />
<extension name="coverage"> <extension name="coverage">
<pattern> <pattern>
<option name="PATTERN" value="core.tasks.*" /> <option name="PATTERN" value="core.users.*" />
<option name="ENABLED" value="true" /> <option name="ENABLED" value="true" />
</pattern> </pattern>
</extension> </extension>
<option name="PACKAGE_NAME" value="core.tasks" /> <option name="PACKAGE_NAME" value="core.users" />
<option name="MAIN_CLASS_NAME" value="core.tasks.TaskRepositoryTest" /> <option name="MAIN_CLASS_NAME" value="core.users.UserRepositoryTests" />
<option name="METHOD_NAME" value="findAllActive" /> <option name="METHOD_NAME" value="test_countUsers" />
<option name="TEST_OBJECT" value="method" /> <option name="TEST_OBJECT" value="method" />
<method v="2"> <method v="2">
<option name="Make" enabled="true" /> <option name="Make" enabled="true" />
</method> </method>
</configuration> </configuration>
<configuration name="TaskRepositoryTest.findAllOverdue" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true"> <configuration name="UserRepositoryTests.test_deleteUserByName" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<module name="demo" /> <module name="demo" />
<extension name="coverage"> <extension name="coverage">
<pattern> <pattern>
<option name="PATTERN" value="core.tasks.*" /> <option name="PATTERN" value="core.users.*" />
<option name="ENABLED" value="true" /> <option name="ENABLED" value="true" />
</pattern> </pattern>
</extension> </extension>
<option name="PACKAGE_NAME" value="core.tasks" /> <option name="PACKAGE_NAME" value="core.users" />
<option name="MAIN_CLASS_NAME" value="core.tasks.TaskRepositoryTest" /> <option name="MAIN_CLASS_NAME" value="core.users.UserRepositoryTests" />
<option name="METHOD_NAME" value="findAllOverdue" /> <option name="METHOD_NAME" value="test_deleteUserByName" />
<option name="TEST_OBJECT" value="method" /> <option name="TEST_OBJECT" value="method" />
<method v="2"> <method v="2">
<option name="Make" enabled="true" /> <option name="Make" enabled="true" />
</method> </method>
</configuration> </configuration>
<configuration name="TaskRepositoryTest.findAllUpcoming" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true"> <configuration name="UserRepositoryTests.test_existsByMail" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<module name="demo" /> <module name="demo" />
<extension name="coverage"> <extension name="coverage">
<pattern> <pattern>
<option name="PATTERN" value="core.tasks.*" /> <option name="PATTERN" value="core.users.*" />
<option name="ENABLED" value="true" /> <option name="ENABLED" value="true" />
</pattern> </pattern>
</extension> </extension>
<option name="PACKAGE_NAME" value="core.tasks" /> <option name="PACKAGE_NAME" value="core.users" />
<option name="MAIN_CLASS_NAME" value="core.tasks.TaskRepositoryTest" /> <option name="MAIN_CLASS_NAME" value="core.users.UserRepositoryTests" />
<option name="METHOD_NAME" value="findAllUpcoming" /> <option name="METHOD_NAME" value="test_existsByMail" />
<option name="TEST_OBJECT" value="method" /> <option name="TEST_OBJECT" value="method" />
<method v="2"> <method v="2">
<option name="Make" enabled="true" /> <option name="Make" enabled="true" />
</method> </method>
</configuration> </configuration>
<configuration name="TaskgroupServiceTest" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true"> <configuration name="UserRepositoryTests.test_existsByUsername" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<module name="demo" /> <module name="demo" />
<extension name="coverage"> <extension name="coverage">
<pattern> <pattern>
<option name="PATTERN" value="core.taskgroups.*" /> <option name="PATTERN" value="core.users.*" />
<option name="ENABLED" value="true" /> <option name="ENABLED" value="true" />
</pattern> </pattern>
</extension> </extension>
<option name="PACKAGE_NAME" value="core.taskgroups" /> <option name="PACKAGE_NAME" value="core.users" />
<option name="MAIN_CLASS_NAME" value="core.taskgroups.TaskgroupServiceTest" /> <option name="MAIN_CLASS_NAME" value="core.users.UserRepositoryTests" />
<option name="TEST_OBJECT" value="class" /> <option name="METHOD_NAME" value="test_existsByUsername" />
<option name="TEST_OBJECT" value="method" />
<method v="2"> <method v="2">
<option name="Make" enabled="true" /> <option name="Make" enabled="true" />
</method> </method>
</configuration> </configuration>
<configuration name="TaskgroupServiceTest.getTaskgroupByIDAndUsername" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true"> <configuration name="UserRepositoryTests.test_findByUsername" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<module name="demo" /> <module name="demo" />
<extension name="coverage"> <extension name="coverage">
<pattern> <pattern>
<option name="PATTERN" value="core.taskgroups.*" /> <option name="PATTERN" value="core.users.*" />
<option name="ENABLED" value="true" /> <option name="ENABLED" value="true" />
</pattern> </pattern>
</extension> </extension>
<option name="PACKAGE_NAME" value="core.taskgroups" /> <option name="PACKAGE_NAME" value="core.users" />
<option name="MAIN_CLASS_NAME" value="core.taskgroups.TaskgroupServiceTest" /> <option name="MAIN_CLASS_NAME" value="core.users.UserRepositoryTests" />
<option name="METHOD_NAME" value="getTaskgroupByIDAndUsername" /> <option name="METHOD_NAME" value="test_findByUsername" />
<option name="TEST_OBJECT" value="method" /> <option name="TEST_OBJECT" value="method" />
<method v="2"> <method v="2">
<option name="Make" enabled="true" /> <option name="Make" enabled="true" />
@ -176,11 +176,11 @@
</configuration> </configuration>
<recent_temporary> <recent_temporary>
<list> <list>
<item itemvalue="JUnit.TaskgroupServiceTest.getTaskgroupByIDAndUsername" /> <item itemvalue="JUnit.UserRepositoryTests.test_countUsers" />
<item itemvalue="JUnit.TaskgroupServiceTest" /> <item itemvalue="JUnit.UserRepositoryTests.test_deleteUserByName" />
<item itemvalue="JUnit.TaskRepositoryTest.findAllActive" /> <item itemvalue="JUnit.UserRepositoryTests.test_existsByUsername" />
<item itemvalue="JUnit.TaskRepositoryTest.findAllUpcoming" /> <item itemvalue="JUnit.UserRepositoryTests.test_existsByMail" />
<item itemvalue="JUnit.TaskRepositoryTest.findAllOverdue" /> <item itemvalue="JUnit.UserRepositoryTests.test_findByUsername" />
</list> </list>
</recent_temporary> </recent_temporary>
</component> </component>
@ -217,7 +217,7 @@
<workItem from="1699460935890" duration="5000" /> <workItem from="1699460935890" duration="5000" />
<workItem from="1699466564154" duration="6646000" /> <workItem from="1699466564154" duration="6646000" />
<workItem from="1699473376129" duration="1423000" /> <workItem from="1699473376129" duration="1423000" />
<workItem from="1699639316405" duration="5468000" /> <workItem from="1699639316405" duration="6148000" />
</task> </task>
<task id="LOCAL-00001" summary="Structure Taskgroups in Hierarchies"> <task id="LOCAL-00001" summary="Structure Taskgroups in Hierarchies">
<option name="closed" value="true" /> <option name="closed" value="true" />
@ -435,14 +435,21 @@
<option name="project" value="LOCAL" /> <option name="project" value="LOCAL" />
<updated>1699641346158</updated> <updated>1699641346158</updated>
</task> </task>
<option name="localTasksCounter" value="28" /> <task id="LOCAL-00028" summary="Initialize Testing of Testservice">
<option name="closed" value="true" />
<created>1699645467388</created>
<option name="number" value="00028" />
<option name="presentableId" value="LOCAL-00028" />
<option name="project" value="LOCAL" />
<updated>1699645467388</updated>
</task>
<option name="localTasksCounter" value="29" />
<servers /> <servers />
</component> </component>
<component name="TypeScriptGeneratedFilesManager"> <component name="TypeScriptGeneratedFilesManager">
<option name="version" value="3" /> <option name="version" value="3" />
</component> </component>
<component name="VcsManagerConfiguration"> <component name="VcsManagerConfiguration">
<MESSAGE value="Implementing edit route for tasks" />
<MESSAGE value="Delete and clear Tasks" /> <MESSAGE value="Delete and clear Tasks" />
<MESSAGE value="Delete and clear Tasks (Frontend)" /> <MESSAGE value="Delete and clear Tasks (Frontend)" />
<MESSAGE value="Define Entity BasicTaskSchedule and Taskcontroller" /> <MESSAGE value="Define Entity BasicTaskSchedule and Taskcontroller" />
@ -467,7 +474,8 @@
<MESSAGE value="TaskgroupRepsitoryTest (+Delete)" /> <MESSAGE value="TaskgroupRepsitoryTest (+Delete)" />
<MESSAGE value="FindAlltasksByUser Test" /> <MESSAGE value="FindAlltasksByUser Test" />
<MESSAGE value="Test TaskRepository" /> <MESSAGE value="Test TaskRepository" />
<option name="LAST_COMMIT_MESSAGE" value="Test TaskRepository" /> <MESSAGE value="Initialize Testing of Testservice" />
<option name="LAST_COMMIT_MESSAGE" value="Initialize Testing of Testservice" />
</component> </component>
<component name="XDebuggerManager"> <component name="XDebuggerManager">
<breakpoint-manager> <breakpoint-manager>
@ -497,11 +505,6 @@
<line>57</line> <line>57</line>
<option name="timeStamp" value="12" /> <option name="timeStamp" value="12" />
</line-breakpoint> </line-breakpoint>
<line-breakpoint enabled="true" type="java-line">
<url>file://$PROJECT_DIR$/src/test/java/core/taskgroups/TaskgroupServiceTest.java</url>
<line>50</line>
<option name="timeStamp" value="21" />
</line-breakpoint>
</breakpoints> </breakpoints>
</breakpoint-manager> </breakpoint-manager>
</component> </component>

View File

@ -8,6 +8,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager; import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager;
import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.jdbc.Sql;
import org.springframework.test.context.junit.jupiter.SpringExtension; import org.springframework.test.context.junit.jupiter.SpringExtension;
import java.util.Optional; import java.util.Optional;
@ -16,7 +17,6 @@ import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
@DataJpaTest @DataJpaTest
@DirtiesContext
@ExtendWith(SpringExtension.class) @ExtendWith(SpringExtension.class)
public class UserRepositoryTests { public class UserRepositoryTests {
@ -27,76 +27,61 @@ public class UserRepositoryTests {
private TestEntityManager testEntityManager; private TestEntityManager testEntityManager;
@Test @Test
@Sql("classpath:userRepisotoryTestEntries.sql")
void test_findByUsername() { void test_findByUsername() {
//Situation 1: No user is present in the database, so no user should be found by a given username //Situation 1: User with name "FawKes100" is present
assertTrue(userRepository.findByUsername("FawKes100").isEmpty()); User referenceUser = testEntityManager.find(User.class, 1L);
Optional<User> findResult = userRepository.findByUsername(referenceUser.getUsername());
//persist a user to the database, so a user is present
testEntityManager.persist(new User("FawKes100", "mail@fawkes100.de", "123456"));
//Situation 2: User with name "FawKes100" is present
Optional<User> findResult = userRepository.findByUsername("FawKes100");
assertTrue(findResult.isPresent()); assertTrue(findResult.isPresent());
assertEquals(new User("FawKes100", "mail@fawkes100.de", "123456"),findResult.get()); assertEquals(referenceUser,findResult.get());
//Situation 3: No user with the required name is present //Situation 2: No user with the required name is present
findResult = userRepository.findByUsername("fawkes1001"); findResult = userRepository.findByUsername("fawkes1001");
assertFalse(findResult.isPresent()); assertFalse(findResult.isPresent());
} }
@Test @Test
@Sql("classpath:userRepisotoryTestEntries.sql")
void test_existsByMail() { void test_existsByMail() {
User referenceUser = testEntityManager.find(User.class, 1L);
//Situation 1: invalid email format should not matter //Situation 1: invalid email format should not matter
assertFalse(userRepository.existsByEmail("FawKes100")); assertFalse(userRepository.existsByEmail("FawKes100"));
//Situation 2: No user exists with such a mail //Situation 2: No user exists with such a mail
assertFalse(userRepository.existsByEmail("mail@fawkes100.de")); assertFalse(userRepository.existsByEmail("mail@fawkes100.de"));
//persist a user to the database, so a user is present
testEntityManager.persist(new User("FawKes100", "mail@fawkes100.de", "123456"));
//Situation 3: User with this mail exists //Situation 3: User with this mail exists
assertTrue(userRepository.existsByEmail("mail@fawkes100.de")); assertTrue(userRepository.existsByEmail(referenceUser.getEmail()));
//Situation 4: No user exists with such a mail (non-empty db)
assertFalse(userRepository.existsByEmail("mail2@fawkes100.de"));
} }
@Test @Test
@Sql("classpath:userRepisotoryTestEntries.sql")
void test_existsByUsername() { void test_existsByUsername() {
//Situation 1: Empty database User referenceUser = testEntityManager.find(User.class, 1L);
//Situation 1: No such user
assertFalse(userRepository.existsByUsername("FawKes100")); assertFalse(userRepository.existsByUsername("FawKes100"));
//persist a user to the database, so a user is present
testEntityManager.persist(new User("FawKes100", "mail@fawkes100.de", "123456"));
//Situation 3: User with this name exists //Situation 3: User with this name exists
assertTrue(userRepository.existsByUsername("FawKes100")); assertTrue(userRepository.existsByUsername(referenceUser.getUsername()));
//Situation 4: No user exists with such a mail (non-empty db)
assertFalse(userRepository.existsByUsername("FawKes101"));
} }
@Test @Test
@Sql("classpath:userRepisotoryTestEntries.sql")
void test_deleteUserByName() { void test_deleteUserByName() {
//persist a user to the database, so a user is present User referenceUser1 = testEntityManager.find(User.class, 1L);
User user = testEntityManager.persist(new User("FawKes100", "mail@fawkes100.de", "123456")); User referenceUser2 = testEntityManager.find(User.class, 2L);
User user1 = testEntityManager.persist(new User("FawKes101", "mail@fawkes101.de", "123456"));
userRepository.deleteByUsername("FawKes100"); userRepository.deleteByUsername(referenceUser2.getUsername());
assertThat(testEntityManager.find(User.class, user.getId())).isNull(); assertThat(testEntityManager.find(User.class, referenceUser2.getId())).isNull();
assertThat(testEntityManager.find(User.class, user1.getId())).isNotNull(); assertThat(testEntityManager.find(User.class, referenceUser1.getId())).isNotNull();
} }
@Test @Test
@Sql("classpath:userRepisotoryTestEntries.sql")
void test_countUsers() { void test_countUsers() {
assertEquals(0, userRepository.countUsers()); assertEquals(2, userRepository.countUsers());
testEntityManager.persist(new User("FawKes100", "mail@fawkes100.de", "123456"));
assertEquals(1, userRepository.countUsers());
} }
} }

View File

@ -0,0 +1,2 @@
INSERT INTO users (id, username, email, password) VALUES (1, 'Testuser1', 'test1@test.com', '123456');
INSERT INTO users (id, username, email, password) VALUES (2, 'Testuser2', 'test2@test.com', '123456');