在Java中,如何使用断言测试Junit Mockito中的void方法?
In Java, How to test void method in Junit Mockito with assertion?
如何使用 assertSame 或 assertEquals 测试 Mockito 中的无效方法。我只能做验证。
我收到声纳或 PMD 规则违规 -"JUnit tests should include assert() or fail()"。
下面是我的样本 class 和测试 class。
@Service
public class MyServiceImpl implements IService {
@Autowired
private IDyDBDAO dyDBDAO;
@Override
public void update() {
dyDBDAO.save(getDetailData());
}
@Override
public List<Detail> getCurrentDetail() {
return getDetails(dyDBDAO.findAll());
}
private List<Detail> getDetails(Iterable<Detail> details) {
...blah...
}
private String getPlace(){
Places p = Places.getPlace();//static
return p == null? PlacesUtil.getName("DH"): p.getName;
}
private Detail getDetailData() {
Detail d = new Detail();
d.setName("blah");
d.setDesc("fsdfsdfdsf");
d.setPlace(getPlace());
return d;
}
}
@RunWith(PowerMockRunner.class)
@PrepareForTest({Places.class, PlacesUtil.class})
public class MyServiceImplTest {
@InjectMocks
private MyServiceImpl myServiceImpl;
@Mock
private IDyDBDAO dyDBDAO;
@Test
public void testGetCurrentDetail() {
given(dyDBDAO.findAll()).willReturn(getMockDetails());
assertSame(myServiceImpl.getCurrentDetail().size(), 2);
}
@Test
public void testUpdate() {
PowerMockito.mockStatic(Places.class);
// first update , second update -us-west-2 will update
given(Places.getPlace()).willReturn(PlacesUtil.getName("UH"))
.willReturn(null);
myServiceImpl.syncStatus();
// update again with DH
myServiceImpl.syncStatus();
verify(dyDBDAO, times(2)).save(any(Detail.class));
// how to assert checking here
}
private Iterable<Detail> getMockDetails() {
Detail d1 = new Detail();
d1.setName("blah");
d1.setDesc("fsdfsdfdsf");
d1.setPlace("sdfsdf");
Detail d2 = new Detail();
d2.setName("blahblah1");
d2.setDesc("e345345");
d2.setPlace("8907j");
List<Detail> listOfDetail = new ArrayList<>();
listOfDetail.add(eps1);
listOfDetail.add(eps2);
return listOfDetail;
}
}
我假设你的 void
方法需要测试是 update
in MyServiceImpl
.
首先,您可以验证是否调用了dyDBDAO.save()
。
Mockito.verify(dyDBDAO).save(Mockito.anyList...);
其次,您可以通过检索并与来自 getMockDetails
的输入进行比较来检查数据库中修改或创建的记录。
您需要捕获传递给 dao 保存方法的值。为此使用 mockito 的 ArgumentCaptor。然后断言该值。
沿着这些线的东西:
ArgumentCaptor<Detail> captor = ArgumentCaptor.forClass(Detail.class);
verify(dyDBDAO, times(2)).save(captor.capture());
Detail detail1 = captor.getValues().get(0)
assertEquals(expectedDetail, detail1)
如何使用 assertSame 或 assertEquals 测试 Mockito 中的无效方法。我只能做验证。 我收到声纳或 PMD 规则违规 -"JUnit tests should include assert() or fail()"。 下面是我的样本 class 和测试 class。
@Service
public class MyServiceImpl implements IService {
@Autowired
private IDyDBDAO dyDBDAO;
@Override
public void update() {
dyDBDAO.save(getDetailData());
}
@Override
public List<Detail> getCurrentDetail() {
return getDetails(dyDBDAO.findAll());
}
private List<Detail> getDetails(Iterable<Detail> details) {
...blah...
}
private String getPlace(){
Places p = Places.getPlace();//static
return p == null? PlacesUtil.getName("DH"): p.getName;
}
private Detail getDetailData() {
Detail d = new Detail();
d.setName("blah");
d.setDesc("fsdfsdfdsf");
d.setPlace(getPlace());
return d;
}
}
@RunWith(PowerMockRunner.class)
@PrepareForTest({Places.class, PlacesUtil.class})
public class MyServiceImplTest {
@InjectMocks
private MyServiceImpl myServiceImpl;
@Mock
private IDyDBDAO dyDBDAO;
@Test
public void testGetCurrentDetail() {
given(dyDBDAO.findAll()).willReturn(getMockDetails());
assertSame(myServiceImpl.getCurrentDetail().size(), 2);
}
@Test
public void testUpdate() {
PowerMockito.mockStatic(Places.class);
// first update , second update -us-west-2 will update
given(Places.getPlace()).willReturn(PlacesUtil.getName("UH"))
.willReturn(null);
myServiceImpl.syncStatus();
// update again with DH
myServiceImpl.syncStatus();
verify(dyDBDAO, times(2)).save(any(Detail.class));
// how to assert checking here
}
private Iterable<Detail> getMockDetails() {
Detail d1 = new Detail();
d1.setName("blah");
d1.setDesc("fsdfsdfdsf");
d1.setPlace("sdfsdf");
Detail d2 = new Detail();
d2.setName("blahblah1");
d2.setDesc("e345345");
d2.setPlace("8907j");
List<Detail> listOfDetail = new ArrayList<>();
listOfDetail.add(eps1);
listOfDetail.add(eps2);
return listOfDetail;
}
}
我假设你的 void
方法需要测试是 update
in MyServiceImpl
.
首先,您可以验证是否调用了dyDBDAO.save()
。
Mockito.verify(dyDBDAO).save(Mockito.anyList...);
其次,您可以通过检索并与来自 getMockDetails
的输入进行比较来检查数据库中修改或创建的记录。
您需要捕获传递给 dao 保存方法的值。为此使用 mockito 的 ArgumentCaptor。然后断言该值。 沿着这些线的东西:
ArgumentCaptor<Detail> captor = ArgumentCaptor.forClass(Detail.class);
verify(dyDBDAO, times(2)).save(captor.capture());
Detail detail1 = captor.getValues().get(0)
assertEquals(expectedDetail, detail1)