How do I verify if I am calling a service with the right paramether, when I want to ignore the rest...












1















I have SomeService class which has method verify(Company company, Location location); Location is enum - Location.EUROPE and Location.USA. Based on the location I choose different email template, that is I have private method inside SomeService:



 private String chooseServiceResource(Location location){
if(location.equals(Location.EUROPE))
return "resourceEU";
return "resourceUSA";
}


inside the someService(Company company, Location location) I call chooseServiceResource(location) and select the right template string.



public void someService(Company company, Location location) {
String name = company.getName();
String info = company.getInfo();
....

String resourcee = chooseResourceTemplate(location);

String body = templateEngine.process(resource, content);
...
//do something else and send the email
}


templateEngine is injected @Qualifier private TemplateEngine templateEngine;



UPDATE:
Inside EmailService I have the following method:



public void setTemplateEngine(TemplateEngine templateEngine) {
this.templateEngine = templateEngine;
}


And I am injecting it the following way:



 @Autowired
@Qualifier("textTemplateEngine")
private TemplateEngine templateEngine;


Inside EmailServiceTest this used to be my setUp():



@Before
public void setup() {
ThymeleafConfig tc = new ThymeleafConfig();
templateEngine = tc.templateEngine();

MockitoAnnotations.initMocks(this);
emailService.setTemplateEngine(templateEngine);
}


Yes, I know that templateEngine is not a mock but an actual object, and hence the error on line verify(templateEngine).process("templateEU", any(Content.class));. But when I change it to a mock, I get NullpointerException.



End Of the Update



In my test, I want to test that when Location is Europe it will process templateEU and when location is USA it will process templateEU.



I tried the following:



@Test
public void shouldCallTemplateEngineWithEuropeTemplate(){
location = Location.EUROPE;

//throws null pointer exception
emailService.sendEmail(any(Person.class), location);

verify(templateEngine).process("templateEU", any(Content.class));
}


But it complains that I am mocking the Person parameter. At this particular test, I don't care which Person object I am sending, I only care about the Location enum.










share|improve this question

























  • You can't call a method with "any person". You need to call it with a specific person. Then, based on that specific person, you can check that process() has been called with eq("templateEU"), and with any() content.

    – JB Nizet
    Nov 22 '18 at 16:59













  • argument matchers are used when arranging or asserting member calls. They are not used as arguments when exercising the member under test'

    – Nkosi
    Nov 22 '18 at 17:03






  • 1





    The message tells exactly what the problem is. You can only verify if a method has been called with expected arguments on a mock, not on a regular object.

    – JB Nizet
    Nov 22 '18 at 17:10
















1















I have SomeService class which has method verify(Company company, Location location); Location is enum - Location.EUROPE and Location.USA. Based on the location I choose different email template, that is I have private method inside SomeService:



 private String chooseServiceResource(Location location){
if(location.equals(Location.EUROPE))
return "resourceEU";
return "resourceUSA";
}


inside the someService(Company company, Location location) I call chooseServiceResource(location) and select the right template string.



public void someService(Company company, Location location) {
String name = company.getName();
String info = company.getInfo();
....

String resourcee = chooseResourceTemplate(location);

String body = templateEngine.process(resource, content);
...
//do something else and send the email
}


templateEngine is injected @Qualifier private TemplateEngine templateEngine;



UPDATE:
Inside EmailService I have the following method:



public void setTemplateEngine(TemplateEngine templateEngine) {
this.templateEngine = templateEngine;
}


And I am injecting it the following way:



 @Autowired
@Qualifier("textTemplateEngine")
private TemplateEngine templateEngine;


Inside EmailServiceTest this used to be my setUp():



@Before
public void setup() {
ThymeleafConfig tc = new ThymeleafConfig();
templateEngine = tc.templateEngine();

MockitoAnnotations.initMocks(this);
emailService.setTemplateEngine(templateEngine);
}


Yes, I know that templateEngine is not a mock but an actual object, and hence the error on line verify(templateEngine).process("templateEU", any(Content.class));. But when I change it to a mock, I get NullpointerException.



End Of the Update



In my test, I want to test that when Location is Europe it will process templateEU and when location is USA it will process templateEU.



I tried the following:



@Test
public void shouldCallTemplateEngineWithEuropeTemplate(){
location = Location.EUROPE;

//throws null pointer exception
emailService.sendEmail(any(Person.class), location);

verify(templateEngine).process("templateEU", any(Content.class));
}


But it complains that I am mocking the Person parameter. At this particular test, I don't care which Person object I am sending, I only care about the Location enum.










share|improve this question

























  • You can't call a method with "any person". You need to call it with a specific person. Then, based on that specific person, you can check that process() has been called with eq("templateEU"), and with any() content.

    – JB Nizet
    Nov 22 '18 at 16:59













  • argument matchers are used when arranging or asserting member calls. They are not used as arguments when exercising the member under test'

    – Nkosi
    Nov 22 '18 at 17:03






  • 1





    The message tells exactly what the problem is. You can only verify if a method has been called with expected arguments on a mock, not on a regular object.

    – JB Nizet
    Nov 22 '18 at 17:10














1












1








1


1






I have SomeService class which has method verify(Company company, Location location); Location is enum - Location.EUROPE and Location.USA. Based on the location I choose different email template, that is I have private method inside SomeService:



 private String chooseServiceResource(Location location){
if(location.equals(Location.EUROPE))
return "resourceEU";
return "resourceUSA";
}


inside the someService(Company company, Location location) I call chooseServiceResource(location) and select the right template string.



public void someService(Company company, Location location) {
String name = company.getName();
String info = company.getInfo();
....

String resourcee = chooseResourceTemplate(location);

String body = templateEngine.process(resource, content);
...
//do something else and send the email
}


templateEngine is injected @Qualifier private TemplateEngine templateEngine;



UPDATE:
Inside EmailService I have the following method:



public void setTemplateEngine(TemplateEngine templateEngine) {
this.templateEngine = templateEngine;
}


And I am injecting it the following way:



 @Autowired
@Qualifier("textTemplateEngine")
private TemplateEngine templateEngine;


Inside EmailServiceTest this used to be my setUp():



@Before
public void setup() {
ThymeleafConfig tc = new ThymeleafConfig();
templateEngine = tc.templateEngine();

MockitoAnnotations.initMocks(this);
emailService.setTemplateEngine(templateEngine);
}


Yes, I know that templateEngine is not a mock but an actual object, and hence the error on line verify(templateEngine).process("templateEU", any(Content.class));. But when I change it to a mock, I get NullpointerException.



End Of the Update



In my test, I want to test that when Location is Europe it will process templateEU and when location is USA it will process templateEU.



I tried the following:



@Test
public void shouldCallTemplateEngineWithEuropeTemplate(){
location = Location.EUROPE;

//throws null pointer exception
emailService.sendEmail(any(Person.class), location);

verify(templateEngine).process("templateEU", any(Content.class));
}


But it complains that I am mocking the Person parameter. At this particular test, I don't care which Person object I am sending, I only care about the Location enum.










share|improve this question
















I have SomeService class which has method verify(Company company, Location location); Location is enum - Location.EUROPE and Location.USA. Based on the location I choose different email template, that is I have private method inside SomeService:



 private String chooseServiceResource(Location location){
if(location.equals(Location.EUROPE))
return "resourceEU";
return "resourceUSA";
}


inside the someService(Company company, Location location) I call chooseServiceResource(location) and select the right template string.



public void someService(Company company, Location location) {
String name = company.getName();
String info = company.getInfo();
....

String resourcee = chooseResourceTemplate(location);

String body = templateEngine.process(resource, content);
...
//do something else and send the email
}


templateEngine is injected @Qualifier private TemplateEngine templateEngine;



UPDATE:
Inside EmailService I have the following method:



public void setTemplateEngine(TemplateEngine templateEngine) {
this.templateEngine = templateEngine;
}


And I am injecting it the following way:



 @Autowired
@Qualifier("textTemplateEngine")
private TemplateEngine templateEngine;


Inside EmailServiceTest this used to be my setUp():



@Before
public void setup() {
ThymeleafConfig tc = new ThymeleafConfig();
templateEngine = tc.templateEngine();

MockitoAnnotations.initMocks(this);
emailService.setTemplateEngine(templateEngine);
}


Yes, I know that templateEngine is not a mock but an actual object, and hence the error on line verify(templateEngine).process("templateEU", any(Content.class));. But when I change it to a mock, I get NullpointerException.



End Of the Update



In my test, I want to test that when Location is Europe it will process templateEU and when location is USA it will process templateEU.



I tried the following:



@Test
public void shouldCallTemplateEngineWithEuropeTemplate(){
location = Location.EUROPE;

//throws null pointer exception
emailService.sendEmail(any(Person.class), location);

verify(templateEngine).process("templateEU", any(Content.class));
}


But it complains that I am mocking the Person parameter. At this particular test, I don't care which Person object I am sending, I only care about the Location enum.







java unit-testing mocking mockito






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 17 at 13:35







sammy333

















asked Nov 22 '18 at 16:52









sammy333sammy333

5113822




5113822













  • You can't call a method with "any person". You need to call it with a specific person. Then, based on that specific person, you can check that process() has been called with eq("templateEU"), and with any() content.

    – JB Nizet
    Nov 22 '18 at 16:59













  • argument matchers are used when arranging or asserting member calls. They are not used as arguments when exercising the member under test'

    – Nkosi
    Nov 22 '18 at 17:03






  • 1





    The message tells exactly what the problem is. You can only verify if a method has been called with expected arguments on a mock, not on a regular object.

    – JB Nizet
    Nov 22 '18 at 17:10



















  • You can't call a method with "any person". You need to call it with a specific person. Then, based on that specific person, you can check that process() has been called with eq("templateEU"), and with any() content.

    – JB Nizet
    Nov 22 '18 at 16:59













  • argument matchers are used when arranging or asserting member calls. They are not used as arguments when exercising the member under test'

    – Nkosi
    Nov 22 '18 at 17:03






  • 1





    The message tells exactly what the problem is. You can only verify if a method has been called with expected arguments on a mock, not on a regular object.

    – JB Nizet
    Nov 22 '18 at 17:10

















You can't call a method with "any person". You need to call it with a specific person. Then, based on that specific person, you can check that process() has been called with eq("templateEU"), and with any() content.

– JB Nizet
Nov 22 '18 at 16:59







You can't call a method with "any person". You need to call it with a specific person. Then, based on that specific person, you can check that process() has been called with eq("templateEU"), and with any() content.

– JB Nizet
Nov 22 '18 at 16:59















argument matchers are used when arranging or asserting member calls. They are not used as arguments when exercising the member under test'

– Nkosi
Nov 22 '18 at 17:03





argument matchers are used when arranging or asserting member calls. They are not used as arguments when exercising the member under test'

– Nkosi
Nov 22 '18 at 17:03




1




1





The message tells exactly what the problem is. You can only verify if a method has been called with expected arguments on a mock, not on a regular object.

– JB Nizet
Nov 22 '18 at 17:10





The message tells exactly what the problem is. You can only verify if a method has been called with expected arguments on a mock, not on a regular object.

– JB Nizet
Nov 22 '18 at 17:10












1 Answer
1






active

oldest

votes


















2














argument matchers are used when arranging or asserting member calls on mocked dependencies.



They are not used/passed as arguments when exercising the member under test and will actually pass the default value of the type, which in this case would be null. Hence the null reference exception when



String name = person.getName();


is called.



Instead, create an instance of the parameter and pass that to the subject under test.



//Arrange
location = Location.EUROPE;
Person person = new Person();

//Act
emailService.sendEmail(person, location);

//Assert
//...


or just mock the class and pass that instead to avoid null reference errors



//Arrange
location = Location.EUROPE;
Person person = mock(Person.class);

//Act
emailService.sendEmail(person, location);

//Assert
//...


making sure to setup any member calls on Person that would cause issues if not configured.



As for the template issue, you would should mock the template so that the unit test is truly an isolated test.



@Before
public void setup() {
templateEngine = mock(TemplateEngine.class); //Use a mock

MockitoAnnotations.initMocks(this);
emailService.setTemplateEngine(templateEngine);
}


And



verify(templateEngine).process("templateEU", any(Content.class));


behaves as intended.






share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53435405%2fhow-do-i-verify-if-i-am-calling-a-service-with-the-right-paramether-when-i-want%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    argument matchers are used when arranging or asserting member calls on mocked dependencies.



    They are not used/passed as arguments when exercising the member under test and will actually pass the default value of the type, which in this case would be null. Hence the null reference exception when



    String name = person.getName();


    is called.



    Instead, create an instance of the parameter and pass that to the subject under test.



    //Arrange
    location = Location.EUROPE;
    Person person = new Person();

    //Act
    emailService.sendEmail(person, location);

    //Assert
    //...


    or just mock the class and pass that instead to avoid null reference errors



    //Arrange
    location = Location.EUROPE;
    Person person = mock(Person.class);

    //Act
    emailService.sendEmail(person, location);

    //Assert
    //...


    making sure to setup any member calls on Person that would cause issues if not configured.



    As for the template issue, you would should mock the template so that the unit test is truly an isolated test.



    @Before
    public void setup() {
    templateEngine = mock(TemplateEngine.class); //Use a mock

    MockitoAnnotations.initMocks(this);
    emailService.setTemplateEngine(templateEngine);
    }


    And



    verify(templateEngine).process("templateEU", any(Content.class));


    behaves as intended.






    share|improve this answer






























      2














      argument matchers are used when arranging or asserting member calls on mocked dependencies.



      They are not used/passed as arguments when exercising the member under test and will actually pass the default value of the type, which in this case would be null. Hence the null reference exception when



      String name = person.getName();


      is called.



      Instead, create an instance of the parameter and pass that to the subject under test.



      //Arrange
      location = Location.EUROPE;
      Person person = new Person();

      //Act
      emailService.sendEmail(person, location);

      //Assert
      //...


      or just mock the class and pass that instead to avoid null reference errors



      //Arrange
      location = Location.EUROPE;
      Person person = mock(Person.class);

      //Act
      emailService.sendEmail(person, location);

      //Assert
      //...


      making sure to setup any member calls on Person that would cause issues if not configured.



      As for the template issue, you would should mock the template so that the unit test is truly an isolated test.



      @Before
      public void setup() {
      templateEngine = mock(TemplateEngine.class); //Use a mock

      MockitoAnnotations.initMocks(this);
      emailService.setTemplateEngine(templateEngine);
      }


      And



      verify(templateEngine).process("templateEU", any(Content.class));


      behaves as intended.






      share|improve this answer




























        2












        2








        2







        argument matchers are used when arranging or asserting member calls on mocked dependencies.



        They are not used/passed as arguments when exercising the member under test and will actually pass the default value of the type, which in this case would be null. Hence the null reference exception when



        String name = person.getName();


        is called.



        Instead, create an instance of the parameter and pass that to the subject under test.



        //Arrange
        location = Location.EUROPE;
        Person person = new Person();

        //Act
        emailService.sendEmail(person, location);

        //Assert
        //...


        or just mock the class and pass that instead to avoid null reference errors



        //Arrange
        location = Location.EUROPE;
        Person person = mock(Person.class);

        //Act
        emailService.sendEmail(person, location);

        //Assert
        //...


        making sure to setup any member calls on Person that would cause issues if not configured.



        As for the template issue, you would should mock the template so that the unit test is truly an isolated test.



        @Before
        public void setup() {
        templateEngine = mock(TemplateEngine.class); //Use a mock

        MockitoAnnotations.initMocks(this);
        emailService.setTemplateEngine(templateEngine);
        }


        And



        verify(templateEngine).process("templateEU", any(Content.class));


        behaves as intended.






        share|improve this answer















        argument matchers are used when arranging or asserting member calls on mocked dependencies.



        They are not used/passed as arguments when exercising the member under test and will actually pass the default value of the type, which in this case would be null. Hence the null reference exception when



        String name = person.getName();


        is called.



        Instead, create an instance of the parameter and pass that to the subject under test.



        //Arrange
        location = Location.EUROPE;
        Person person = new Person();

        //Act
        emailService.sendEmail(person, location);

        //Assert
        //...


        or just mock the class and pass that instead to avoid null reference errors



        //Arrange
        location = Location.EUROPE;
        Person person = mock(Person.class);

        //Act
        emailService.sendEmail(person, location);

        //Assert
        //...


        making sure to setup any member calls on Person that would cause issues if not configured.



        As for the template issue, you would should mock the template so that the unit test is truly an isolated test.



        @Before
        public void setup() {
        templateEngine = mock(TemplateEngine.class); //Use a mock

        MockitoAnnotations.initMocks(this);
        emailService.setTemplateEngine(templateEngine);
        }


        And



        verify(templateEngine).process("templateEU", any(Content.class));


        behaves as intended.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 22 '18 at 17:27

























        answered Nov 22 '18 at 17:06









        NkosiNkosi

        114k16124191




        114k16124191






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53435405%2fhow-do-i-verify-if-i-am-calling-a-service-with-the-right-paramether-when-i-want%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Wiesbaden

            Marschland

            Dieringhausen