来自 FacesContext 的 Jmockit 模拟 addMessage 方法
Jmockit mock addMessage method from FacesContext
我正在为我的 bean 编写组件测试,但由于来自 FacesContext
的 addMessage()
方法,我不断收到 NullPointerExceptions。
我确定这是因为 addMessage()
方法,因为如果我删除该行,测试就会通过。如何模拟 addMessage()
方法?
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;
}
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;
@Injectable
User user;
@Test
public void testAddChartPoint(@Mocked FacesContext fakeContext) {
new NonStrictExpectations() {
{
session.isLoggedIn();
result = true;
session.getUser();
result = user;
manager.addPointToUser((User) any, (ChartPoint) any);
FacesContext.getInstance();
result = fakeContext;
};
};
Deencapsulation.setField(trendBean, "point", new ChartPoint());
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();
}
protected Session getSession() {
return session;
}
public String getTranslation(final String messageKey,
final Object... formatParameters) {
return getTranslation(getLanguage(), assertNotNull(messageKey),
assertWithoutNull(formatParameters));
}
public static String getTranslation(final Locale locale, final String messageKey,
final Object... formatParameters) {
try {
final ResourceBundle bundle = ResourceBundle.getBundle(
"internationalization.messages", assertNotNull(locale));
return getTranslation(bundle, assertNotNull(messageKey),
assertNotNull(formatParameters));
} catch (final MissingResourceException e) {
assertNotNull(logger)
.error(String.format(
"Severe internationalization error: Internationalization bundle for locale '%s' not found!",
locale.toString()), e);
}
return String.format("No message found for key '%s'!", messageKey);
}
在这种情况下,在对 FacesContext.getCurrentInstance().addMessage
的调用中,您还调用了 AbstractBean .getTranslation(String)
,您还需要模拟该方法。
像这样:
TrendBeanTest
public class TrendBeanTest {
@Tested
TrendBean trendBean;
@Injectable
LoginBean loginBean;
@Injectable
Session session;
@Injectable
ITrendManager manager;
@Injectable
IUserManager userManager;
@Injectable
User user;
@Test
public void testAddChartPoint(@Mocked FacesContext fakeContext, @Mocked AbstractBean abstractBean) {
new NonStrictExpectations() {
{
session.isLoggedIn();
result = true;
session.getUser();
result = user;
manager.addPointToUser((User) any, (ChartPoint) any);
FacesContext.getInstance();
result = fakeContext;
abstractBean.getMessage(anyString); result = "foo";
};
};
Deencapsulation.setField(trendBean, "point", new ChartPoint());
assertEquals(null, trendBean.addChartPoint());
new Verifications(){{
fakeContext.addMessage(withNull(), (FacesMessage)any);
}};
}
}
注意:我是从头开始做的,没有 IDE,所以可能我打错了一些东西。
我正在为我的 bean 编写组件测试,但由于来自 FacesContext
的 addMessage()
方法,我不断收到 NullPointerExceptions。
我确定这是因为 addMessage()
方法,因为如果我删除该行,测试就会通过。如何模拟 addMessage()
方法?
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;
}
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;
@Injectable
User user;
@Test
public void testAddChartPoint(@Mocked FacesContext fakeContext) {
new NonStrictExpectations() {
{
session.isLoggedIn();
result = true;
session.getUser();
result = user;
manager.addPointToUser((User) any, (ChartPoint) any);
FacesContext.getInstance();
result = fakeContext;
};
};
Deencapsulation.setField(trendBean, "point", new ChartPoint());
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();
}
protected Session getSession() {
return session;
}
public String getTranslation(final String messageKey,
final Object... formatParameters) {
return getTranslation(getLanguage(), assertNotNull(messageKey),
assertWithoutNull(formatParameters));
}
public static String getTranslation(final Locale locale, final String messageKey,
final Object... formatParameters) {
try {
final ResourceBundle bundle = ResourceBundle.getBundle(
"internationalization.messages", assertNotNull(locale));
return getTranslation(bundle, assertNotNull(messageKey),
assertNotNull(formatParameters));
} catch (final MissingResourceException e) {
assertNotNull(logger)
.error(String.format(
"Severe internationalization error: Internationalization bundle for locale '%s' not found!",
locale.toString()), e);
}
return String.format("No message found for key '%s'!", messageKey);
}
在这种情况下,在对 FacesContext.getCurrentInstance().addMessage
的调用中,您还调用了 AbstractBean .getTranslation(String)
,您还需要模拟该方法。
像这样:
TrendBeanTest
public class TrendBeanTest {
@Tested
TrendBean trendBean;
@Injectable
LoginBean loginBean;
@Injectable
Session session;
@Injectable
ITrendManager manager;
@Injectable
IUserManager userManager;
@Injectable
User user;
@Test
public void testAddChartPoint(@Mocked FacesContext fakeContext, @Mocked AbstractBean abstractBean) {
new NonStrictExpectations() {
{
session.isLoggedIn();
result = true;
session.getUser();
result = user;
manager.addPointToUser((User) any, (ChartPoint) any);
FacesContext.getInstance();
result = fakeContext;
abstractBean.getMessage(anyString); result = "foo";
};
};
Deencapsulation.setField(trendBean, "point", new ChartPoint());
assertEquals(null, trendBean.addChartPoint());
new Verifications(){{
fakeContext.addMessage(withNull(), (FacesMessage)any);
}};
}
}
注意:我是从头开始做的,没有 IDE,所以可能我打错了一些东西。