HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaModerate

Using reflection in a test to check if a private variable is really null after a function, is this okay?

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
afterthisreflectionreallynullokayfunctionprivatetestusing

Problem

I am using reflection to check if a private variable is set to null after the logout function. This is needed because the getUser function will always attempt to set and return an user if no user is set. Am I doing something wrong or can I do something better?

This is the bean that has the logout and getUser function:

@ManagedBean(name = "authBean")
@SessionScoped
public class AuthorizationBean implements Serializable{

    //Data access object for the users
    @Inject
    UserDao userDao;

    private User user; // The JPA entity.
    public User getUser() {
        if (user == null) {
            user = (User) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("user");
            if (user == null) {
                Principal principal = FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal();
                if (principal != null) {
                    user = userDao.findByEmail(principal.getName()); // Find User by j_username.
                }
            }
        }
        return user;
    }

    /**
     * Function that handles the logout
     * @return Redirect string that points to the login page
     */
    public String doLogout() {
        // invalidate the session, so that the session is removed
        FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
        user = null;
        // return redirect to login page
        return "/login.xhtml?faces-redirect=true";
    }

}


This is the test class, with the reflection:

```
@RunWith(PowerMockRunner.class)
@PrepareForTest(FacesContext.class)
public class AuthorizationBeanTest {

private AuthorizationBean authorizationBean;

@Mock
User user;

@Mock
FacesContext facesContext;

@Mock
ExternalContext externalContext;

@Before
public void setUp() {
authorizationBean = new AuthorizationBean();
Map sessionMap = new HashMap<>();
sessionMap.put("user", user);

//Mocking

Solution

Personally I wouldn't do this.

The reason why is when your field change name, your test fails because the field is hardcoded there and with refactoring this isn't persisted to the test.

What should I do?

Normally your AuthorizationBeanTest is in the same package as your AuthorizationBean.

When this is correct you could use a protected method.

Example :

protected boolean isUserNull () {
    return user==null;
}


This is completely safe in your pojo, nobody could even acces the User object.

And outside the package no one will ever see this method.

The advantage is when you refactor user or the method name, your test is automatically updated.

Code Snippets

protected boolean isUserNull () {
    return user==null;
}

Context

StackExchange Code Review Q#112010, answer score: 10

Revisions (0)

No revisions yet.