我正在学习为我的JSF项目编写一个UnitTest,该项目具有ManangedBeans和Session Beans
我在从 Mockito 测试调用 EJB 时遇到问题
package Test;
import ejb.CountrySession;
import java.io.Serializable;
import javax.ejb.EJB;
import javax.inject.Named;
import javax.faces.view.ViewScoped;
@Named(value = "countryMB")
@ViewScoped
public class CountryMB implements Serializable {
@EJB
private CountrySession countSession;
//
private String countryName;
//
private StatusMsg msg;
public CountryMB() {
}
public void setMsg(StatusMsg msg) {
this.msg = msg;
}
public void setCountSession(CountrySession countSession) {
this.countSession = countSession;
}
public String getCountryName() {
return countryName;
}
public void setCountryName(String countryName) {
this.countryName = countryName;
}
public void ajaxAll() {
}
public void saveCountry() {
if (countryName != null && !countryName.trim().isEmpty()) {
boolean chk = countSession.chkCountry(countryName);
if (!chk) {
chk = countSession.addCountry(countryName);
if (chk) {
msg.addInfo("Add Country", "New Country added");
} else {
msg.addError("Add Country", "Unable to add Country");
}
} else {
msg.addWarn("Add Country", "Country already exists");
}
} else {
msg.addWarn("Add Country", "Required parameter not available");
}
}
}
现在在我的测试代码中,我有以下内容
package Test;
import ejb.CountrySession;
import entities.Country;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
public class CountryMBTest {
@Mock
private CountryMB countryMB;
@Mock
private StatusMsg sm;
@Mock
private CountrySession countSession;
@Mock
private EntityManager em;
@Mock
private Query qry;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
countryMB = new CountryMB();
countryMB.setMsg(sm);
countryMB.setCountSession(countSession);
}
@After
public void after() {
countryMB = null;
}
@Test
public void infoCountrySave() {
countryMB.setCountryName("Test");
countryMB.saveCountry();
verify(sm).addInfo("Add Country", "New Country added");
}
@Test
public void errorCountrySave() {
countryMB.setCountryName("Test");
countryMB.saveCountry();
verify(sm).addError("Add Country", "Unable to add Country");
}
@Test
public void warnCountrySave() {
countryMB.setCountryName("Test");
countryMB.saveCountry();
verify(sm).addWarn("Add Country", "Country already exists");
}
@Test
public void chkCountSave() {
List<Country> countLst = null;
Country dum = mock(Country.class);
EntityManager em = mock(EntityManager.class);
Mockito.when(em.find(Country.class, 111)).thenReturn(dum);
CountrySession cs = Mockito.mock(CountrySession.class);
Mockito.when(cs.chkCountry("Test")).thenCallRealMethod();
Mockito.when(cs.getEm()).thenReturn(em);
String name = "Test";
Assert.assertNull(cs.chkCountry(name));
}
}
我的表只有一个记录 pk=1,国家=测试
上面的测试代码从不检查会话 bean,它只是抛出
java.lang.NullPointerException
at ejb.CountrySession.chkCountry(CountrySession.java:67)
at Test.CountryMBTest.chkCountSave(CountryMBTest.java:112)
对于 infoCountrySave() 和 warnCountrySave(),它从不检查数据库中提供的值。
由于我以前从未使用过任何 UnitTest,所以我真的不知道我正在做的事情是否正确,而且我无法通过谷歌搜索找出任何工作代码。
如果有人可以指导我访问一些在线可用的资源,甚至让我知道我需要更正什么才能获得上述 ejb 部分的模拟测试工作,这将非常有帮助。
用 @InjectMocks
更改@Mock
private CountryMB countryMB
当你得到java.lang.NullPointerException
.大多数情况下,您错过了注入某些类或某些依赖项的调用when