Spring MVC Test-Framework和Mockito的JUnit Test Sample

1.需要的依赖:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.microfocus.g11n</groupId>
    <artifactId>openl10n</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <properties>
        <ci.buildnumber>SNAPSHOT</ci.buildnumber>
        <git.branch>GIT_BRANCH</git.branch>
        <git.commit>GIT_COMMIT</git.commit>
        <maven.build.timestamp.format>yyyyMMddHHmmss</maven.build.timestamp.format>
        <current.version>${project.version}</current.version>
        <spring.version>5.0.2.RELEASE</spring.version>
        <hibernate.version>5.2.12.Final</hibernate.version>
        <postgresql.version>42.1.4.jre7</postgresql.version>
        <slf4j.version>1.7.25</slf4j.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
        <jacoco.version>0.7.9</jacoco.version>
        <junit.version>4.12</junit.version>
        <junit.jupiter.version>5.0.1</junit.jupiter.version>
        <junit.vintage.version>${junit.version}.1</junit.vintage.version>
        <junit.platform.version>1.0.1</junit.platform.version>
        <nexus.url>http://ftct0035g.ftc.hpeswlab.net/nexus</nexus.url>
        <repo.url>${nexus.url}/content/groups/public</repo.url>
        <commons.dbcp2.version>2.1.1</commons.dbcp2.version>
        <commons.pool2.version>2.4.2</commons.pool2.version>
        <flyway.version>4.2.0</flyway.version>
        <jackson.version>2.9.2</jackson.version>
        <commons.io.version>2.5</commons.io.version>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <spring.security.version>5.0.0.RELEASE</spring.security.version>
        <typeconfig.version>1.3.0</typeconfig.version>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.7.0</version>
                <configuration>
                    <source>${maven.compiler.source}</source>
                    <target>${maven.compiler.source}</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>${jacoco.version}</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>report</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.19.1</version>
                <configuration>
                    <includes>
                        <include>**/Test*.java</include>
                        <include>**/*Test.java</include>
                        <include>**/*Tests.java</include>
                        <include>**/*TestCase.java</include>
                    </includes>
                    <properties>
                        <!-- <includeTags>fast</includeTags> -->
                        <excludeTags>slow</excludeTags>
                        <!--
                        <configurationParameters>
                            junit.jupiter.conditions.deactivate = *
                        </configurationParameters>
                        -->
                    </properties>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.apache.maven.surefire</groupId>
                        <artifactId>surefire-junit47</artifactId>
                        <version>2.19.1</version>
                    </dependency>
                </dependencies>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <warName>openl10n</warName>
                    <archive>
                        <manifestEntries>
                            <Specification-Title>${project.name}</Specification-Title>
                            <Specification-Version>${project.version}</Specification-Version>
                            <Implementation-BuildNumber>${ci.buildnumber}</Implementation-BuildNumber>
                            <Implementation-Timestamp>${maven.build.timestamp}</Implementation-Timestamp>
                            <Implementation-GitCommitHash>${git.commit}</Implementation-GitCommitHash>
                            <Implementation-GitBranch>${git.branch}</Implementation-GitBranch>
                        </manifestEntries>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>

        <dependency>
            <groupId>com.hp.cloud.identity</groupId>
            <artifactId>idm-client-core</artifactId>
            <version>1.10.8</version>
        </dependency>
        <!-- Spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-core</artifactId>
            <version>${spring.security.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
            <version>${spring.security.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
            <version>${spring.security.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <!-- Hibernate -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>${hibernate.version}</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>${hibernate.version}</version>
        </dependency>

        <dependency>
            <groupId>org.flywaydb</groupId>
            <artifactId>flyway-core</artifactId>
            <version>${flyway.version}</version>
        </dependency>

        <!-- Database driver-->
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>${postgresql.version}</version>
        </dependency>
        <dependency>
            <groupId>org.hsqldb</groupId>
            <artifactId>hsqldb</artifactId>
            <version>2.2.9</version>
        </dependency>
        <!-- Apache Commons DBCP -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-dbcp2</artifactId>
            <version>${commons.dbcp2.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
            <version>${commons.pool2.version}</version>
        </dependency>


        <!-- AspectJ -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.7.4</version>
        </dependency>
        <!-- JSTL -->
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <!-- @Inject -->
        <dependency>
            <groupId>javax.inject</groupId>
            <artifactId>javax.inject</artifactId>
            <version>1</version>
        </dependency>

        <!-- Servlet -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.1</version>
            <scope>provided</scope>
        </dependency>
        <!-- Logging -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>apache-log4j-extras</artifactId>
            <version>1.1</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.hamcrest</groupId>
                    <artifactId>hamcrest-core</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apiguardian</groupId>
            <artifactId>apiguardian-api</artifactId>
            <version>1.0.0</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>${jackson.version}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>${jackson.version}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>${jackson.version}</version>
        </dependency>

        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>${commons.io.version}</version>
        </dependency>

        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.1</version>
        </dependency>
        <dependency>
            <groupId>org.ostermiller</groupId>
            <artifactId>utils</artifactId>
            <version>1.07.00</version>
        </dependency>
        <dependency>
            <groupId>org.owasp.antisamy</groupId>
            <artifactId>antisamy</artifactId>
            <version>1.5.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-text</artifactId>
            <version>1.2</version>
        </dependency>

        <dependency>
            <groupId>com.typesafe</groupId>
            <artifactId>config</artifactId>
            <version>${typeconfig.version}</version>
        </dependency>
        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-all</artifactId>
            <version>1.3</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-all</artifactId>
            <version>1.10.19</version>
            <exclusions>
                <exclusion>
                    <groupId>org.hamcrest</groupId>
                    <artifactId>hamcrest-core</artifactId>
                </exclusion>
            </exclusions>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.jayway.jsonpath</groupId>
            <artifactId>json-path-assert</artifactId>
            <version>2.2.0</version>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.hamcrest</groupId>
                    <artifactId>hamcrest-core</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-api</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.geronimo.specs</groupId>
            <artifactId>geronimo-servlet_3.0_spec</artifactId>
            <version>1.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20171018</version>
        </dependency>
    </dependencies>

</project>

2. JenkinsControllerTest

package com.microfocus.g11n.openl10n.controller;

import com.microfocus.g11n.openl10n.model.dto.JenkinsRequestBody;
import com.microfocus.g11n.openl10n.model.dto.PassoloTaskDTO;
import com.microfocus.g11n.openl10n.service.JenkinsService;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

/**
 * Created by David Gong on 2018/3/16.
 */

