[JAVA] Really scary ClassCastException

Interface of method under test

public class Foo {
    public void process(List<String> arg0) {
        /*See below*/

Appropriate test code written by myself

List<String> arg0 = Arrays.asList("Ah", "I", "U");

Foo foo = new Foo();

This falls with a ClassCastException for some reason. I was surprised to see the source to be tested.

Why did you bother to cast

public void process(List<String> arg0) {
    //↓ I don't even get a compile warning!
    List<String> ls = (ArrayList) arg0;

    //The following is omitted

It was horrible code that didn't even give a compile warning ...

It didn't fall where it was natural. (I'm not saying it's good because it doesn't fall)

Test code written by a newcomer

List<String> arg0 = new ArrayList<>();

Foo foo = new Foo();

Recommended Posts

Really scary ClassCastException
A really scary (Java anti-pattern) story
[Java] Really scary switch statement story