simple jackson test for enums

This commit is contained in:
eugenp 2014-05-12 17:57:33 +03:00
parent 87b25ea4d7
commit 07419451d7
2 changed files with 45 additions and 2 deletions

View File

@ -0,0 +1,33 @@
package org.baeldung.jackson.dtos.withEnum;
public enum TypeEnumSimple {
TYPE1(1, "Type A"), TYPE2(2, "Type 2");
private Integer id;
private String name;
private TypeEnumSimple(final Integer id, final String name) {
this.id = id;
this.name = name;
}
// API
public Integer getId() {
return id;
}
public void setId(final Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(final String name) {
this.name = name;
}
}

View File

@ -8,6 +8,7 @@ import java.io.IOException;
import org.baeldung.jackson.dtos.withEnum.MyDtoWithEnum;
import org.baeldung.jackson.dtos.withEnum.MyDtoWithEnumCustom;
import org.baeldung.jackson.dtos.withEnum.TypeEnum;
import org.baeldung.jackson.dtos.withEnum.TypeEnumSimple;
import org.baeldung.jackson.dtos.withEnum.TypeEnumWithCustomSerializer;
import org.junit.Test;
@ -19,9 +20,18 @@ public class JacksonSerializationEnumsUnitTest {
// tests - enums
@Test
public final void whenSerializingSimpleEnum_thenCorrect() throws JsonParseException, IOException {
public final void whenSerializingASimpleEnum_thenCorrect() throws JsonParseException, IOException {
final ObjectMapper mapper = new ObjectMapper();
final String dtoAsString = mapper.writeValueAsString(TypeEnum.TYPE1);
final String dtoAsString = mapper.writeValueAsString(TypeEnumSimple.TYPE1.name());
System.out.println(dtoAsString);
assertThat(dtoAsString, containsString("TYPE1"));
}
@Test
public final void whenSerializingAnEnum_thenCorrect() throws JsonParseException, IOException {
final ObjectMapper mapper = new ObjectMapper();
final String dtoAsString = mapper.writeValueAsString(TypeEnum.TYPE1.name());
System.out.println(dtoAsString);
assertThat(dtoAsString, containsString("\"name\":\"Type A\""));