public class JenkinsControllerTest {

    @Mock
    JenkinsService jenkinsService;

    @InjectMocks
    private JenkinsController jenkinsController;

    private MockMvc mockMvc;

    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);
        mockMvc = MockMvcBuilders.standaloneSetup(jenkinsController).build();
    }

    @After
    public void tearDown() throws Exception {

    }

    @Test
    public void testGetAllTask() throws Exception {
        List<PassoloTaskDTO> passoloTaskDTOList = new ArrayList<PassoloTaskDTO>();
        when(jenkinsService.getAllPassoloTask()).thenReturn(passoloTaskDTOList);
        mockMvc.perform(get("/jenkins/getAllTask"))
                .andExpect(status().isOk())
                .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
                .andExpect(jsonPath("result", is("Succeed")))
                .andExpect(jsonPath("data", hasSize(0)));
    }

    @Test
    public void testDownloadSourceCsvZipFile() throws Exception {
        ClassLoader classLoader = getClass().getClassLoader();
        URL url = classLoader.getResource("hibernate.cfg.xml");
        Assert.assertNotNull(url);
        File file = new File(url.getFile());
        when(jenkinsService.getSourceZipFilePath(any(String.class), any(String.class), any(String.class))).thenReturn(file);
        ResultActions ra = mockMvc.perform(get("/jenkins/downloadSourceCsvZipFile").param("userEmail", "userEmail").param("productName", "productName").param("version", "version"));
        ra.andExpect(status().isOk());
        Assert.assertTrue(ra.andReturn().getResponse().getContentAsString().length() > 0);

    }

    @Test
    public void testUploadLpZipFile() throws Exception {
        doNothing().when(jenkinsService).saveLpZipFile(any(String[].class), any(MultipartFile.class));
        MockMultipartFile lpZip = new MockMultipartFile("file", "lp.zip", MediaType.MULTIPART_FORM_DATA_VALUE, "fileContent".getBytes());
        mockMvc.perform(multipart("/jenkins/uploadLpZipFile").file(lpZip).param("userEmail", "userEmail").param("productName", "productName").param("version", "version"))
                .andExpect(status().isOk())
                .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
                .andExpect(jsonPath("result", is("Succeed")))
                .andExpect(jsonPath("data").isEmpty());
    }

    @Test
    public void testUpdateTaskStatus() throws Exception {
        when(jenkinsService.updateTaskStatus(any(String.class), any(String.class), any(String.class))).thenReturn(1);
        mockMvc.perform(put("/jenkins/updateTaskStatus").contentType(MediaType.APPLICATION_JSON_UTF8).content("{}"))
                .andExpect(status().isOk())
                .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
                .andExpect(jsonPath("result", is("Succeed")))
                .andExpect(jsonPath("data").isEmpty());
    }

    @Test
    public void testAddNewRelease() throws Exception {
        when(jenkinsService.getLanguageIds(any(JenkinsRequestBody.class))).thenReturn(new ArrayList<String>());
        doNothing().when(jenkinsService).createProject(any(String.class), any(String.class), any(ArrayList.class), any(ArrayList.class));
        mockMvc.perform(post("/jenkins/addNewRelease").contentType(MediaType.APPLICATION_JSON_UTF8).content("{}"))
                .andExpect(status().isOk())
                .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
                .andExpect(jsonPath("result", is("Succeed")))
                .andExpect(jsonPath("data").isEmpty());
    }

    @Test
    public void testAddNewLanguage() throws Exception {
        when(jenkinsService.getLanguageIds(any(JenkinsRequestBody.class))).thenReturn(new ArrayList<String>());
        when(jenkinsService.getProjectIdByProductNameAndVersion(any(JenkinsRequestBody.class))).thenReturn(new String());
        doNothing().when(jenkinsService).updateProjectLanguage(any(String.class), any(ArrayList.class));
        mockMvc.perform(post("/jenkins/addNewLanguage").contentType(MediaType.APPLICATION_JSON_UTF8).content("{}"))
                .andExpect(status().isOk())
                .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
                .andExpect(jsonPath("result", is("Succeed")))
                .andExpect(jsonPath("data").isEmpty());
    }

    @Test
    public void testUploadPrjDataCsvZipFile() throws Exception {
        when(jenkinsService.saveProjDataCsvZipFile(any(String[].class), any(MultipartFile.class))).thenReturn(new String());
        when(jenkinsService.unzipSourceCsvZipFile(any(String[].class), any(String.class))).thenReturn(new String());
        doNothing().when(jenkinsService).convertFileName(any(String.class));
        MockMultipartFile sourceZip = new MockMultipartFile("file", "SMA-X_2018.02_fr.zip", MediaType.MULTIPART_FORM_DATA_VALUE, "fileContent".getBytes());
        mockMvc.perform(multipart("/jenkins/uploadPrjDataCsvZipFile").file(sourceZip).param("productName", "productName").param("version", "version"))
                .andExpect(status().isOk())
                .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
                .andExpect(jsonPath("result", is("Succeed")))
                .andExpect(jsonPath("data").isEmpty());
    }
}

