Home > Software engineering >  IntelliJ, Junit 4 Maven - Cannot resolve symbol assertEquals
IntelliJ, Junit 4 Maven - Cannot resolve symbol assertEquals

Time:01-19

Maven JUnit 4.12 dependency is there. Doesn't work with both static and without it. assertEquals is in red/not resolved. Assert. does not show methods.

Tried by adding assertJ library too as dependency. Same issue. In code, I do Assertions.. It doesn't show any methods.

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>

CodePudding user response:

The issue isn't the import of Assert (which is fine), but the fact you have the call to Assert.assertEquals outside of any method - and you can't have such a statement directly under a class. Move it up into requestUsZipExtractPlaceName, and you should be fine:

@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
  • Related