Q:

How to test if a List is an UnmodifableList?

I'm looking for a way to test if some given List is an unmodifiable one.
I have an object that has a List<NoMatter>, in order to offer methods such as addNoMatter(NoMatter nm) instead of allowing the API client to simply do .getNoMatters().add(nm); I always return an unmodifiable version of this list, so the client is still able to have the list. I do it as follows:
public List<NoMatter> getNoMatters() {
    return Collections.unmodifiableList(this.noMatters);
}

The problem is that when I'm doing my tests I simply cant check if this object is of type UnmodifiableList. My first try was doing:
@Test
public void checkIfListIsImmutable(){
    assertTrue("List is not immutable", this.myObj.getNoMatters() instanceof UnmodifiableList);
}

Happens that I cant seem to be able to import the type UnmodifiableList neither java.util.Collections$UnmodifiableRandomAccessList that is what I get when I try System.out.println(myObj.getNoMatters().getClass().getName()); at the console.
So how can I achieve it???
PS.: I know I can pass this test by doing:
@Test(expected = UnsupportedOperationException.class)
public void checkIfListIsImmutable(){
     this.myObj.getNoMatters().add(null);
}

EDIT: The above test doesn't grants me that the list Im dealing with isn't immutable as I would need to have tests to every method that may modify my original list, including .remove(), .clear(), .shuffle(), etc! thats why I do not think this is a nice way to proceed. 
>>> But I still believe that its not even close to an elegant solution! <<<

A:

I think your solution is not only reasonable, it is elegant.  You want to test that you cannot modify the list, and your test proves it succinctly.  Testing the name of the class tests the name, not the behavior.  In this case, who cares about the name?
Likewise, if I wanted to test that I cannot pass null to some method, I'd pass null in a test and expect an IllegalArgumentException.

A:

I actually think that is your best bet.  Your alternative (less elegant) way is to check the name of the class.
this.myObj.getNoMatters().getClass().getSimpleName().equals("UnmodifiableCollection")

The issue for you is the wrapped UnmodifiableCollection is package-private.
I don't see anything wrong with expecting an exception there, but that's just me.

A:

You can use Class#isInstance to check.
Collections.unmodifiableList(someList).getClass().isInstance(listToCheck);

/e1
The following returns false.
Collections.unmodifiableList(new ArrayList<Object>()).getClass().isInstance(new ArrayList<Object>())

The following returns true.
Collections.unmodifiableList(new ArrayList<Object>()).getClass().isInstance(Collections.unmodifiableList(new ArrayList<Object>()))

/e2
Your problem may be because Collections#unmodifiableList returns an UnmodifiableList some of the time, and an UnmodifiableRandomAccessList the rest of the time (see below for code). However, since UnmodifiableRandomAccessList extends UnmodifiableList, if you get an instance of an UnmodifiableList to check with you'll be golden.
To obtain an instance of UnmodifiableList you can use Collections.unmodifiableList(new LinkedList<Object>()). LinkedList does not implement RandomAccess, so an instance of UnmodifiableRandomAccessList will not be returned.
Code of Collections#unmodifiableList:
public static <T> List<T> unmodifiableList(List<? extends T> list) {
        return (list instanceof RandomAccess ?
                new UnmodifiableRandomAccessList<>(list) :
                new UnmodifiableList<>(list));
}

Class header of UnmodifiableRandomAccessList:
static class UnmodifiableRandomAccessList<E> extends UnmodifiableList<E> implements RandomAccess

