Accessing internal class from Java tests in the same module


#1

I have a module organized as such:

foo/
  src/
    main/
      java/
        com.org.pkg/
          ...(some java files)
      kotlin/
        com.org.pkg/
          Foo.kt
    test/
      java/
        com.org.pkg/
          FooTest.java

Where Foo.kt is:

package com.org.pkg

internal class Foo {
  fun bar() = "bar"
}

and FooTest.java is:

package com.org.pkg;

import org.testng.annotations.Test;

@Test
public class FooTest {
  @Test
  public void testBar() {
    Foo foo = new Foo();
  }
}

I’m getting errors at references to Foo on FooTest.java:

Usage of Kotlin internal declaration from different module

However, I believe it’s still in the same foo module, so why is this error happening?

The reason of why I’m doing this is I am currently trying to port some of my code from Java (inside main/java) to Kotlin (inside main/kotlin). Previously the Java classes has package-private modifiers.

Note that if I convert the FooTest.java into Kotlin (via IDEA convert functionality) the errors don’t happen, so it only happens when the test file is in Java.

Would really appreciate the help!


#2

Huh, after trying it again, the error seems to be only an IDEA inspection error, and the test runs fine. Is this expected and is there any way to fix this?


#3

Found it: Editor > Inspections > Kotlin > Java Interop Issues > Usage of Kotlin internal declaration from Java, and setting it to warning instead of error makes it happy. As I may still be naive, does anyone know what issues may arise from this?