Skip to content

自动化测试用例

自动化测试用例

目标

PetClinic 宠物主人姓搜索功能的单元测试目标主要是确保这一功能在各种情况下都能正确工作。

  1. 姓搜索的准确性验证:

  2. 针对已知存在的宠物主人姓,验证搜索功能是否能够准确返回匹配的结果。

  3. 针对不存在的宠物主人姓,验证搜索功能是否返回空的结果。

  4. 非法输入参数的处理:

  5. 验证搜索功能对于非法输入参数的处理是否能够产生正确的响应,例如输入空字符串、数字或特殊字符。

通过达成这些目标,单元测试可以提供对宠物主人姓搜索功能的信心,确保其在各种情况下都能够正确运行。

步骤

编写 PetClinic 宠物主人姓搜索功能的单元测试代码通常涉及使用一个合适的单元测试框架(如 JUnit、TestNG 等)和编程语言(例如 Java)。以下是编写单元测试代码的一般步骤:

  1. 导入测试框架
import org.junit.Test;
import static org.junit.Assert.*;

这是一个简单的 JUnit 导入语句的示例。确保你使用的测试框架的导入语句正确。

  1. 导入被测试的类
import com.example.PetClinic; // 替换为实际的包路径和类名

导入宠物诊所类,确保它包含了你想要测试的姓搜索功能。

  1. 编写测试用例

这是一个简化的测试用例示例,你可以根据实际情况添加更多的测试用例。

  1. 运行测试

在 IDE 中运行你的单元测试,确保每个测试用例都能通过。这通常会显示测试运行结果和任何失败的断言。

  1. 分析测试结果

如果测试用例失败,检查失败的断言并分析问题的原因。可能需要回到被测试的代码并进行修复。

参考示例

package org.springframework.samples.petclinic.owner;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.hasProperty;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.BDDMockito.given;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;

import java.time.LocalDate;
import java.util.List;

import org.assertj.core.util.Lists;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledInNativeImage;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.test.web.servlet.MockMvc;


@WebMvcTest(OwnerController.class)
@DisabledInNativeImage
class OwnerControllerTests {

    private static final int TEST_OWNER_ID = 1;

    @Autowired
    private MockMvc mockMvc;

    @MockBean
    private OwnerRepository owners;

    private Owner george() {
        Owner george = new Owner();
        george.setId(TEST_OWNER_ID);
        george.setFirstName("George");
        george.setLastName("Franklin");
        george.setAddress("110 W. Liberty St.");
        george.setCity("Madison");
        george.setTelephone("6085551023");
        Pet max = new Pet();
        PetType dog = new PetType();
        dog.setName("dog");
        max.setType(dog);
        max.setName("Max");
        max.setBirthDate(LocalDate.now());
        george.addPet(max);
        max.setId(1);
        return george;
    };

    @BeforeEach
    void setup() {

        Owner george = george();
        given(this.owners.findByLastName(eq("Franklin"), any(Pageable.class)))
            .willReturn(new PageImpl<Owner>(Lists.newArrayList(george)));

        given(this.owners.findAll(any(Pageable.class))).willReturn(new PageImpl<Owner>(Lists.newArrayList(george)));

        given(this.owners.findById(TEST_OWNER_ID)).willReturn(george);
        Visit visit = new Visit();
        visit.setDate(LocalDate.now());
        george.getPet("Max").getVisits().add(visit);

    }

    @Test
    void testInitCreationForm() throws Exception {
        mockMvc.perform(get("/owners/new"))
            .andExpect(status().isOk())
            .andExpect(model().attributeExists("owner"))
            .andExpect(view().name("owners/createOrUpdateOwnerForm"));
    }

    @Test
    void testProcessCreationFormSuccess() throws Exception {
        mockMvc
            .perform(post("/owners/new").param("firstName", "Joe")
                .param("lastName", "Bloggs")
                .param("address", "123 Caramel Street")
                .param("city", "London")
                .param("telephone", "01316761638"))
            .andExpect(status().is3xxRedirection());
    }

    @Test
    void testProcessCreationFormHasErrors() throws Exception {
        mockMvc
            .perform(post("/owners/new").param("firstName", "Joe").param("lastName", "Bloggs").param("city", "London"))
            .andExpect(status().isOk())
            .andExpect(model().attributeHasErrors("owner"))
            .andExpect(model().attributeHasFieldErrors("owner", "address"))
            .andExpect(model().attributeHasFieldErrors("owner", "telephone"))
            .andExpect(view().name("owners/createOrUpdateOwnerForm"));
    }

    @Test
    void testInitFindForm() throws Exception {
        mockMvc.perform(get("/owners/find"))
            .andExpect(status().isOk())
            .andExpect(model().attributeExists("owner"))
            .andExpect(view().name("owners/findOwners"));
    }

