欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Mockito单元测试

程序员文章站 2022-04-30 12:45:15
...
import lombok.extern.slf4j.Slf4j;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import static org.mockito.ArgumentMatchers.any;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Application.class)
@Slf4j
public class UserInfoKeeperTest {


    @Autowired
    @InjectMocks
    private TestServiceImpl testServiceImpl;

    @Autowired
    @MockBean
    SayHelloApi sayHelloApi;

    @Before
    public void before(){
        MockitoAnnotations.initMocks(this);
        Mockito.when(sayHelloApi.say(any(SayReq.class))).thenReturn("java world");
    }
    @Test
    public void testMqConfig() {
        String loginRes = testServiceImpl.login();
        log.info("============================================="+loginRes);
    }


}
相关标签: 单元测试