added spring boot unit test example
This commit is contained in:
@@ -31,4 +31,5 @@ dependencies {
|
|||||||
runtime('com.h2database:h2')
|
runtime('com.h2database:h2')
|
||||||
testCompile('org.springframework.boot:spring-boot-starter-test')
|
testCompile('org.springframework.boot:spring-boot-starter-test')
|
||||||
testCompile 'org.junit.jupiter:junit-jupiter-engine:5.2.0'
|
testCompile 'org.junit.jupiter:junit-jupiter-engine:5.2.0'
|
||||||
|
testCompile('org.mockito:mockito-junit-jupiter:2.23.0')
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,19 @@
|
|||||||
|
package io.reflectoring.testing;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class RegisterUseCase {
|
||||||
|
|
||||||
|
private final UserRepository userRepository;
|
||||||
|
|
||||||
|
public User registerUser(User user) {
|
||||||
|
user.setRegistrationDate(LocalDateTime.now());
|
||||||
|
return userRepository.save(user);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
package io.reflectoring.testing;
|
||||||
|
|
||||||
|
import javax.persistence.Entity;
|
||||||
|
import javax.persistence.Id;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Data
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class User {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
private Long id;
|
||||||
|
private String name;
|
||||||
|
private String email;
|
||||||
|
private LocalDateTime registrationDate;
|
||||||
|
|
||||||
|
public User(String name, String email) {
|
||||||
|
this.name = name;
|
||||||
|
this.email = email;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package io.reflectoring.testing;
|
||||||
|
|
||||||
|
|
||||||
|
import org.springframework.data.repository.CrudRepository;
|
||||||
|
|
||||||
|
public interface UserRepository extends CrudRepository<User, Long> {
|
||||||
|
}
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
package io.reflectoring.testing;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.junit.jupiter.MockitoExtension;
|
||||||
|
import static io.reflectoring.testing.UserAssert.assertThat;
|
||||||
|
import static org.mockito.AdditionalAnswers.*;
|
||||||
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
|
import static org.mockito.Mockito.*;
|
||||||
|
|
||||||
|
@ExtendWith(MockitoExtension.class)
|
||||||
|
class RegisterUseCaseTest {
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private UserRepository userRepository;
|
||||||
|
|
||||||
|
private RegisterUseCase registerUseCase;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void initUseCase() {
|
||||||
|
registerUseCase = new RegisterUseCase(userRepository);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void savedUserHasRegistrationDate() {
|
||||||
|
User user = new User("zaphod", "zaphod@mail.com");
|
||||||
|
when(userRepository.save(any(User.class))).then(returnsFirstArg());
|
||||||
|
User savedUser = registerUseCase.registerUser(user);
|
||||||
|
assertThat(savedUser).hasRegistrationDate();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
package io.reflectoring.testing;
|
||||||
|
|
||||||
|
import org.assertj.core.api.AbstractAssert;
|
||||||
|
|
||||||
|
public class UserAssert extends AbstractAssert<UserAssert, User> {
|
||||||
|
|
||||||
|
public UserAssert(User user) {
|
||||||
|
super(user, UserAssert.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static UserAssert assertThat(User actual) {
|
||||||
|
return new UserAssert(actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
public UserAssert hasRegistrationDate() {
|
||||||
|
isNotNull();
|
||||||
|
if (actual.getRegistrationDate() == null) {
|
||||||
|
failWithMessage("Expected user to have a registration date, but it was null");
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user