简体   繁体   English

在 Java 中,如何使用断言测试 Junit Mockito 中的 void 方法?

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

How to test void methods in Mockito with assertSame or assertEquals.如何使用 assertSame 或 assertEquals 测试 Mockito 中的 void 方法。 I am able to do verify only.我只能做验证。 i am getting sonar or PMD rules violation -"JUnit tests should include assert() or fail()".我收到声纳或 PMD 规则违规 - “JUnit 测试应包括 assert() 或 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;
        }

    }

You need to capture the value passed to the dao save method.您需要捕获传递给 dao 保存方法的值。 Use mockito's ArgumentCaptor for that.为此使用 mockito 的 ArgumentCaptor。 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)

I would assume that your void method that needs to be tested is update in MyServiceImpl .我假设您需要测试的void方法是update MyServiceImpl

Firstly, you can verify if dyDBDAO.save() is called.首先,您可以验证是否dyDBDAO.save()

Mockito.verify(dyDBDAO).save(Mockito.anyList...);

Secondly, you can check the modified or created records in the database by retrieving them and comparing to the inputs from getMockDetails .其次,您可以通过检索它们并与来自getMockDetails的输入进行比较来检查数据库中修改或创建的记录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM