-1

I have a method that creates an object, which the constructor of that object then calls several static/final methods. Mockito cannot mock a constructor, and it can also not mock static/final methods, so what am I supposed to do?

--Edit--

Apparently people think my question is unclear: I have a method. It calls a constructor. Constructor is not mockable. How do I test this method?

The reason why I mentioned static/final methods is because I decided I may as well test the constructor as well and mock the methods within that constructor. However, it is full of static/final methods which makes this impossible.

JobHunter69
  • 1,706
  • 5
  • 25
  • 49
  • 1
    Please be a bit more specific when asking a question: What have you tried so far, with a code example? ([I downvoted because there is no code](//idownvotedbecau.se/nocode/)) / What do you expect? / What error do you get? For help, take a look at "[How to ask](//stackoverflow.com/help/how-to-ask)". – chriptus13 Jun 15 '20 at 16:59
  • @chriptus13 the question is already very clear without needing an example – JobHunter69 Jun 15 '20 at 17:00
  • 1
    @Goldname: Nope, it isn’t. – Nikolas Charalambidis Jun 15 '20 at 17:01
  • @Nikolas So what exactly is unclear about needing code coverage on a method calling a constructor (which is unmockable)? Is there some advanced topic in Mockito where one can mix up with what I'm talking about? – JobHunter69 Jun 15 '20 at 17:03
  • 1
    @Goldname: Is that hard to provide a minimal sample of the code about to be covered? – Nikolas Charalambidis Jun 15 '20 at 17:33
  • @Goldname, maybe you should not initialize the object inside that method, this way, you can create a mock and pass to the method itself. – Mansur Jun 15 '20 at 17:45
  • @Nikolas yes, because I don't have mockito/junit installed on my local computer and I'm working off a remote cluster but setting up a new project there just for testing will take a really long time – JobHunter69 Jun 15 '20 at 18:52
  • @MensurQulami Isn't it bad practice to write additional functions just for the sake of testing it? – JobHunter69 Jun 15 '20 at 18:54
  • 1
    Well, since there is no code example, I am assuming. But no, if the code is not testable there's a big chance that it may not be okay overall. That's not THE rule tho. – Mansur Jun 15 '20 at 18:55
  • Does this answer your question? [Mocking static methods with Mockito](https://stackoverflow.com/questions/21105403/mocking-static-methods-with-mockito) – Progman Jun 15 '20 at 20:38

1 Answers1

1

You can use PowerMockito to mock static methods and also the constructors

For example: https://automationrhapsody.com/mock-static-methods-junit-powermock-example/

  • That's what I've been seeing online, but I cannot use powermockito however – JobHunter69 Jun 15 '20 at 17:18
  • I don't think that we can do it using mockito without some cheating. You can cheat like here https://stackoverflow.com/questions/44967108/how-to-mock-static-method-without-powermock – Daimon Cool Jun 15 '20 at 17:28