| 
 | 1 | +/*  | 
 | 2 | + *    Copyright 2015-2023 the original author or authors.  | 
 | 3 | + *  | 
 | 4 | + *    Licensed under the Apache License, Version 2.0 (the "License");  | 
 | 5 | + *    you may not use this file except in compliance with the License.  | 
 | 6 | + *    You may obtain a copy of the License at  | 
 | 7 | + *  | 
 | 8 | + *       https://www.apache.org/licenses/LICENSE-2.0  | 
 | 9 | + *  | 
 | 10 | + *    Unless required by applicable law or agreed to in writing, software  | 
 | 11 | + *    distributed under the License is distributed on an "AS IS" BASIS,  | 
 | 12 | + *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  | 
 | 13 | + *    See the License for the specific language governing permissions and  | 
 | 14 | + *    limitations under the License.  | 
 | 15 | + */  | 
 | 16 | +package sample.mybatis.web;  | 
 | 17 | + | 
 | 18 | +import static org.assertj.core.api.Assertions.assertThat;  | 
 | 19 | + | 
 | 20 | +import java.util.Map;  | 
 | 21 | + | 
 | 22 | +import org.junit.jupiter.api.BeforeEach;  | 
 | 23 | +import org.junit.jupiter.api.Test;  | 
 | 24 | +import org.mockito.Mockito;  | 
 | 25 | +import org.springframework.beans.factory.annotation.Autowired;  | 
 | 26 | +import org.springframework.boot.test.context.SpringBootTest;  | 
 | 27 | +import org.springframework.boot.test.mock.mockito.MockBean;  | 
 | 28 | +import org.springframework.boot.test.web.client.TestRestTemplate;  | 
 | 29 | + | 
 | 30 | +import sample.mybatis.web.domain.City;  | 
 | 31 | +import sample.mybatis.web.mapper.CityMapper;  | 
 | 32 | + | 
 | 33 | +/**  | 
 | 34 | + * @author Kazuki Shimizu  | 
 | 35 | + */  | 
 | 36 | +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)  | 
 | 37 | +class SampleMybatisWithMockBeanApplicationTest {  | 
 | 38 | + | 
 | 39 | +  @Autowired  | 
 | 40 | +  private TestRestTemplate restTemplate;  | 
 | 41 | + | 
 | 42 | +  @MockBean  | 
 | 43 | +  CityMapper cityMapper;  | 
 | 44 | + | 
 | 45 | +  @BeforeEach  | 
 | 46 | +  void setup() {  | 
 | 47 | +    City city = new City();  | 
 | 48 | +    city.setId(10L);  | 
 | 49 | +    city.setCountry("US");  | 
 | 50 | +    city.setState("NV");  | 
 | 51 | +    city.setName("Las Vegas");  | 
 | 52 | +    Mockito.when(cityMapper.findByState("NV")).thenReturn(city);  | 
 | 53 | +  }  | 
 | 54 | + | 
 | 55 | +  @Test  | 
 | 56 | +  void test() {  | 
 | 57 | +    @SuppressWarnings("unchecked")  | 
 | 58 | +    Map<String, Object> body = this.restTemplate.getForObject("/cities/{state}", Map.class, "NV");  | 
 | 59 | +    assertThat(body).hasSize(4).containsEntry("id", 10).containsEntry("name", "Las Vegas").containsEntry("state", "NV")  | 
 | 60 | +        .containsEntry("country", "US");  | 
 | 61 | +  }  | 
 | 62 | +}  | 
0 commit comments