False "Could not autowire" error in IDEA 2019.2 (and IIRC 2019.1 and earlier versions too)

Answered

I have reported this in YouTrack too, but thought there might be a workaround available so I am posting here too.

I uploaded a sample project (resolution-error.zip) to https://uploads.services.jetbrains.com/ but I am not sure if it got uploaded successfully as it shows these success/error message combo:

Uploading...
Error #-200: HTTP Error.
Upload complete!

Anyway, I have defined a meta annotation like this in a common module:

package org.behrang.bugreport;

import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
* Marker package for component scan.
*
* @see SpringBootApplication#scanBasePackageClasses()
*/
public interface BugReportPackage {
}
package org.behrang.bugreport.api.common.annotations;

import org.behrang.bugreport.BugReportPackage;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.lang.annotation.Documented;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootApplication(scanBasePackageClasses = BugReportPackage.class)
public @interface BugReportApplication {
}

In a module named foo that depends on this common module, I have defined an application entry point:

package org.behrang.bugreport.foo;

import org.behrang.bugreport.api.common.annotations.BugReportApplication;
import org.springframework.boot.SpringApplication;

@BugReportApplication
public class Application {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

There's also a service defined in this module:

package org.behrang.bugreport.foo.service;

import org.springframework.stereotype.Service;

@Service
public class FooService {

public void foo() {

}
}

And this test:

package org.behrang.bugreport.foo.service;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;

import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = RANDOM_PORT)
@ActiveProfiles("test")
public class FooServiceSmokeTest {

@Autowired
private FooService fooService;

@Test
public void shouldAutowireFooService() {
assertThat(fooService).isNotNull();
}
}

The test passes successfully both from CLI and from within IDEA, but IDEA marks:

@Autowired
private FooService fooService;

with an error:

I think this happens because:

1. The BugReportApplication meta annotation and BugReportPackage marker interface are defined in the common module

2. The application uses the BugReportApplication meta annotation instead of SpringBootApplication

Right now I have disabled the inspection to suppress the error, but are there any workarounds to make IDEA to properly parse this project and understand that there actually is a bean that can be autowired?

1 comment
Comment actions Permalink

Helo, 

The issue is being reviewed in the scope of the following YouTrack item: 

https://youtrack.jetbrains.com/issue/IDEA-219248

0

Please sign in to leave a comment.