In Java, How to test void method in Junit Mockito with assertion?

Learner :

How to test void methods in Mockito with assertSame or assertEquals. I am able to do verify only. i am getting sonar or PMD rules violation -"JUnit tests should include assert() or fail()". Below is my sample class with test 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;
        }

    }
LiorH :

You need to capture the value passed to the dao save method. Use mockito's ArgumentCaptor for that. Then assert on that value. Something along these lines:

ArgumentCaptor<Detail> captor = ArgumentCaptor.forClass(Detail.class);
verify(dyDBDAO, times(2)).save(captor.capture());
Detail detail1 = captor.getValues().get(0)
assertEquals(expectedDetail, detail1)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=217558&siteId=1