spring—避免在junit中模拟模拟对象的底层方法调用

xhv8bpkk  于 2021-07-23  发布在  Java
关注(0)|答案(1)|浏览(342)

下面是我的代码。resttemplatefactoryformyapp是从中获取resttemplate对象的。在我正在测试的其他类resttemplateexampleclass中。我使用它如下,它的作品很好。我已经将junit编写到restemplate call,它也可以正常工作。但是当我在junit测试类中将下面的代码注解为resttemplatefactoryformyapp.resttemplate()=null时,我得到了nullpointerexception。我怎样才能避免这种情况?假设我在restemplatefactoryformyapp中调用了10个不同的类方法,那么我也必须对它们执行相同的操作。那我怎么才能避免呢?

@Configuration
public class RestTemplateFactoryForMyApp {

    @Bean
    public RestTemplate restTemplate() {

    return new RestTemplate();
}

given(restTemplateFactoryForMyApp.restTemplate()).willReturn(restTemplate);
    private final RestTemplate restTemplate = mock(RestTemplate.class);

@Autowired
    private RestTemplateFactoryForMyApp restTemplateFactoryForMyApp;

restTemplateFactoryForMyApp.restTemplate().exchange

下面是我的junit测试restemplate调用

@ExtendWith(MockitoExtension.class)
public class RestTemplateExampleTest {

    @Mock
    private RestTemplateFactoryForMyApp restTemplateFactoryForMyApp;

    @InjectMocks
    private RestTemplateExample restTemplateExample ;

    private final RestTemplate restTemplate = mock(RestTemplate.class);

    @BeforeEach
    void setUp() throws Exception {
        given(restTemplateFactoryForMyApp.restTemplate()).willReturn(restTemplate);
        ReflectionTestUtils.setField(restTemplateExample, "id", 1);
    }

    @Test
    public void mock_resttemplate() {
        TodosObject todosObject = new TodosObject();
        todosObject.setId(1);

        System.out.println(" restTemplateFactoryForMyApp "  + restTemplateFactoryForMyApp.restTemplate());

        int i = (int) ReflectionTestUtils.getField(restTemplateExample, "id");

        System.out.println( " value of i " + i);

        Mockito
          .when(restTemplateFactoryForMyApp.restTemplate().exchange(
                "https://jsonplaceholder.typicode.com/todos/" + i,
                HttpMethod.GET,
                null,
                TodosObject.class
        ))
          .thenReturn(new ResponseEntity<TodosObject>(todosObject, HttpStatus.OK));
kyvafyod

kyvafyod1#

我不完全确定您的问题,但也许返回\u deep \u stubs有帮助:

@ExtendWith(MockitoExtension.class)
public class RestTemplateExampleTest {

    @Mock(answer = RETURNS_DEEP_STUBS) 
    private RestTemplateFactoryForMyApp restTemplateFactoryForMyApp;

    @InjectMocks
    private RestTemplateExample restTemplateExample;

    @BeforeEach
    void setUp() throws Exception {
        ReflectionTestUtils.setField(restTemplateExample, "id", 1);
    }

    @Test
    public void mock_resttemplate() {
        TodosObject todosObject = new TodosObject();
        todosObject.setId(1);

        System.out.println(" restTemplateFactoryForMyApp "  + restTemplateFactoryForMyApp.restTemplate());

        int i = (int) ReflectionTestUtils.getField(restTemplateExample, "id");

        System.out.println( " value of i " + i);

        Mockito
          .when(restTemplateFactoryForMyApp.restTemplate().exchange(
                "https://jsonplaceholder.typicode.com/todos/" + i,
                HttpMethod.GET,
                null,
                TodosObject.class
        ))
          .thenReturn(new ResponseEntity<TodosObject>(todosObject, HttpStatus.OK));

相关问题