I would like to write some state based tests using JMockit
to mock up CSVRecord . The problem is that CSVRecord
is final
(which means I have to use a mocking framework like JMockit
) and CSVRecord's
constructor has package private visibility.
Since it is package private, I can't call new CSVRecord(arg, arg, ...)
, which means I can never I instantiate my mock. Its parent, CSVParser, is the only class that can create an instance.
Does JMockit
have a way to deal with this scenario?
note: JMockit
or Mockito
are the only frameworks we use on this project. No other framework will be acceptable. My preference is to use a MockUp.
If it is just a matter of calling the private constructor, then yes, JMockit has tools to deal with that. See the tutorial
It sounds more like CSVRecord
isn't a good candidate for mocking. If possible, tests targetting another public class which uses it internally would be preferable.
Otherwise, JMockit provides a Deencapsulation
class with newInstance
methods.
As a side note, Mockito only supports behavior-based tests; JMockit provides "mock-ups" ( MockUp
), but it's not the same as writing a pure state-based black box test.
Use:
ConstructorReflection.newInstance
in newer versions of Jmockit.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.