Hi, My first post on kotlinlang so kindly pardon...
# announcements
o
Hi, My first post on kotlinlang so kindly pardon me if I’m missing any expected culture. I’m trying to use Kotlin with CDI (Weld). I have some CDI beans (and EJBs) I’m spying on with Mockito and verifying the state of their properties (which could also be CDI beans). Normally in Java I would use a
package-private
access modifier and but the test in the same package (albeit in a different directory hierarchy per Maven). Since Kotlin doesn’t have
package-private
, I tried its
public
(default) but Weld doesn’t like CDI beans with
public
fields. Another thing was to try
internal
but since the field had to be
lateinit
so Kotlin knows CDI is going to inject it before use in production, the field ended up being compiled to public again and Weld complains. I’ve seen this discussion (https://discuss.kotlinlang.org/t/automatic-visibility-relaxation-for-unit-tests/489/2) and this issue (https://youtrack.jetbrains.com/issue/KT-525) but it seems unattended. I can’t use
protected
because that would mean the test has to extend the SUT which doesn’t make sense. I also can’t put the test in the same file as the SUT and use
private
as that would mean the test will be deployed to production. Here is what I currently have:
Copy code
@Inject
@Config @Key(“config.from.db")
internal open lateinit var apiKey: String
Copy code
[ERROR] Caused by: org.jboss.weld.exceptions.DefinitionException: WELD-000075: Normal scoped managed bean implementation class has a public field:  [EnhancedAnnotatedFieldImpl] @Inject @Key @Config public com.company.project.OurClass.apiKey"
Here is what I would normally have done in Java
Copy code
@Inject
@Config @Key(“config.from.db”)
@VisibleForTesting
String apiKey;
How do I make this work?