模拟依赖项和代码覆盖工具显示未执行的代码
Mocking dependencies and code coverage tool shows not executed code
我正在尝试将我的 bean 作为组件本身进行测试。这样我要测试的方法才能正确执行。因此我在嘲笑它与 JMockit 的依赖关系。我写了两个测试,一个用于验证 if 条件是否为真,因此方法立即结束,returns null.The 第二个用于执行低于此条件的代码也导致返回 null。但是我的代码覆盖工具 (JaCoCo) 只显示执行了 if 条件,而不是下面的代码。
session是superclassÀbstractBean
中的一个字段。方法 isLoggedIn()
调用 session.isLoggedIn()
并在 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();
}
}
对于遇到熟悉问题的任何人,声明
session.isLoggedIn();
result = true;
在 Expectations Block 中就是答案。虽然我遇到了新的问题,但我要问一个新的问题。
我正在尝试将我的 bean 作为组件本身进行测试。这样我要测试的方法才能正确执行。因此我在嘲笑它与 JMockit 的依赖关系。我写了两个测试,一个用于验证 if 条件是否为真,因此方法立即结束,returns null.The 第二个用于执行低于此条件的代码也导致返回 null。但是我的代码覆盖工具 (JaCoCo) 只显示执行了 if 条件,而不是下面的代码。
session是superclassÀbstractBean
中的一个字段。方法 isLoggedIn()
调用 session.isLoggedIn()
并在 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();
}
}
对于遇到熟悉问题的任何人,声明
session.isLoggedIn();
result = true;
在 Expectations Block 中就是答案。虽然我遇到了新的问题,但我要问一个新的问题。