    @Test
    void testProcessFindFormSuccess() throws Exception {
        Page<Owner> tasks = new PageImpl<Owner>(Lists.newArrayList(george(), new Owner()));
        Mockito.when(this.owners.findByLastName(anyString(), any(Pageable.class))).thenReturn(tasks);
        mockMvc.perform(get("/owners?page=1")).andExpect(status().isOk()).andExpect(view().name("owners/ownersList"));
    }

    @Test
    void testProcessFindFormByLastName() throws Exception {
        Page<Owner> tasks = new PageImpl<Owner>(Lists.newArrayList(george()));
        Mockito.when(this.owners.findByLastName(eq("Franklin"), any(Pageable.class))).thenReturn(tasks);
        mockMvc.perform(get("/owners?page=1").param("lastName", "Franklin"))
            .andExpect(status().is3xxRedirection())
            .andExpect(view().name("redirect:/owners/" + TEST_OWNER_ID));
    }

    @Test
    void testProcessFindFormNoOwnersFound() throws Exception {
        Page<Owner> tasks = new PageImpl<Owner>(Lists.newArrayList());
        Mockito.when(this.owners.findByLastName(eq("Unknown Surname"), any(Pageable.class))).thenReturn(tasks);
        mockMvc.perform(get("/owners?page=1").param("lastName", "Unknown Surname"))
            .andExpect(status().isOk())
            .andExpect(model().attributeHasFieldErrors("owner", "lastName"))
            .andExpect(model().attributeHasFieldErrorCode("owner", "lastName", "notFound"))
            .andExpect(view().name("owners/findOwners"));

    }

    @Test
    void testInitUpdateOwnerForm() throws Exception {
        mockMvc.perform(get("/owners/{ownerId}/edit", TEST_OWNER_ID))
            .andExpect(status().isOk())
            .andExpect(model().attributeExists("owner"))
            .andExpect(model().attribute("owner", hasProperty("lastName", is("Franklin"))))
            .andExpect(model().attribute("owner", hasProperty("firstName", is("George"))))
            .andExpect(model().attribute("owner", hasProperty("address", is("110 W. Liberty St."))))
            .andExpect(model().attribute("owner", hasProperty("city", is("Madison"))))
            .andExpect(model().attribute("owner", hasProperty("telephone", is("6085551023"))))
            .andExpect(view().name("owners/createOrUpdateOwnerForm"));
    }

    @Test
    void testProcessUpdateOwnerFormSuccess() throws Exception {
        mockMvc
            .perform(post("/owners/{ownerId}/edit", TEST_OWNER_ID).param("firstName", "Joe")
                .param("lastName", "Bloggs")
                .param("address", "123 Caramel Street")
                .param("city", "London")
                .param("telephone", "01616291589"))
            .andExpect(status().is3xxRedirection())
            .andExpect(view().name("redirect:/owners/{ownerId}"));
    }

    @Test
    void testProcessUpdateOwnerFormUnchangedSuccess() throws Exception {
        mockMvc.perform(post("/owners/{ownerId}/edit", TEST_OWNER_ID))
            .andExpect(status().is3xxRedirection())
            .andExpect(view().name("redirect:/owners/{ownerId}"));
    }

    @Test
    void testProcessUpdateOwnerFormHasErrors() throws Exception {
        mockMvc
            .perform(post("/owners/{ownerId}/edit", TEST_OWNER_ID).param("firstName", "Joe")
                .param("lastName", "Bloggs")
                .param("address", "")
                .param("telephone", ""))
            .andExpect(status().isOk())
            .andExpect(model().attributeHasErrors("owner"))
            .andExpect(model().attributeHasFieldErrors("owner", "address"))
            .andExpect(model().attributeHasFieldErrors("owner", "telephone"))
            .andExpect(view().name("owners/createOrUpdateOwnerForm"));
    }

    @Test
    void testShowOwner() throws Exception {
        mockMvc.perform(get("/owners/{ownerId}", TEST_OWNER_ID))
            .andExpect(status().isOk())
            .andExpect(model().attribute("owner", hasProperty("lastName", is("Franklin"))))
            .andExpect(model().attribute("owner", hasProperty("firstName", is("George"))))
            .andExpect(model().attribute("owner", hasProperty("address", is("110 W. Liberty St."))))
            .andExpect(model().attribute("owner", hasProperty("city", is("Madison"))))
            .andExpect(model().attribute("owner", hasProperty("telephone", is("6085551023"))))
            .andExpect(model().attribute("owner", hasProperty("pets", not(empty()))))
            .andExpect(model().attribute("owner", hasProperty("pets", new BaseMatcher<List<Pet>>() {

                @Override
                public boolean matches(Object item) {
                    @SuppressWarnings("unchecked")
                    List<Pet> pets = (List<Pet>) item;
                    Pet pet = pets.get(0);
                    if (pet.getVisits().isEmpty()) {
                        return false;
                    }
                    return true;
                }

                @Override
                public void describeTo(Description description) {
                    description.appendText("Max did not have any visits");
                }
            })))
            .andExpect(view().name("owners/ownerDetails"));
    }

}