3. LpStatusRepositoryTest

package com.microfocus.g11n.openl10n.repository;

import com.hp.ccue.identity.user.idm.domain.IdmUser;
import com.microfocus.g11n.openl10n.constants.GlobalConstants;
import com.microfocus.g11n.openl10n.model.entity.LpStatus;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.junit.*;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

import static org.junit.Assert.*;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.times;

public class LpStatusRepositoryTest {

    private static final Logger log = LoggerFactory.getLogger(LpStatusRepositoryTest.class);

    private static LpStatusRepository lpStatusRepository;

    private static SessionFactory sessionFactory;

    @Mock
    private HttpServletRequest httpServletRequest;

    @Mock
    private HttpServletResponse httpServletResponse;

    @Mock
    private HttpSession httpSession;

    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
        if (sessionFactory == null) {
            StandardServiceRegistry standardRegistry = new StandardServiceRegistryBuilder().configure().build();
            Metadata metaData = new MetadataSources(standardRegistry).getMetadataBuilder().build();
            sessionFactory = metaData.getSessionFactoryBuilder().build();
        }
        lpStatusRepository = new LpStatusRepository();
        lpStatusRepository.sessionFactory = sessionFactory;
    }

    @AfterClass
    public static void tearDownAfterClass() throws Exception {
        lpStatusRepository = null;
        sessionFactory.close();
    }

    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);
        IdmUser idmUser = new IdmUser();
        idmUser.setUsername("forTest");
        Mockito.when(httpSession.getAttribute(GlobalConstants.CURRENT_USER)).thenReturn(idmUser);
        Mockito.when(httpServletRequest.getSession(false)).thenReturn(httpSession);
        ServletRequestAttributes attrs = new ServletRequestAttributes(httpServletRequest, httpServletResponse);
        RequestContextHolder.setRequestAttributes(attrs);
    }

    @After
    public void tearDown() throws Exception {
    }

    @Test
    public void updateLpStatus() {
        log.debug("Start........ updateLpStatus()");
        Session session = getSession();
        List<LpStatus> lpStatusList = session.createQuery("select lps from LpStatus lps").list();
        if (lpStatusList.isEmpty()) {
            Assert.assertFalse("No any records in the table of openl10n_lp_status", lpStatusList.isEmpty());
        } else {
            LpStatus lpStatus = lpStatusList.get(0);
            try {
                lpStatusRepository.updateLpStatus(lpStatus.getUserEmail(), lpStatus.getProductName(), lpStatus.getVersion(), lpStatus.getStatus(), lpStatus.getLanguageCode(), lpStatus.getTargetZipName());
            } catch (Exception e) {
                Assert.assertNull(e);
                e.printStackTrace();
            } finally {
                closeSession(session);
            }
        }
        log.debug("End........ updateLpStatus()");
    }

    @Test
    public void updateLpStatus1() {
        log.debug("Start........ updateLpStatus1()");
        Session session = getSession();
        List<LpStatus> lpStatusList = session.createQuery("select lps from LpStatus lps").list();
        if (lpStatusList.isEmpty()) {
            Assert.assertFalse("No any records in the table of openl10n_lp_status", lpStatusList.isEmpty());
        } else {
            LpStatus lpStatus = lpStatusList.get(0);
            try {
                lpStatusRepository.updateLpStatus(lpStatus.getUserEmail(), lpStatus.getProductName(), lpStatus.getVersion(), lpStatus.getStatus(), lpStatus.getMessage(), lpStatus.getLanguageCode(), lpStatus.getTargetZipName());
            } catch (Exception e) {
                Assert.assertNull(e);
                e.printStackTrace();
            } finally {
                closeSession(session);
            }
        }
        log.debug("End........ updateLpStatus1()");
    }

    @Test
    public void getLpStatus() {
        log.debug("Start........ getLpStatus()");
        Session session = getSession();
        List<LpStatus> lpStatusList = session.createQuery("select lps from LpStatus lps").list();
        if (lpStatusList.isEmpty()) {
            Assert.assertFalse("No any records in the table of openl10n_lp_status", lpStatusList.isEmpty());
        } else {
            LpStatus lpStatus = lpStatusList.get(0);
            try {
                LpStatus resultObject = lpStatusRepository.getLpStatus(lpStatus.getUserEmail(), lpStatus.getProductName(), lpStatus.getVersion());
                Assert.assertTrue(lpStatus.getUserEmail().equals(resultObject.getUserEmail()));
                Assert.assertTrue(lpStatus.getProductName().equals(resultObject.getProductName()));
                Assert.assertTrue(lpStatus.getVersion().equals(resultObject.getVersion()));
            } catch (Exception e) {
                Assert.assertNull(e);
                e.printStackTrace();
            } finally {
                closeSession(session);
            }
        }
        log.debug("End........ getLpStatus()");
    }

    @Test
    public void getLpStatusList4User() {
        log.debug("Start........ getLpStatusList4User()");
        Session session = getSession();
        List<LpStatus> lpStatusList = session.createQuery("select lps from LpStatus lps").list();
        if (lpStatusList.isEmpty()) {
            Assert.assertFalse("No any records in the table of openl10n_lp_status", lpStatusList.isEmpty());
        } else {
            LpStatus lpStatus = lpStatusList.get(0);
            try {
                List<LpStatus> resultList = lpStatusRepository.getLpStatusList4User(lpStatus.getUserEmail());
                Assert.assertTrue(!resultList.isEmpty());
            } catch (Exception e) {
                Assert.assertNull(e);
                e.printStackTrace();
            } finally {
                closeSession(session);
            }
        }
        log.debug("End........ getLpStatusList4User()");
    }

    private Session getSession() {
        return sessionFactory.openSession();
    }

    private void closeSession(Session session) {
        if (session != null) {
            session.close();
        }
    }
}

4.JsonPath的网址

jsonPath帮助

猜你喜欢

转载自blog.csdn.net/qq_38844636/article/details/79626448