Warm tip: This article is reproduced from serverfault.com, please click

test junit for coverage with mock

发布于 2020-11-28 23:12:08

sorry for my english. i have a problem with junit tests. the client asks for coverage for all classes, even for dto / model / entity (even if it's wrong, I have to do it). but i have trouble testing getters and setters. I can't cover, I have tested many solutions but either 0 coverage or in error, what can I do?

my class:

@Entity
 public class AnagrafeUser {

private String firstName; 
private String lastName; 

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;
} }

my class test

public class AnagrafeUserTest {

@InjectMocks
private AnagrafeUser anagrafeUser;

@Before
public void setUp() throws Exception {
    MockitoAnnotations.initMocks(this);
}

@Test
public void setanagrafeUser() throws Exception {
    when(anagrafeUser.getFirstName()).thenReturn("fistname");
    when(anagrafeUser.getLastName()).thenReturn("lastName");

    anagrafeUser.setFirstName("fistname");
    anagrafeUser.setLastName("lastName");
}

always error, for example for last when : when() requires an argument which has to be 'a method call on a mock'. For example: when(mock.getArticles()).thenReturn(articles);

Also, this error might show up because:

  1. you stub either of: final/private/equals()/hashCode() methods. Those methods cannot be stubbed/verified. Mocking methods declared on non-public parent classes is not supported.
  2. inside when() you don't call method on mock but on some other object. tks

setter not coverage.

NEW test, coverage 100%, but is ok?

public class AnagrafeUserTest {

@InjectMocks
private AnagrafeUser anagrafeUser;

Before
public void setUp() throws Exception {
    MockitoAnnotations.initMocks(this);
    anagrafeUser.setFirstName("a");
    anagrafeUser.setLastName("a");
    anagrafeUser.setUserCode("a");
}

Test
public void setanagrafeUser() throws Exception {
    assertNotNull(anagrafeUser.getFirstName());
    assertNotNull(anagrafeUser.getLastName());
    assertNotNull(anagrafeUser.getUserCode());
}

}

Questioner
qwerty98
Viewed
0
priyranjan 2020-11-29 11:31:02

you need not to struggle that much. 1st of all this is simple POJO class. so you can make use of Assert class that provides you assertion methods, useful for writing tests cases.

public void AnagrafeUserTest{

AnagrafeUser anagrafeUser = new AnagrafeUser();

@Test
public void setanagrafeUser(){
anagrafeUser.setFirstName("Harry");
anagrafeUser.setLastName("jackson");
Assert.assertEquals("Harry",anagrafeUser.getFirstName());
Assert.assertEquals("jackson",anagrafeUser.getLastName());
}}

I would never recommend you to use Mockito here since this this is just basic POJO class. Mockito we use if there is dependency in the code ( like database n all). In that case we use Mockito to create dummy object n perform dependent operations.