[Spring](EN) Test Spring Service Using Mockito
Test Spring Service Using Mockito
Environment and Prerequisite
- Java
- Spring
- JUnit5
- SpringExtension.class
Mockito
Mockito
Mockito
: Java test framework which supports mocking@Mock
: Object mocking annotation for test@InjectMocks
: Object mocking annotation with inject all needed objects with@Mock
annotation for testwhen(method_name).thenReturn(return_value);
: When call mocking object’s specific method(method_name
), this method make it to returnreturn_value
Example
build.gradle
- Below build.gradle is personal project setting so it can be different so see the links below for more information
- https://www.baeldung.com/junit-5
- https://www.baeldung.com/junit-5-gradle
- https://www.baeldung.com/spring-boot-testing
plugins {
id 'org.springframework.boot' version '2.5.13'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}
group = 'org.twpower'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '8'
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
maven { url 'https://repo.spring.io/milestone' }
maven { url 'https://repo.spring.io/snapshot' }
}
dependencies {
implementation('org.springframework.boot:spring-boot-starter-data-jpa')
implementation('org.springframework.boot:spring-boot-starter-web')
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
runtimeOnly 'com.h2database:h2'
testImplementation('org.springframework.boot:spring-boot-starter-test')
testImplementation('org.junit.jupiter:junit-jupiter')
testImplementation('org.junit.jupiter:junit-jupiter-api')
testImplementation('org.mockito:mockito-core')
testImplementation('org.mockito:mockito-junit-jupiter')
testImplementation('org.hamcrest:hamcrest')
testRuntimeOnly('org.junit.jupiter:junit-jupiter-engine')
}
test {
useJUnitPlatform()
}
Entity
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Data
@Entity
@Table(uniqueConstraints = {@UniqueConstraint(columnNames = "email")})
public class UserEntity {
@Id
@GeneratedValue(generator = "system-uuid")
@GenericGenerator(name = "system-uuid", strategy = "uuid")
private String id;
@Column(nullable = false)
private String username;
@Column(nullable = false)
private String email;
@Column(nullable = false)
private String password;
}
JPA Repository
@Repository
public interface UserRepository extends JpaRepository<UserEntity, String> {
UserEntity findByEmail(String email);
Boolean existsByEmail(String email);
}
Service
@Slf4j
@Service
public class UserService {
@Autowired
UserRepository userRepository;
public UserEntity create(final UserEntity userEntity){
if (userEntity == null || userEntity.getEmail() == null) {
throw new RuntimeException("Invalid Arguments");
}
final String email = userEntity.getEmail();
if (userRepository.existsByEmail(email)) {
log.warn("Email already exists {}", email);
throw new RuntimeException("Email already exists");
}
return userRepository.save(userEntity);
}
}
Test
@ExtendWith(MockitoExtension.class)
class UserServiceTest {
@Mock
private UserRepository userRepository;
@InjectMocks
private UserService userService;
@Test
public void createTest() {
Assertions.assertThrows(RuntimeException.class, () -> {
userService.create(null);
});
UserEntity userEntityForNullEmail = UserEntity.builder().build();
Assertions.assertThrows(RuntimeException.class, () -> {
userService.create(userEntityForNullEmail);
});
UserEntity userEntityForExistUser = UserEntity.builder()
.email("test@test.com")
.username("testUsername")
.password("testPassword")
.build();
when(userRepository.existsByEmail(any())).thenReturn(true);
Assertions.assertThrows(RuntimeException.class, () -> {
userService.create(userEntityForExistUser);
});
UserEntity userEntity = UserEntity.builder()
.email("test@test.com")
.username("testUsername")
.password("testPassword")
.build();
reset(userRepository);
when(userRepository.save(any())).thenReturn(userEntity);
UserEntity returnedUserEntity = userService.create(userEntity);
Assertions.assertEquals("test@test.com", returnedUserEntity.getEmail());
Assertions.assertEquals("testUsername", returnedUserEntity.getUsername());
Assertions.assertEquals("testPassword", returnedUserEntity.getPassword());
}
}