Fork me on GitHub
#mount
<
2016-07-22
>
jindrichm06:07:04

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)?

arnout09:07:30

@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.

jindrichm09:07:23

@arnout: Yes, but that won't work once the component is started, because it would be already dereferenced.

arnout09:07:37

So you are trying to pass an unstarted state to a Java method?

jindrichm09:07:20

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.

arnout09:07:48

But if the function that expects the component to be started is not used, then I expect a class cast exception should not occur

jindrichm09:07:33

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.

arnout09:07:46

Ok, good luck 😄

jindrichm09:07:06

Any tips for managing Mount in testing?

arnout09:07:29

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)

arnout09:07:18

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.

arnout09:07:03

Does that make sense to you?

jindrichm09:07:56

Yes, thanks for the tip.