-2
public class DatatypeTest {

    private static final Log logger = LogFactory
            .getLog(TreeConstantTest.class);

    @Test
    public void testDatatypeFromName()
    {
        Datatype d= Datatype.fromString("Profile");

        assertTrue((d.toString().compareToIgnoreCase("PROFILE") == 0));

    }
     @Test
     public void testDatatypeFromName1()
     {
         Datatype d = Datatype.fromString("SupportDetail");
         assertTrue((d.toString().compareToIgnoreCase("SUPPORT_DETAIL") == 0 ));
     }
}

When i execute the first test case it is showing failure case in green.

But when i execute the second test case it is showing me the error as java.lang.AssertionError.

I am writing the test cases for this class

public enum DocDatatype {

PROFILE("Profile"),

SUPPORT_DETAIL("SupportDetail"),
Sri
  • 29
  • 5
  • 3
    The string `"SupportDetail"` does not match the string `"SUPPORT_DETAIL"`, even if you ignore case. The second one has an underscore in it. – khelwood Dec 24 '14 at 16:24
  • public enum DocDatatype { PROFILE("Profile"), SUPPORT_DETAIL("SupportDetail"), I am writing test case for this class – Sri Dec 24 '14 at 16:26
  • 2
    If you're saying that this ought to be true based on your enum, then include the enum code in your question. – khelwood Dec 24 '14 at 16:28
  • `AssertionError`, being a `Throwable`, can give you a detailed message of the underlying cause of the error. You should examine that. – abl Dec 24 '14 at 16:30
  • 1
    The bit of `enum` code you've posted doesn't include the definitions of `fromString` or `toString`, which are the things your unit test is checking. – khelwood Dec 24 '14 at 16:32
  • java.lang.AssertionError at org.junit.Assert.fail(Assert.java:86) at org.junit.Assert.assertTrue(Assert.java:41) at org.junit.Assert.assertTrue(Assert.java:52) at com.canon.cusa.newweb.orm.inet.test.DocDatatypeTest.testDocDatatypeFromName1(DocDatatypeTest.java:37) – Sri Dec 24 '14 at 16:37

2 Answers2

1

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

thomas77
  • 1,100
  • 13
  • 27
0

What you need is the ability to see the actual value.

You can go in with a debugger, but that's not a good solution: what if your test is run by an automated system?

You need to change your assertion into something that will provide more information.

Please look at this question for JUnit way to assert equalsIgnoreCase.

Another way is to provide a failure message:

assertTrue("Unexpected result: + d.toString(), "SUPPORT_DETAIL".compareToIgnoreCase(d.toString()) == 0 ));
Community
  • 1
  • 1
  • DOC Datatype Constants public enum DocDatatype { PROFILE("Profile"), SUPPORT_DETAIL("SupportDetail"), String name; DocDatatype(String name) { this.name = name; } public String getName() { return name; } public String toString() { return name; } public static DocDatatype fromString(String value) { for (DocDatatype type : DocDatatype.values()) { if (type.getName().equals(value)) return type; } throw new java.lang.IllegalArgumentException(value + " is Not valid dmDataType"); } } – Sri Dec 24 '14 at 21:35