Reputation: 576
I have a class that will have these methods. In future there will be a method that sets email and password variables. But these variables would never be able for setup from outside of the class. How to test these methods?
public class Auth {
private String email;
private String password;
public String getEmail(){
return this.email;
}
public String getPassword(){
return this.password;
}
}
Upvotes: 1
Views: 7304
Reputation: 821
Testing getters and setters offers no markable benefits and can even be harmful. The reason for that is that pure getters and setters offer no business functionality which could be tested. Instead, they are part of the object oriented design principle (information hiding).
Upvotes: 1
Reputation: 54045
Don't test getters, but some sensible units / use cases. Something must be creating these objects, and it is a part of this unit.
For example:
@Test
public void shouldCreateAuthFromColonSeparated() {
Auth auth = authFactory.create("[email protected]:secret");
assertThat(auth.getEmail(), is("[email protected]"));
assertThat(auth.getPassword(), is("secret"));
}
Upvotes: 4
Reputation: 817
Ideally, you need to setup JUnit using setUp()
overridden method whether password will be set in your Auth
class.
Next thing you need to do is to get the value of actual password from database/whereverStored and compare it with the getPassword()
value.
For testing then you can call:
Auth authObj = ...; //your Auth object where password is set
assertTrue(dbPassword.equals(authObj.getPassword()), true);
Upvotes: 0