Closed benhutchison closed 7 months ago
That's a good find. I suppose one solution would be to have scala3mock filter out private constructors when selecting the constructor when overriding the base class. Filtering based on the access type sounds like something the macro API should allow, although IĀ haven't checked if that is indeed the case. IĀ can probably find some time over the weekend to check that one out.
Interestingly, I do not hit the same compilation error. Instead IĀ get something a bit different, although it could still be the same root cause.
It turns out that error above happen when trying to extends a class with multiple constructors without choosing one in particular, like so
class PSM extends java.io.PrintStream {
}
I got some code that can discard private constructors and build off the first public constructor. Now, PrintStream
is an interesting example because it requires that its OutputStream
argument is non-null, but scala3mock default to null parameters for its parameters and so we get a java.lang.NullPointerException
when instantiating the mocked class.
I can see two ways forward:
class MPrintStream extends java.io.PrintStream(java.io.OutputStream.nullOutputStream())
val m = mock[MPrintStream] when(m.print(_: String)).expects("hello") m.print("hello")
2. Improve the default system of scala3mock for some known type that are often problematic when nulled. We could have a `Default` implementation for `OutputStream` and that should solve the issue for this specific type. This is a more adhoc solution, but with more interesting output long term as we find out more usual types that are required to be null by standard libraries functions.
edit: I went ahead and implemented the second option. Note that the first option is still totally fine, and probably something to resort to for non-standard types. Note that with #16 merged, you can also define your own `Default` given for a type that is not present in the library. The fixed version is available on maven central snapshots with version `0.3.2+8-b7205c5d-SNAPSHOT` if you'd like to give it a try.
I like option 2 too š I'll give it a try and report back
As IĀ haven't heard from you in a while, I'm going to assume this issue is fixed. I have released 0.3.2+8-b7205c5d-SNAPSHOT
as 0.4.0
last month to simplify consumption of this library. Feel free to re-open if that is not the case.
mock[java.io.PrintStream]
doesnt work. UnfortunatelyPrintStream
is just the sort of class one often wants to mock, to verify output is being written.However, there are public ctors for PrintStream, they just aren't the zero-arg one, eg
It would be nice to hint to scala3mock to use a specific ctor that was public, by specifying a signature somehow.. š¤