Skip to content
Advertisement

How to mock my service using Mockito framework

Hello I have struglling write a mock test according to my following method below;

my code below;

JavaScript

my yml configuration below;

JavaScript

my service below;

JavaScript

I have a created PropertySourceResolver class

JavaScript

and my test class below;

JavaScript

I like to change my code according to test scenarios like ; when … then return any idea? thank you.

Advertisement

Answer

To make this simpler and better organize your code, abstract away the complexity of injecting the configurations by creating the following class:

JavaScript

Then, you should make this a dependency for CodeConfigBuilder:

JavaScript

Now, in order to test this class you have to do it with three tests:

  1. Test that PromoRequest is valid because postfix is null;
  2. Test that PromoRequest is valid because length is valid;
  3. Test that PromoRequest is invalid because length is not valid;

They would be something like the following:

JavaScript

If you are using JUnit 5, you could replace the last test with the following one to easily assert the exception message:

JavaScript

P.S.: do you mean suffix instead of postfix?

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement