Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Mockito: check void instance methods


Sometimes, while testing a function, you would like to check whether a Void function is called inside it or not.

You can check Void Instance Methods by using 'Mockito.verify' method.

For example,
Mockito.verify(printServiceMock, Mockito.times(1)).printName("Hari krishna");

Above statement checks, whether printName method of printServiceMock object is called with argument ‘Hari krishna’ or not.

Find the below working application.

Employee.java
package com.sample.model;

public class Employee {
private int id;
private String firstName;
private String lastName;
private double salary;
private String country;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public double getSalary() {
return salary;
}

public void setSalary(double salary) {
this.salary = salary;
}

public String getCountry() {
return country;
}

public void setCountry(String country) {
this.country = country;
}

public Employee(int id, String firstName, String lastName, double salary, String country) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.salary = salary;
this.country = country;
}

@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("Employee [id=").append(id).append(", firstName=").append(firstName).append(", lastName=")
.append(lastName).append(", salary=").append(salary).append(", country=").append(country).append("]");
return builder.toString();
}

}



This post first appeared on Java Tutorial : Blog To Learn Java Programming, please read the originial post: here

Share the post

Mockito: check void instance methods

×

Subscribe to Java Tutorial : Blog To Learn Java Programming

Get updates delivered right to your inbox!

Thank you for your subscription

×