0
votes

I'm attempting to run a simple junit test from gradle on an proof of concept class I've compiled and deployed to aws lambda. The class Auth.java takes a POJO with a single property email. It compiles fine on it's own and works on aws. However, when I run:

gradle build

I receive the following error:

Auth.java:6: error: cannot find symbol
System.out.println(payload.email);
symbol:   variable email
location: variable payload of type Object
1 error
:compileJava FAILED

If I System.out.println(payload.email); from within TestAuth.java I can access the object's property no problem. Am I failing to grasp some key java or gradle concept here? I've refactored this things 5 different ways and still, I always receive this cannot find symbol error. Anybody have any ideas? Here's my simplified code.


./src/main/java/Auth.java

package works.valt.api;

public class Auth {

    public Auth(Object payload) {
        System.out.println(payload.email);
    }

}

./src/test/java/TestAuth.java

import org.junit.Test;
import org.junit.Before;
import static org.junit.Assert.assertEquals;
import works.valt.api.Auth;

public class TestAuth {

    public class Payload {
      public String email = "[email protected]";
    }

    private Payload payload;

    // create payload
    @Before
    public void setUp() {
      this.payload = new Payload();
    }    

   @Test
   public void testResponse() {
      Auth response = new Auth(this.payload);
   }

}

build.gradle

apply plugin: 'java'

repositories {
    mavenCentral()
}

dependencies {
    testCompile 'junit:junit:4.12'
}

test {
    testLogging.showStandardStreams = true
}

task buildZip(type: Zip) {
    from compileJava
    from processResources              
    into('lib') {
        from configurations.runtime
    }           
}

build.dependsOn buildZip
3

3 Answers

1
votes

Constructor of Auth is having a param of type Object. And Object class doesn't have variable called email. Change the parameter type to Payload

1
votes

This is the constructor of Auth

public Auth(Object payload) {
    System.out.println(payload.email);
}

The payload parameter has type Object, so the field email is not found the in the Object class definition, and that's what the compiler is complaining about:

Auth.java:6: error: cannot find symbol
System.out.println(payload.email);
symbol:   variable email
location:
variable payload of type Object

You either change the parameter type to Payload or you cast the reference inside the constructor and loose some compile-time type safety

0
votes

The problem is payload is an Object and not Payload

You will have to actually say that it's a Payload in order to access that property:

package works.valt.api;

public class Auth {

    public Auth(TestAuth.Payload payload) {
        System.out.println(payload.email);
    }

}