Spring中测试如何写

2018-09-10  本文已影响0人  Huld多

最近在实践测试,经常遇到一个问题,就是测试怎么写,测试粒度是怎么样的?所以狂补了一下测试相关的知识。

测试金字塔

测试金字塔将测试进行了分层,越底层的测试,实现成本越低,测试效率更高,可维护性越高,但是业务需求的反映越低


测试金字塔

如图,测试被分成了三层,从下到上分成:Unit层,UI层和Service层。

单元测试

单元测试处于金字塔最底部,这类测试的特点是:

在Spring boot中主要有三种测试方法

@RunWith(SpringRunner.class)
@WebMvcTest(TaskController.class)
public class TaskControllerTest { 
    @Autowired 
    private MockMvc mockMvc; 
    @MockBean 
    private TaskService taskService; 
    @Test 
    public void should_get_all_tasks_by_getAllTask() throws Exception { 
      given(taskService.getTasks())
        .willReturn(Lists.newArrayList(createTask("test"), createTask("aaa"))); 
       mockMvc.perform(MockMvcRequestBuilders.get("/tasks")) 
        .andExpect(status().isOk())
        .andExpect(jsonPath("$[0].name").value("test")) 
        .andExpect(jsonPath("$[1].name").value("aaa")); 
     } 
    public Task createTask(String name) { 
      Task task = new Task(); 
      task.setName(name); return task; 
    }
 }
@RunWith(MockitoJUnitRunner.class)
public class TaskServiceTest {
  @InjectMocks
  private TaskService taskService;
  @Mock
  private TaskRepository taskRepository;
  @Test
  public void should_return_all_tasks_when_getAllTasks() {
    given(taskRepository.findAll())
      .willReturn(Lists.newArrayList(createTask("test"), createTask("aaa")));
    List<Task> tasks = taskService.getTasks();

    assertThat(tasks.size(), is(2));
    assertThat(tasks.get(0).getName(), is("test"));
    assertThat(tasks.get(1).getName(), is("aaa"));
  }

  public Task createTask(String name) {
    Task task = new Task();
    task.setName(name);
    return task;
  }
}

repository 测试 - 使用DataJpaTest

@RunWith(SpringRunner.class)
@DataJpaTest
@AutoConfigureTestDatabase(replace = NONE)
public class TaskRepositoryTest {
  @Autowired
    private TaskRepository taskRepository;

  @Autowired
  private TestEntityManager entityManager;

  @Test
  public void get_all_will_return_all_tasks() {
    List<Task> tasks = taskRepository.findAll();
    assertThat(tasks.size(), is(2));
    assertThat(tasks.get(0).getName(), is("test"));
    assertThat(tasks.get(1).getName(), is("aaa"));
  }
}

基于H2,flywayTest annotation测试的写法。
@FlywayTest可以放在3个地方

@SpringBootTest(classes = {Application.class}, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@Slf4j
@RunWith(SpringRunner.class)
@TestExecutionListeners({
        DependencyInjectionTestExecutionListener.class,
        TransactionalTestExecutionListener.class,
        FlywayTestExecutionListener.class
})
class FlywayMethodTest {

    @Autowired
    private UserRepository repository;

    @Test
    @FlywayTest
    void contextLoads() {
        assertThat(repository.findAll().size(), is(3));
    }
}

上一篇 下一篇

猜你喜欢

热点阅读