The main problem here is that the question is lacking some context. And the use-case for this DataType class is a bit strange (DataType should be the enum).
public enum DataType {
PROFILE("Profile"),
SUPPORT_DETAIL("SupportDetail");
private String value;
private DataType(String value) {
this.value = value;
}
@Override
public String toString() {
return this.value;
}
}
Your do not really need this fromString
-method. Look at this class:
public class DataTypeUtil {
public static DataType fromString(String value) {
return DataType.valueOf(value.toUpperCase()); // should check for null or blank
}
}
Then you can do your test like this, note the 3'rd test-method:
public class DataTypeTest {
@Test
public void testDatatypeFromName() {
DataType d = DataTypeUtil.fromString("Profile");
assertTrue((d.toString().compareToIgnoreCase(DataType.PROFILE.toString()) == 0));
}
@Test(expected = IllegalArgumentException.class)
public void testDatatypeFromInvalidName() {
DataType d = DataTypeUtil.fromString("SupportDetail");
assertFalse((d.toString().compareToIgnoreCase(DataType.SUPPORT_DETAIL.toString()) == 0));
}
@Test
public void testDatatypeFromCorrectName() {
DataType d = DataTypeUtil.fromString("Support_Detail");
assertTrue((d.toString().compareToIgnoreCase(DataType.SUPPORT_DETAIL.toString()) == 0));
}
@Test
public void testGetValueFromEnum() throws Exception {
DataType dataType = DataType.valueOf("Profile".toUpperCase());
assertTrue(dataType == DataType.PROFILE);
}
}
Note the valueOf
-method. It does exactly what your fromString
-method does, and it is available to you from the enum
.
Main point is that you only need your enum
and DataType
is your best bet.
Hope this clarifies your problem somehow :)
Edit:
If I should guess your use-case, you have a document which has a certain format or type. Why not create a Document
class with DataType
as an enum field on the object?
Regards,
Thomas