Skip to content

Commit

Permalink
Users - add tests for mapper
Browse files Browse the repository at this point in the history
  • Loading branch information
arminv committed Oct 17, 2024
1 parent 92d82bc commit a3e4bc8
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
13 changes: 13 additions & 0 deletions src/test/java/com/movieapp/movieapp/TestDataUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.movieapp.movieapp.movie.Movie;
import com.movieapp.movieapp.movie.MovieDto;
import com.movieapp.movieapp.user.User;
import com.movieapp.movieapp.user.UserDto;

public final class TestDataUtil {

Expand All @@ -27,6 +28,18 @@ public static User createTestUserEntityA() {
.build();
}

public static UserDto createTestUserDtoA() {
final LocalDateTime dateCreated = LocalDateTime.parse("2024-10-11T18:34:14.406");

return UserDto.builder()
.id(TEST_USER_ID)
.name("Armin")
.email("[email protected]")
.created(dateCreated)
.lastUpdated(dateCreated)
.build();
}

public static Movie createTestMovieEntityA() {
final LocalDateTime dateCreated = LocalDateTime.parse("2020-01-07T11:21:12.103");

Expand Down
42 changes: 39 additions & 3 deletions src/test/java/com/movieapp/movieapp/user/UserMapperTest.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,52 @@
package com.movieapp.movieapp.user;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.modelmapper.ModelMapper;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import com.movieapp.movieapp.TestDataUtil;
import com.movieapp.movieapp.mappers.Mapper;


//TODO:
class UserMapperTest {

private Mapper<User, UserDto> userMapper;

@BeforeEach
public void setUp() {
this.userMapper = new UserMapper(new ModelMapper());
}

@Test
public void testMapTo() {
User userB = TestDataUtil.createTestUserEntityA();
UserDto userDtoB = TestDataUtil.createTestUserDtoA();
UserDto result = this.userMapper.mapTo(userB);

assertEquals(userDtoB, result);
}

@Test
public void testMapFrom() {
UserDto userDtoA = TestDataUtil.createTestUserDtoA();
User userA = TestDataUtil.createTestUserEntityA();
User result = this.userMapper.mapFrom(userDtoA);

assertEquals(userA, result);
}

@Test
void testMapTo() {
public void testMapToThrowsWhenPassedNull() {
NullPointerException exp = assertThrows(NullPointerException.class, () -> this.userMapper.mapTo(null));
assertEquals("user is null", exp.getMessage());
}

@Test
void testMapFrom() {
public void testMapFromThrowsWhenPassedNull() {
NullPointerException exp = assertThrows(NullPointerException.class, () -> this.userMapper.mapFrom(null));
assertEquals("userDto is null", exp.getMessage());
}

}

0 comments on commit a3e4bc8

Please sign in to comment.