How to write Junit for Interface default methods

后端 未结 5 1974
梦如初夏
梦如初夏 2021-02-05 15:24

Please help in writing Junit for the interface default method.

public interface ABC {
    default List getSrc(DEF def, XYZ xyz) t         


        
5条回答
  •  长情又很酷
    2021-02-05 16:12

    You can either create a class that implements your interface or make your test implement it. The second one seems to be a shorter solution:

    public class FunctionCallingTransactionTemplateTest implements FunctionCallingTransactionTemplate {
        private final Object object = Mockito.mock(Object.class);
    
        @Test
        public void should_invoke_function() throws Exception {
            // given
            when(object.toString()).thenReturn("salami");
    
            // when
            String result = functionCallingTransactionTemplate().execute((status) -> object.toString());
    
            // then
            assertThat(result).isEqualTo("salami");
        }
    }
    

提交回复
热议问题