DefaultValueGenerator { public Object generate(Object value) { if (value != null) { Class<?> clazz = value.getClass(); if (clazz.isPrimitive()) { return Defaults.defaultValue(clazz); } else { Object defaultValue = BoxedDefaults.defaultValue(clazz); if (defaultValue == null) { try { defaultValue = clazz.newInstance(); } catch (InstantiationException e) { return null; } catch (IllegalAccessException e) { return null; } } return defaultValue; } } return null; }  Object generate(Object value);  }
@Test public void givenLongValue_generateDefault_shouldReturn0() { Long longValue = 3L; long primitiveLong = 4L; Object result = defaultValueGenerator.generate(longValue); Object primitiveResult = defaultValueGenerator.generate(primitiveLong); assertEquals(0L, result); assertEquals(0L, primitiveResult); }
@Test public void givenStringValue_generateDefault_shouldReturnEmptyString() { String stringValue = "hello"; Object result = defaultValueGenerator.generate(stringValue); assertEquals("", result); }
@Test public void givenByteValue_generateDefault_shouldReturn0() { Byte byteValue = 3; byte primitiveByte = 4; Object result = defaultValueGenerator.generate(byteValue); Object primitiveResult = defaultValueGenerator.generate(primitiveByte); assertEquals((byte) 0, result); assertEquals((byte) 0, primitiveResult); }
@Test public void givenShortValue_generateDefault_shouldReturn0() { Short shortValue = 3; short primitiveShort = 4; Object result = defaultValueGenerator.generate(shortValue); Object primitiveResult = defaultValueGenerator.generate(primitiveShort); assertEquals((short) 0, result); assertEquals((short) 0, primitiveResult); }
@Test public void givenIntegerValue_generateDefault_shouldReturn0() { Integer integerValue = 3; int primitiveInteger = 4; Object result = defaultValueGenerator.generate(integerValue); Object primitiveResult = defaultValueGenerator.generate(primitiveInteger); assertEquals(0, result); assertEquals(0, primitiveResult); }
@Test public void givenFloatValue_generateDefault_shouldReturn0() { Float floatValue = 3f; float primitiveFloat = 4; Object result = defaultValueGenerator.generate(floatValue); Object primitiveResult = defaultValueGenerator.generate(primitiveFloat); assertEquals(0f, result); assertEquals(0f, primitiveResult); }
@Test public void givenDoubleValue_generateDefault_shouldReturn0() { Double doubleValue = 3d; double primitiveDouble = 4; Object result = defaultValueGenerator.generate(doubleValue); Object primitiveResult = defaultValueGenerator.generate(primitiveDouble); assertEquals(0d, result); assertEquals(0d, primitiveResult); }