samedi 28 novembre 2020

test junit for coverage with mock

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.

Aucun commentaire:

Enregistrer un commentaire