Hi, I just started using Mount. Probably a beginner's question: I wonder how do you avoid class cast exceptions when using Mount components as arguments of Java methods (i.e. mount.core.DerefableState cannot be cast to the.expected.class
)?
@jindrichm: A DerefableState
must be derefed to get to the actual value (which you can then pass in as an argument to any function or Java method). So, (.yourJavaMethod obj @your-state)
would do the trick I guess.
@arnout: Yes, but that won't work once the component is started, because it would be already dereferenced.
So you are trying to pass an unstarted state to a Java method?
No. This is used in a function that expects the component to be already started. However, it doesn't work in contexts when the component doesn't need to be started and the function is not used, such as during testing. Then I get a class cast exception.
But if the function that expects the component to be started is not used, then I expect a class cast exception should not occur
That's correct. Actually I overlooked that the function is called indirectly during testing. My fault. So I need to start the component also during testing.
Ok, good luck 😄
Any tips for managing Mount in testing?
Eh, let me see, I use mount-lite
, where combinations of (start (only ...))
and (start (substitute ...))
(so (start (only ...) (substitute ...))
) are your friend. In mount
this translates to use of (start-with ...)
and (start ...)
, or with (-> (only ...) (swap ...) start)
So, in short, you get full control over what components need to be started for a certain test, and you can mock (substitute) them if necessary.
Does that make sense to you?
Yes, thanks for the tip.
@jindrichm: a little more about composing states: https://github.com/tolitius/mount#composing-states