I understand that unit tests must be as isolated as possible, i.e. are self-contained and do not have to rely on outside resources like databases, network access or even the execution of previous unit tests.
However, suppose I want to test class Y. Class Y uses class X. However, I have already a number of unit tests that test class X.
I think that in the unit tests of class Y, I could just assume that class X works properly and use instantiations of it to test class Y (instantiated in the class Y unit tests, so no leftovers or other pollution).
Is this correct? Or do I need to mock class X when testing class Y or do something else entirely? If so or if I should mock class X, what are the reasons for that?
Your unit tests for class Y should only test class Y's code. You should assume that everything class Y relies on is already working (and tested). This is standard unit testing. You want to reduce external dependencies, and try to isolate your tests so that you're really only testing class Y's functionality in class Y's tests, but in the real world, everything is connected.
In my opinion it's much better to use class X and assume it works than it is to mock out class X to provide purer unit isolation. Either way, you should assume that class X is a black box and that it works.
Is assuming that another unit test has tested the input of the unit code breaking isolation?