#Mockito
52 messages Ā· Page 1 of 1 (latest)
ā This post has been reserved for your question.
Hey @proven pine! Please use
/closeor theClose Postbutton 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.
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.
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
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.
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
Why do you want to use Mockito in the first place?
It is a task given by University 
Just do
Bill bill = new Bill();
Well, Bill expects a Customer object
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);
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?
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?
Idk, it's just what the task says 
Can you share your task?
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
Where does it say that you must use Mockito?
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.
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.

Ill just use the name " " with two spaces because one space throws an IllegalArguementException I guess 
do you have the mockito-inline dependency?
Do you have a MockMaker file in your project?
No, I only have the testImplementation 'org.mockito:mockito-core:4.5.1 dependency in Gradle.
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.
mock-make-inlinebeing a file?
ah nvm
no, MockMaker is the file. mock-make-inline is the content of the file
Help, how do I tell the IDE org.mockito.plugins.MockMaker is a package, or should it already be there
It's not a package
It's the file name
I know it looks weird but that's how it is
š
š¤ 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.
Thank you for your help. I forgot to say that yesterday. I was in a hurry : D
If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
No problem mate, glad I could help. Good luck with your studies!