Mockint objectmapper with mockito (#9017)
* Mockint objectmapper with mockito * enforcing naming convention for test classes * removed Spring * removed unattended changes * modified example * simplified call stack in flower validator
This commit is contained in:
parent
5738683a5b
commit
b31b84d475
|
@ -28,7 +28,7 @@
|
|||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<mockito.version>2.21.0</mockito.version>
|
||||
<mockito.version>2.24.0</mockito.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -14,8 +14,23 @@
|
|||
<relativePath>../../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
<version>${jackson.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-junit-jupiter</artifactId>
|
||||
<version>${mockito.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<mockito.version>2.21.0</mockito.version>
|
||||
<jackson.version>2.10.3</jackson.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -0,0 +1,32 @@
|
|||
package com.baeldung.mockito.objectmapper;
|
||||
|
||||
public class Flower {
|
||||
|
||||
private String name;
|
||||
private Integer petals;
|
||||
|
||||
public Flower(String name, Integer petals) {
|
||||
this.name = name;
|
||||
this.petals = petals;
|
||||
}
|
||||
|
||||
public Flower() {
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Integer getPetals() {
|
||||
return petals;
|
||||
}
|
||||
|
||||
public void setPetals(Integer petals) {
|
||||
this.petals = petals;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
package com.baeldung.mockito.objectmapper;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
public class FlowerJsonStringValidator {
|
||||
private ObjectMapper objectMapper;
|
||||
|
||||
public FlowerJsonStringValidator(ObjectMapper objectMapper) {
|
||||
this.objectMapper = objectMapper;
|
||||
}
|
||||
|
||||
public boolean flowerHasPetals(String jsonFlowerAsString) throws JsonProcessingException {
|
||||
Flower flower = objectMapper.readValue(jsonFlowerAsString, Flower.class);
|
||||
return flower.getPetals() > 0;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,53 @@
|
|||
package com.baeldung.mockito.objectmapper;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
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 org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.Mockito.anyString;
|
||||
import static org.mockito.Mockito.eq;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
public class FlowerJsonStringValidatorUnitTest {
|
||||
|
||||
@Mock
|
||||
private ObjectMapper objectMapper;
|
||||
|
||||
private FlowerJsonStringValidator flowerJsonStringValidator;
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
flowerJsonStringValidator = new FlowerJsonStringValidator(objectMapper);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCallingHasPetalsWithPetals_thenReturnsTrue() throws JsonProcessingException {
|
||||
Flower rose = new Flower("testFlower", 100);
|
||||
|
||||
when(objectMapper.readValue(anyString(), eq(Flower.class))).thenReturn(rose);
|
||||
|
||||
assertTrue(flowerJsonStringValidator.flowerHasPetals("this can be a very long json flower"));
|
||||
|
||||
verify(objectMapper, times(1)).readValue(anyString(), eq(Flower.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCallingHasPetalsWithZeroPetal_thenReturnsFalse() throws JsonProcessingException {
|
||||
Flower rose = new Flower("testFlowerWithoutPetal", 0);
|
||||
|
||||
when(objectMapper.readValue(anyString(), eq(Flower.class))).thenReturn(rose);
|
||||
|
||||
assertFalse(flowerJsonStringValidator.flowerHasPetals("this can be a very long json flower"));
|
||||
|
||||
verify(objectMapper, times(1)).readValue(anyString(), eq(Flower.class));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue