-1

Which mock framework can we use to mock a class or its object. We usually use mocking framework while implementing junit test cases, but how can i mock a class/its's object not in any test case class but a normal class.

Is it possible to do so ?

Janvi Kadu
  • 17
  • 1
  • 6
  • Does this answer your question? [Is there any Mockito-like framework for java application (not under JUnit/Testing environment)?](https://stackoverflow.com/questions/33945506/is-there-any-mockito-like-framework-for-java-application-not-under-junit-testin) – Jonasz Sep 09 '22 at 05:32
  • let me open a big secret - junit test case is normal class, the same as any other – Iłya Bursov Sep 09 '22 at 05:32
  • Yes i agree, but i want to use mock framework in java application , not in junit or any testing environment – Janvi Kadu Sep 09 '22 at 06:16

1 Answers1

0

While mock-frameworks are normally done only in test code, that's not a requirement. You could certainly import your favorite mocking framework as a compile (not test) dependency, and then in any src/main/java class, you would be able to constuct mocks in the normal manner (e.g. @Mock etc).

Why you would do that, I don't know. And therein lies a much more intersting question. Realize mock-frameworks operate via reflection and sometimes by byte-code manipulation. That poses some concern for production-quality code, but now you have it in mainline source. Depending on what you are REALLY trying to accomplish, there might be better ways - e.g. de-encapsulation, AoP, stubs, etc.

Jeff Bennett
  • 996
  • 7
  • 18