Skip to content
This repository was archived by the owner on Jan 13, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,19 @@
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManagerFactory;

import java.util.List;
import java.util.stream.Stream;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.Matchers.array;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.core.IsNull.notNullValue;

@SpringJUnitConfig(RootConfig.class)
@Transactional
public class SpringDataJpaConfigTest {
class SpringDataJpaConfigTest {
@Configuration
static class TestConfig {
@Bean
Expand All @@ -47,35 +49,35 @@ TestDataGenerator dataGenerator() {
private TestDataGenerator dataGenerator;

@Test
public void testTxManagerBeanName() {
void testTxManagerBeanName() {
PlatformTransactionManager transactionManager = applicationContext.getBean(PlatformTransactionManager.class, "transactionManager");

assertThat(transactionManager, notNullValue());
}

@Test
public void testUserRepositoryBeanName() {
void testUserRepositoryBeanName() {
UserRepository userRepository = applicationContext.getBean(UserRepository.class, "userRepository");

assertThat(userRepository, notNullValue());
}

@Test
public void testEntityManagerFactoryBeanName() {
void testEntityManagerFactoryBeanName() {
EntityManagerFactory entityManagerFactory = applicationContext.getBean(EntityManagerFactory.class, "entityManagerFactory");

assertThat(entityManagerFactory, notNullValue());
}

@Test
public void testUserRepositoryIsNotMarkedAsRepository() {
void testUserRepositoryIsNotMarkedAsRepository() {
Repository repository = UserRepository.class.getAnnotation(Repository.class);

assertThat(repository, nullValue());
}

@Test
public void testRootConfigComponentScan() {
void testRootConfigComponentScan() {
ComponentScan componentScan = RootConfig.class.getAnnotation(ComponentScan.class);

String[] basePackages = componentScan.basePackages();
Expand All @@ -87,14 +89,14 @@ public void testRootConfigComponentScan() {
}

@Test
public void testJpaConfigRepositoriesPackage() {
void testJpaConfigRepositoriesPackage() {
EnableJpaRepositories enableJpaRepositories = JpaConfig.class.getAnnotation(EnableJpaRepositories.class);

assertThat(enableJpaRepositories.basePackages(), array(equalTo("com.bobocode.dao")));
}

@Test
public void testSaveUser() {
void testSaveUser() {
User user = dataGenerator.generateUser();

userRepository.save(user);
Expand All @@ -103,7 +105,7 @@ public void testSaveUser() {
}

@Test
public void testFindAll() {
void testFindAll() {
Stream.generate(dataGenerator::generateUser).limit(10).forEach(userRepository::save);

List<User> users = userRepository.findAll();
Expand Down
49 changes: 28 additions & 21 deletions user-service/src/test/java/com/bobocode/UserServiceAppTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,22 @@

import static java.util.stream.Collectors.toList;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.Matchers.array;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.everyItem;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.hasProperty;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.core.IsNull.notNullValue;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

@SpringJUnitConfig(RootConfig.class)
@Transactional
public class UserServiceAppTest {
class UserServiceAppTest {
@Configuration
static class TestConfig {
@Bean
Expand All @@ -61,35 +69,35 @@ TestDataGenerator dataGenerator() {
private EntityManager entityManager;

@Test
public void testTxManagerBeanName() {
void testTxManagerBeanName() {
PlatformTransactionManager transactionManager = applicationContext.getBean(PlatformTransactionManager.class, "transactionManager");

assertThat(transactionManager, notNullValue());
}

@Test
public void testUserRepositoryBeanName() {
void testUserRepositoryBeanName() {
UserRepository userRepository = applicationContext.getBean(UserRepository.class, "userRepository");

assertThat(userRepository, notNullValue());
}

@Test
public void testEntityManagerFactoryBeanName() {
void testEntityManagerFactoryBeanName() {
EntityManagerFactory entityManagerFactory = applicationContext.getBean(EntityManagerFactory.class, "entityManagerFactory");

assertThat(entityManagerFactory, notNullValue());
}

@Test
public void testUserRepositoryIsNotMarkedAsRepository() {
void testUserRepositoryIsNotMarkedAsRepository() {
Repository repository = UserRepository.class.getAnnotation(Repository.class);

assertThat(repository, nullValue());
}

@Test
public void testRootConfigComponentScan() {
void testRootConfigComponentScan() {
ComponentScan componentScan = RootConfig.class.getAnnotation(ComponentScan.class);

String[] basePackages = componentScan.basePackages();
Expand All @@ -101,7 +109,7 @@ public void testRootConfigComponentScan() {
}

@Test
public void testJpaConfigRepositoriesPackage() {
void testJpaConfigRepositoriesPackage() {
EnableJpaRepositories enableJpaRepositories = JpaConfig.class.getAnnotation(EnableJpaRepositories.class);

String[] basePackages = enableJpaRepositories.basePackages();
Expand All @@ -113,9 +121,8 @@ public void testJpaConfigRepositoriesPackage() {
}



@Test
public void testFindUsersByCity() {
void testFindUsersByCity() {
List<User> userList = Stream.generate(dataGenerator::generateUser).limit(10).collect(toList());
userRepository.saveAll(userList);
entityManager.flush();
Expand All @@ -129,15 +136,15 @@ public void testFindUsersByCity() {
}

@Test
public void testFindUsersByCityIsReadOnly() throws NoSuchMethodException {
void testFindUsersByCityIsReadOnly() throws NoSuchMethodException {
Transactional transactional = UserService.class.getMethod("findByCity", String.class)
.getAnnotation(Transactional.class);

assertThat(transactional.readOnly(), is(true));
}

@Test
public void testGetUserByEmail() {
void testGetUserByEmail() {
User generatedUser = dataGenerator.generateUser();
userRepository.save(generatedUser);
entityManager.flush();
Expand All @@ -149,7 +156,7 @@ public void testGetUserByEmail() {
}

@Test
public void testGetUserByEmailFetchesRoles() {
void testGetUserByEmailFetchesRoles() {
User generatedUser = dataGenerator.generateUser();
userRepository.save(generatedUser);
entityManager.flush();
Expand All @@ -161,7 +168,7 @@ public void testGetUserByEmailFetchesRoles() {
}

@Test
public void testGetUserByEmailForUserWithoutRoles() {
void testGetUserByEmailForUserWithoutRoles() {
User generatedUserWithoutRoles = dataGenerator.generateUserWithoutRoles();
userRepository.save(generatedUserWithoutRoles);
entityManager.flush();
Expand All @@ -173,7 +180,7 @@ public void testGetUserByEmailForUserWithoutRoles() {
}

@Test
public void testGetUserByEmailFetchesAddress() {
void testGetUserByEmailFetchesAddress() {
User generatedUser = dataGenerator.generateUser();
userRepository.save(generatedUser);
entityManager.flush();
Expand All @@ -185,7 +192,7 @@ public void testGetUserByEmailFetchesAddress() {
}

@Test
public void testGetUserByEmailForUserWithoutAddress() {
void testGetUserByEmailForUserWithoutAddress() {
User generatedUserWithoutAddress = dataGenerator.generateUser();
generatedUserWithoutAddress.setAddress(null);
userRepository.save(generatedUserWithoutAddress);
Expand All @@ -198,15 +205,15 @@ public void testGetUserByEmailForUserWithoutAddress() {
}

@Test
public void testGetByEmailIsReadOnly() throws NoSuchMethodException {
void testGetByEmailIsReadOnly() throws NoSuchMethodException {
Transactional transactional = UserService.class.getMethod("getByEmail", String.class)
.getAnnotation(Transactional.class);

assertThat(transactional.readOnly(), is(true));
}

@Test
public void testGetUserByNotExistingEmail() {
void testGetUserByNotExistingEmail() {
try {
User foundUser = userService.getByEmail("xxx@gmail.com");
fail("Exception should be thrown");
Expand All @@ -217,7 +224,7 @@ public void testGetUserByNotExistingEmail() {
}

@Test
public void testAddRoleToAllUsers() {
void testAddRoleToAllUsers() {
List<User> userList = Stream.generate(dataGenerator::generateUser).limit(10).collect(toList());
userRepository.saveAll(userList);

Expand All @@ -232,7 +239,7 @@ public void testAddRoleToAllUsers() {
}

@Test
public void testAddRoleToAllUsersIncludingUsersWithoutRoles() {
void testAddRoleToAllUsersIncludingUsersWithoutRoles() {
List<User> userList = Stream.generate(dataGenerator::generateUser).limit(10).collect(toList());
User userWithoutRoles = dataGenerator.generateUserWithoutRoles();
userList.add(userWithoutRoles);
Expand All @@ -250,7 +257,7 @@ public void testAddRoleToAllUsersIncludingUsersWithoutRoles() {
}

@Test
public void testAddRoleToAllUsersDoesntAddDuplicates() {
void testAddRoleToAllUsersDoesntAddDuplicates() {
User user = dataGenerator.generateUser(RoleType.USER, RoleType.OPERATOR);
userRepository.save(user);

Expand Down