我正在尝试为web服务编写Junits。web服务通过3层实现。
Webservice ->实现层-> Dao层。
实现层实际上将DAO对象转换为业务对象,并将其传递回服务。
我的目标是为服务层编写junit。现在,为了实现这一点,我使用@Mock和@ injectmock模拟实现层中的值,因为实现层使用spring注释字段。但每次测试都会得到空指针异常。下面是代码
服务层@Override
public FicheSolvaDetailDTO consulterFicheSolvabiliteTriennale(Long numFicheSolva) throws MetierException, TechniqueException {
try {
LOGGER.info("Consultation de la fiche de solvabilité triennale n° " + numFicheSolva);
return boToMessageConverter.convertFicheSolvabiliteDetaillee(manager.consulterFicheSolvabiliteTriennale(numFicheSolva));
} catch (com.accord.contrat.business.exception.MetierException e) {
throw new MetierException(e.getMessage(), e);
} catch (com.accord.contrat.business.exception.TechniqueException e) {
throw new TechniqueException(e.getMessage(), e);
}
}
然后进入下一层。
@Override
public FicheSolvaDetailDTO consulterFicheSolvabiliteTriennale(Long numFicheSolva) throws MetierException, TechniqueException {
try {
LOGGER.info("Consultation de la fiche de solvabilité triennale n° " + numFicheSolva);
return boToMessageConverter.convertFicheSolvabiliteDetaillee(manager.consulterFicheSolvabiliteTriennale(numFicheSolva));
} catch (com.accord.contrat.business.exception.MetierException e) {
throw new MetierException(e.getMessage(), e);
} catch (com.accord.contrat.business.exception.TechniqueException e) {
throw new TechniqueException(e.getMessage(), e);
}
}
现在是DAO层,我没有发布。
测试类:
@RunWith(MockitoJUnitRunner.class)
public class SolvabiliteWSImplTest {
@InjectMocks
private static SolvabiliteWSImpl mockedSolvabiliteWSImpl;
private static FicheSolvabiliteDetailBO ficheSolvabiliteDetailBO;
@Mock
IFicheSolvaManager managera;
@Mock
BOToMessageConverter boToMessageConverter;
@Mock
MessageToBOConverter messageToBo;
@Before
public void initMocks(){
MockitoAnnotations.initMocks(this);
}
@Before
public void setUp() throws MetierException, TechniqueException{
ficheSolvabiliteDetailBO = new FicheSolvabiliteDetailBO();
ficheSolvabiliteDetailBO.setCodeSituationFamilialeSouscripteur("1");
ficheSolvabiliteDetailBO.setCodeSituationLocativeFoyer("1");
ficheSolvabiliteDetailBO.setNumCommercant(705l);
ficheSolvabiliteDetailBO.setNumEngagement(44686301l);
ficheSolvabiliteDetailBO.setNumFicheSolva(138553l);
when(managera.consulterFicheSolvabiliteTriennale(138553l)).thenReturn(ficheSolvabiliteDetailBO);
}
@Test
public void testConsulterFicheSolvaDetailDTO() throws MetierException, TechniqueException{
long numFicheSolve = 138553l;
assertNotNull(mockedSolvabiliteWSImpl.consulterFicheSolvabiliteTriennale(numFicheSolve));
}
但它不模拟值,如
当(managera.consulterFicheSolvabiliteTriennale (138553 l)) .thenReturn (ficheSolvabiliteDetailBO);
当上面的方法被调用时,它应该返回我ficheSolvabiliteDetailBO。但函数进一步进入这个方法,然后给我空指针异常,这意味着它不是模拟?
请建议一种可能的测试方法。
a)
@InjectMocks
private static SolvabiliteWSImpl mockedSolvabiliteWSImpl;
静态字段上的。这不起作用-将其更改为实例字段。无论如何,JUnit将为每次测试运行创建一个新实例。
b)你不需要MockitoAnnotations.initMocks(this);
当你使用
时@RunWith(MockitoJUnitRunner.class)
就是它们中的任意一个。正如@crea1所写- @Before
s的顺序是不确定的,所以最好直接删除MockitoAnnotations.initMocks(this);
您有多个@Before
方法。JUnit不能保证initMocks()
在setUp()
之前被调用。因此,Mock可能还没有在setUp()
方法中初始化。最好有一个这样的@Before
方法来保证模拟被初始化:
@Before
public void setUp() throws MetierException, TechniqueException {
MockitoAnnotations.initMocks(this);
ficheSolvabiliteDetailBO = new FicheSolvabiliteDetailBO();
ficheSolvabiliteDetailBO.setCodeSituationFamilialeSouscripteur("1");
ficheSolvabiliteDetailBO.setCodeSituationLocativeFoyer("1");
ficheSolvabiliteDetailBO.setNumCommercant(705l);
ficheSolvabiliteDetailBO.setNumEngagement(44686301l);
ficheSolvabiliteDetailBO.setNumFicheSolva(138553l);
when(managera.consulterFicheSolvabiliteTriennale(138553l)).thenReturn(ficheSolvabiliteDetailBO);
}