java IntelliJ、Junit 4 Maven -无法解析符号Assert等于

2admgd59  于 2023-01-19  发布在  Java
关注(0)|答案(1)|浏览(225)

存在Maven JUnit 4.12依赖项。无论是否使用静态依赖项,都无法正常工作。assertEquals显示为红色/未解析。Assert.未显示方法。
尝试添加assertJ库作为依赖项。同样的问题。在代码中,我做Assertions.。它没有显示任何方法。

package examples;

import io.restassured.builder.RequestSpecBuilder;
import io.restassured.builder.ResponseSpecBuilder;
import io.restassured.http.ContentType;
import io.restassured.specification.RequestSpecification;
import io.restassured.specification.ResponseSpecification;
import org.junit.Assert;
import static org.junit.Assert.*;
import org.junit.BeforeClass;
import org.junit.Test;

import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;

public class Ch4Test {
    private static RequestSpecification requestSpec;
    @BeforeClass
    public static void createRequestSpec() {

        requestSpec = new RequestSpecBuilder().
                setBaseUri("http://zippopotam.us").build();
    }

    private static ResponseSpecification responseSpec;

    @BeforeClass
    public static void createResponseSpec(){
        responseSpec = new ResponseSpecBuilder().
                expectStatusCode(200).
                expectContentType(ContentType.JSON).
                build();
    }

    @Test
    public void requestUsZip(){

        given().
                spec(requestSpec).
                when().
                get("us/90210").
                then().
                spec(responseSpec).
                and().
                assertThat().
                body("places[0].'place name'", equalTo("Beverly Hills"));
    }

    @Test
    public void requestUsZipExtractPlaceName(){

        String placeName =

                given().
                spec(requestSpec).
                when().
                get("us/90210").
                then().
               extract().
                path("places[0].'place name'");
    }

   // Assert.assertEquals("Beverly Hills", placeName);

}
<?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>org.example</groupId>
    <artifactId>RestAssuredLearn</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>
    <dependencies>
        <!-- https://mvnrepository.com/artifact/io.rest-assured/rest-assured -->
        <dependency>
            <groupId>io.rest-assured</groupId>
            <artifactId>rest-assured</artifactId>
            <version>5.3.0</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/junit/junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.tngtech.java/junit-dataprovider -->
        <dependency>
            <groupId>com.tngtech.java</groupId>
            <artifactId>junit-dataprovider</artifactId>
            <version>1.13.1</version>
            <scope>test</scope>
        </dependency>

    </dependencies>

</project>
<dependency>
    <groupId>org.assertj</groupId>
    <artifactId>assertj-core</artifactId>
    <version>3.23.1</version>
    <scope>test</scope>
</dependency>
nnsrf1az

nnsrf1az1#

问题不在于Assert的导入(这很好),而在于你在任何方法之外调用Assert.assertEquals--你不能直接在类下有这样的语句,把它移到requestUsZipExtractPlaceName中,你应该没问题:

@Test
public void requestUsZipExtractPlaceName(){

    String placeName =
            given().
            spec(requestSpec).
            when().
            get("us/90210").
            then().
            extract().
            path("places[0].'place name'");

    Assert.assertEquals("Beverly Hills", placeName); // Move here, inside the method
}

// Instead of here

相关问题