Form docs:
Comparable c=mock(Comparable.class);
when(c.compareTo(anyInt())).thenReturn(-1);
I would like:
File tempDir=new File("test");
Comparable c=mock(Comparable.class);
when(c.compareTo(anyInt())).thenReturn(tempDir.mkdir());
So will be created real folder (i will use it next private method of class).
Does it possible?
Thanks.
It’s incredibly unclear what you’re trying to accomplish. I’m not at all certain why you want to do something like this in the first place.
As I said in my comment, the reason your current example won’t compile is because
File.mkdir()returns abooleanandComparable.compareTo(Comparable)returns anint. You can, however, make this compile if you are mocking a method that returns aboolean, like so:That said, I seriously doubt this will do what you want. The line
when(bar.isTrue()).thenReturn(file.mkdir());actually invokesfile.mkdir(). So your directory will be created when you create your mock. In other words, the previous example can (and should) be equivalently written as:Writing it like this would avoid any confusion on when the directory was actually created.