Search code examples
javaunit-testingjunitmockingjmockit

Mocking dependencies and code coverage tool shows not executed code


i'm trying to test my bean as a component itself. So that the method i want to test gets executed correctly. Therefore i'm mocking it's dependencies with JMockit. I wrote two tests, one for validating the if condition to true, so the method ends immediately and returns null.The second one for executing the code below this condition also resulting in returning null. But my code coverage tool (JaCoCo) just shows that the if condition is executed and not the code below. Session is a field in the super class ÀbstractBean. The method isLoggedIn() invokes session.isLoggedIn() and is defined in AbstractBean.

TrendBean

@Named(value = "trendBean")
@ViewScoped
@SuppressWarnings("deprecation")
public class TrendBean extends AbstractBean implements Serializable {

private static final long serialVersionUID = -310401000218411384L;

private static final Logger logger = Logger.getLogger(TrendBean.class);

private ChartPoint point;

private List<ChartPoint> points;

@Inject
private ITrendManager manager;


public String addChartPoint() {
    if (!isLoggedIn()) {
        return null; //only this block is executed
    }
    assertNotNull(point);
    final User user = getSession().getUser();
    manager.addPointToUser(user, point);
    FacesContext.getCurrentInstance().addMessage(
            null,
            new FacesMessage(FacesMessage.SEVERITY_INFO,
                    getTranslation("pointAdded"), ""));
    init();
    return null;

    }
}

TrendBeanTest

public class TrendBeanTest {

@Tested
TrendBean trendBean;
@Injectable
LoginBean loginBean;
@Injectable
Session session;
@Injectable
ITrendManager manager;
@Injectable
IUserManager userManager;

@Test
public void testAddChartPoint() {

    new NonStrictExpectations() {

        {
            session.isLoggedIn();
            result = true;
            session.getUser();
            result = (User) any;
            manager.addPointToUser((User) any, (ChartPoint) any);
        };
    };

    Deencapsulation.setField(trendBean, "point", new ChartPoint());

    assertEquals(null, trendBean.addChartPoint());

}

@Test
public void testAddChartPointNotLoggedIn() {

    new Expectations() {
        {
            manager.addPointToUser((User) any, (ChartPoint) any);
            times = 0;
        };
    };

    Session s = new Session();
    s.setUser(null);

    Deencapsulation.setField(trendBean, "session", s);

    assertEquals(null, trendBean.addChartPoint());

    }
}

AbstractBean

public abstract class AbstractBean {

private static final Logger logger = Logger.getLogger(AbstractBean.class);

@Inject
private Session session;

public boolean isLoggedIn() {
    return session.isLoggedIn();
}
} 

Solution

  • For anyone having familiar problems the statement

    session.isLoggedIn();
    result = true;
    

    in Expectations Block was the answer. Although i'm facing new problems, i'm going to ask a new question.