|
| 1 | +package com.baeldung.mapstruct.emptystringtonull; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 5 | + |
| 6 | +import org.junit.jupiter.api.Test; |
| 7 | + |
| 8 | +class MapEmptyStringToNullUnitTest { |
| 9 | + |
| 10 | + @Test |
| 11 | + void givenAMapperWithGlobalNullHandling_whenConvertingEmptyString_thenOutputNull() { |
| 12 | + EmptyStringToNullGlobal globalMapper = EmptyStringToNullGlobal.INSTANCE; |
| 13 | + Student student = new Student("Steve", ""); |
| 14 | + Teacher teacher = globalMapper.toTeacher(student); |
| 15 | + assertEquals("Steve", teacher.firstName); |
| 16 | + assertNull(teacher.lastName); |
| 17 | + } |
| 18 | + |
| 19 | + @Test |
| 20 | + void givenAMapperWithConditionAnnotationNullHandling_whenConvertingEmptyString_thenOutputNull() { |
| 21 | + EmptyStringToNullCondition conditionMapper = EmptyStringToNullCondition.INSTANCE; |
| 22 | + Student student = new Student("Steve", ""); |
| 23 | + Teacher teacher = conditionMapper.toTeacher(student); |
| 24 | + assertEquals("Steve", teacher.firstName); |
| 25 | + assertNull(teacher.lastName); |
| 26 | + } |
| 27 | + |
| 28 | + @Test |
| 29 | + void givenAMapperUsingExpressionBasedNullHandling_whenConvertingEmptyString_thenOutputNull() { |
| 30 | + EmptyStringToNullExpression expressionMapper = EmptyStringToNullExpression.INSTANCE; |
| 31 | + Student student = new Student("Steve", ""); |
| 32 | + Teacher teacher = expressionMapper.toTeacher(student); |
| 33 | + assertEquals("Steve", teacher.firstName); |
| 34 | + assertNull(teacher.lastName); |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + void givenAMapperUsingConditionBasedNullHandlingWithPropertyNames_whenConvertingEmptyString_thenOutputNull() { |
| 39 | + EmptyStringToNullConditionSourceProperty expressionMapper = EmptyStringToNullConditionSourceProperty.INSTANCE; |
| 40 | + Student student = new Student("Steve", ""); |
| 41 | + Teacher teacher = expressionMapper.toTeacher(student); |
| 42 | + assertEquals("Steve", teacher.firstName); |
| 43 | + assertNull(teacher.lastName); |
| 44 | + } |
| 45 | + |
| 46 | +} |
0 commit comments