Q:

ClassCastException on enum toString

Can anyone explain why the following JUnit test will fail with a ClassCastException: Message cannot be cast to java.lang.String in the Assert statement below?
    public enum Message{
        NULL_REQUEST("Null Request"),
        INVALID_NUMBER_OF_REQUESTS("Invalid number of requests");

        Message(String msg){
            this.msg = msg;
        }

        @Override
        public String toString(){
            return msg;
        }
        private final String msg;
    }

Then the test class has: 
@Test
public void test(){
    String x = Message.INVALID_NUMBER_OF_REQUESTS.toString();
    Assert.assertEquals(x, "actual value" );
}

A:

cxfquest,
your code has (had) some errors, please correct it.
The following code works for me:
public class MyTestUnit {

    public enum Message{
        NULL_REQUEST("Null Request"),
        INVALID_NUMBER_OF_REQUESTS("Invalid number of requests");

        Message(String msg){
            this.msg = msg;
        }

        private final String msg;

        public String getMsg() {
            return msg;
        }

        @Override
        public String toString() {
            return msg;
        }

    }

    @Test
    public void test(){
        String x = Message.INVALID_NUMBER_OF_REQUESTS.toString();
        Assert.assertEquals(x, "actual value" );
    }

}

I think you are not compiling and you are running an old version (maybe).
Result is:

org.junit.ComparisonFailure: expected:<[Invalid number of requests]>
  but was:<[actual value]>

as expected.

