• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Junit test with mockito

 
Greenhorn
Posts: 4
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

This is my first time participating in this beautiful website.

I am new to unit-testing and Mcokito.
I am working on creating a unit-test for a method in a legacy big project.
And I am a bit confused how to proceed with it.

I have learned that final classes cannot be mocked.

Is there anyone who can help me how to complete the test for this method?
Here is the method:


Thank you,
 
Marshal
Posts: 5112
319
IntelliJ IDE Python Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I assume when you mention the final class you mean the CareMessageSubject<CareChangeVO> type?

That's really not one of your problems here since the fact that it's final only means you can't reassign another instance to the reference within the scope of the reference, i.e. the method. The key to being able to test a method or any "unit" of code is to gain control of the program state, and in your case you have one particular nasty to tackle.

The method depends on a variable called careSubjectValidatorProvider that appears to be a field belonging to the class (that you don't show). Are you able to inject an instance of that class from your test case? If so then you've no problem.

The other one that may be a problem is the static call to CareMessageSubjectUtils.createSubject. If that static method is a pure function, meaning its output is completely dependent on its input, then you can include that functionality in your test and not worry about it. But if that static method depends on some other state then you're in trouble.

Questions to answer:
1. Can you inject your own careSubjectValidatorProvider?
2. Is CareMessageSubjectUtils.createSubject deterministic? Or does it rely on external state?
 
Amer Mn
Greenhorn
Posts: 4
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your reply,

The class CareMessageSubject<CareChangeVO> is not final. but it has some fields that are final.

Answers:
1. Yes it is injected using this line:


2. The class "CareMessageSubjectUtils" is final with static methods:



I think here I need to test CareSubjectValidator<CareChangeVO>.validate is called on subject. and that CareSubjectValidator<CareChangeProductVO>.validate is called if there are any products. and that any validationMessages are returned.
But I am not sure how to perform this test yet.

Thank you

 
Tim Cooke
Marshal
Posts: 5112
319
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Assuming that the injection of the CareSubjectValidatorProvider work ok then it looks to me like you have everything you need to test this unit.

The basic format of any test is Arrange, Act, Assert.

Arrange: Set up the prerequisite state for your test. In your case you have 3 pieces of state to handle, the method arguments CareMessageSubject<CareChangeVO> and Date, and the class member CareSubjectValidatorProvider.

Act: Call the method.

Assert: Check the returned ValidationMessages object is in the correct state.

In your case the complexity is in the Arrange part of the process, but at the end of it you'll have a much better understanding of how the code behaves and how the inputs influence the outputs.

Have fun!
 
Tim Cooke
Marshal
Posts: 5112
319
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello again, did you make any progress with testing this bit of functionality? I'd love to hear how you got on.
 
Amer Mn
Greenhorn
Posts: 4
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Sir,

Thank you for checking,

Actually I understood the principles of testing and also for the mock, but I am finding difficulties with applying the knowledge in a real complex project.

I have done the following:



The last line is throwing an exception, and it is because the method validate is Final:


And Mockito doesn't mock final classes, at least the version that is used globally in the project.

If you have any suggestion, I would be happy to read them.
Thank you.
 
Tim Cooke
Marshal
Posts: 5112
319
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you considered using the actual objects instead of mocks?
 
Amer Mn
Greenhorn
Posts: 4
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I tried, but CareSubjectValidator is abstract which extends also an abstract class. and an abstract class cannot be instantiated.
 
Tim Cooke
Marshal
Posts: 5112
319
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There must be a concrete subclass of it somewhere you can use
 
Tim Cooke
Marshal
Posts: 5112
319
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On a second look, you don't need to instantiate a CareSubjectValidator yourself because that's the job of the CareSubjectValidatorProvider. You do need to instantiate a CareSubjectValidatorProvider and inject that into your class somehow.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic