#Mockito

52 messages Ā· Page 1 of 1 (latest)

proven pine
#

Hi,
I am learning Mockito and I wonder, why it sets every instance variable as null?
The class I am mocking has an internal List, it must not be null in order for it's methods to work. How do I do that with Mockito?

reef sailBOT
#

āŒ› This post has been reserved for your question.

Hey @proven pine! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

dusty gull
#

The entire point of using mockito is so that you don't have to worry about the class internals and focus on the unit you're testing.

If you're trying to write a unit test for the method doSomething of a class Service that internally calls your ClassWithList which has a method:

private List<Something> list;
public int sizeOfList() {
  return list.size();
}

In your unit test you should mock the ClassWithList, make sure that you inject that in Service somehow, and then mock sizeOfList like this:

// mock the dependency
ClassWithList classWithList = mock(ClassWithList.class);
// mock the behavior
when(classWithList.sizeOfList()).thenReturn(10);
// inject the dependency on the unit we want to test
Service service = new Service(classWithList);
// call the specific behavior we want to test
service.doSomething();
// your assertions
...
#

The main thing to understand here is that when you mock the behavior of sizeOfList it doesn't matter that internally list is null, because sizeOfList is mocked and won't actually run.

proven pine
#
    bill.positionHinzufuegen(MARMELADE,2);
    bill.positionHinzufuegen(BEEF,1);
    FastMoney billValue = bill.billValue();
    assertThat(billValue).isEqualTo(FastMoney.of(2 * 0.99 + 2.29, "EUR"));```
#

@dusty gull

#

Returns null instead of the correct value. I assume it is because the internal List of the Bill class is null

dusty gull
#

What you are doing makes no sense, you are testing your mock. Just instantiate Bill if you want to test the Bill class.

#

If Bill has any internal dependencies that make it difficult to test it, then you should mock those dependencies, but not Bill.

proven pine
#

Is that what you mean?
The problem is that the task says we now have to make the Class customer finalmeaning I cannot mock it like that

#

And we are not allowed to write an Interface

dusty gull
#

Why do you want to use Mockito in the first place?

proven pine
#

It is a task given by University shrugging

dusty gull
#

Just do

Bill bill = new Bill();
proven pine
#

Well, Bill expects a Customer object

dusty gull
#

Then create an instance of Customer as well

#

If it's final , you can't mock it. At least not with Mockito.

#
Customer customer = new Customer();
Bill bill = new Bill(customer);
proven pine
#

Hm, that just seems to easy.
I mean we just learned about Mockito, so why make a task where we have to remove any Mockito usage? : D

#

Ah wait Customer also excepts an argument, a String. Can I mock THAT?

dusty gull
#

Well, I don't know what your task is. I'm just telling you what is possible and what makes sense.

You can't mock a String, it is a final class.

#

Why does Customer have to be final?

proven pine
dusty gull
#

Can you share your task?

proven pine
#

Run the tests in the project 'bill.' Modify the 'Customer' class to make it final (so it cannot be inherited) and then make the 'BillTest' run again without writing an interface.
Note this is translated

dusty gull
#

Where does it say that you must use Mockito?

proven pine
#
  void test_beef_und_marmelade() {
    Bill bill = new Bill(Mockito.mock(Customer.class));
    bill.positionHinzufuegen(MARMELADE,2);
    bill.positionHinzufuegen(BEEF,1);
    FastMoney billValue = bill.billValue();
    assertThat(billValue).isEqualTo(FastMoney.of(2 * 0.99 + 2.29, "EUR"));
  }```
That is the current code. The task is one of three about the topic "Mocking with Mocktio".
#

It does not specifically state I have to use Mockito, but since that's the topic, I assumed so.

dusty gull
#

If I had to guess, I would say your teacher just wants to demonstrate that a final class cannot be mocked.

#

The way Mockito works is by extending your mocked class and replacing the behavior of the methods by the mocked behaviors that you specifiy. If your class is final, then it cannot be extended and therefore Mockito will not work.

proven pine
#

Ill just use the name " " with two spaces because one space throws an IllegalArguementException I guess shrugging

dusty gull
#

do you have the mockito-inline dependency?

proven pine
#

Nope
testImplementation 'org.mockito:mockito-core:4.5.1'

#

Gradle

#

Or wdym

dusty gull
#

Do you have a MockMaker file in your project?

proven pine
dusty gull
#

You can create a file called MockMaker :
src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker
And add the following as the content:
mock-maker-inline

If you do this, then Mockito will allow you to mock final classes. It's an experimental feature.

proven pine
#

ah nvm

dusty gull
#

no, MockMaker is the file. mock-make-inline is the content of the file

proven pine
#

Help, how do I tell the IDE org.mockito.plugins.MockMaker is a package, or should it already be there

dusty gull
#

It's not a package

#

It's the file name

#

I know it looks weird but that's how it is

#

šŸ™‚

reef sailBOT
#

šŸ’¤ Post marked as dormant

This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.

proven pine
reef sailBOT
dusty gull
#

No problem mate, glad I could help. Good luck with your studies!