在Junit测试用例中动态传递一个值

问题描述 投票:0回答:1

我为弹性搜索CRUD操作创建了一个JUNIT测试用例,我在下面给出了我的类文件和JUNIT测试用例的代码。我已经在代码测试用例中对值进行了硬编码,是否有可能将值动态传递给测试用例

Employeeinformation.class

package com.ElasticSearchCrud.ElasticSearchCrud;

import lombok.Data;

import java.util.List;

@Data
public class EmployeeInformation {
    private String id;
    private String firstName;
    private String lastName;
    //private List<Technologies> technologies;
   // private List<String> emails;


    public EmployeeInformation(String id, String firstName, String lastName) {
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
    }

    @Override
    public String toString() {
        return "EmployeeInformation{" +
                "id='" + id + '\'' +
                ", firstName='" + firstName + '\'' +
                ", lastName='" + lastName + '\'' +
                '}';
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
}

用于在Elasticsearch中创建文档的JUNIT测试用例

package com.ElasticSearchCrud.ElasticSearchCrud;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.ArrayList;
import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

@SpringBootTest
class ElasticSearchCrudApplicationTests {

    @Test
    void contextLoads() {

    }

    private EmployeeService Eservice;

    @Autowired
    public ElasticSearchCrudApplicationTests(EmployeeService service) {
        this.Eservice = service;
    }

    @Test
    void createProfile() throws Exception {

        List<Technologies> technologies = new ArrayList<>();
        technologies.add(new Technologies("xxx", "xxxx"));
        technologies.add(new Technologies("xxx", "xxx"));

        List<String> emails = new ArrayList<>();
        emails.add("[email protected]");
        emails.add("[email protected]");

        EmployeeInformation EmployeePost = new EmployeeInformation("5", "xx", "xx",technologies,emails);

        String Result = Eservice.createProfileDocument(EmployeePost, "elcrud");

        EmployeeInformation EmployeeGet = Eservice.findById("elcrud", "5");

        assertNotNull(EmployeeGet.getId());

        assertEquals(EmployeeGet.getId(), EmployeePost.getId());

        assertEquals(EmployeeGet.getFirstName(), EmployeePost.getFirstName());

        assertEquals(EmployeeGet.getLastName(), EmployeePost.getLastName());

        assert Result.equals("CREATED") || Result.equals("UPDATED");
    }

    @Test
    void findAll() throws Exception {

        List<EmployeeInformation> Emp= Eservice.findAll("elcrud");

        System.out.println(Emp.size());

        int Result = Emp.size();

        assertTrue(Result  >= 0 );
    }

    @Test
    void findById() throws Exception {

        EmployeeInformation EmployeeGet = Eservice.findById("elcrud", "2");

        assertNotNull(EmployeeGet.getId());

        assertNotNull(EmployeeGet.getFirstName());

        assertNotNull(EmployeeGet.getLastName());

    }

    @Test
    void deleteProfileDocument() throws Exception {

        String Result = Eservice.deleteProfileDocument("elcrud", "3");

        System.out.println(Result);

        assertEquals(Result, "DELETED");
    }

    @Test
    void updateProfile() throws Exception {

        List<Technologies> technologies = new ArrayList<>();
        technologies.add(new Technologies("xxx", "2020"));
        technologies.add(new Technologies("xxx", "2013"));

        List<String> emails = new ArrayList<>();
        emails.add("[email protected]");
        emails.add("[email protected]");

        EmployeeInformation EmployeePost = new EmployeeInformation("3", "JUnit", "Testing",technologies,emails);

        String Result = Eservice.updateProfile(EmployeePost,"elcrud");

        assert Result.equals("NOOP") || Result.equals("UPDATED");

    }


    @Test
    void search() throws Exception {

        List<EmployeeInformation> Emp=Eservice.searchByTechnology("Lucidworks","elcrud");

        System.out.println(Emp.size());

        int Result = Emp.size();

        assertTrue(Result  >= 0 );
    }

    @Test
    void searchByName() throws Exception {

        List<EmployeeInformation> Emp=Eservice.findProfileByName("junit","elcrud");

        System.out.println(Emp.size());

        int Result = Emp.size();

        assertTrue(Result  >= 0 );
    }

}

上面的代码可以正常工作,但是要在测试用例中对值进行硬编码,必须动态地传递它。有人可以建议如何实现吗?

spring-boot elasticsearch junit5 spring-data-elasticsearch
1个回答
0
投票

可以在单元测试中对值进行硬编码,这实际上是一个好习惯,因此您可以非常清楚地看到测试用例的输入/输出。有时由于您的代码需要唯一值(例如在数据库中创建新用户)或特殊情况而可能无法实现,在这种情况下,您几乎没有选择:

  • 使用模拟(请参阅Mockito)以避免与数据库的交互
  • 生成随机值(仍在测试代码中)
  • 从属性文件或POM文件中读取值(作为系统变量)
© www.soinside.com 2019 - 2024. All rights reserved.