Reputation: 12149
I have created a simple unit test but IntelliJ is incorrectly highlighting it red. marking it as an error
No beans?
As you can see below it passes the test? So it must be Autowired?
Upvotes: 286
Views: 679920
Reputation: 1548
I had this exact error, however, it was not a test class, and it was because there was no constructor with constructor injection for the bean.
If you are using Lombok, you can use @RequiredArgsConstructor on the class which generates a constructor with 1 parameter for each field that requires special handling.
Upvotes: 0
Reputation: 63
IntelliJ Idea 2021.3.2 (Ultimate Edition)
I have such a service
import org.springframework.boot.info.BuildProperties;
@Service
public class MyVeryBestService {
private final BuildProperties buildProperties;
@Autowired
public MyVeryBestService(BuildProperties buildProperties) {
this.buildProperties = buildProperties;
}
//some other code
}
Application failed to start with this error
Parameter 0 of constructor in MyVeryBestService required a bean of type 'org.springframework.boot.info.BuildProperties' that could not be found
I execute maven goal clean and go to File -> Repair IDE... and it helps. I've tried Invalidate Caches... but it doesn't help.
Upvotes: 1
Reputation: 826
This and another kind of "Could not autowire. No Beans of ..." can be solved by installing the plugin, in my case was JobBuilderFactory from Spring Batch.
Upvotes: 5
Reputation: 1
i get the same error while i was using my first springbootapp but I solved with :
but if I removed it I got the same error again
Upvotes: 0
Reputation: 4415
I got this error out of Repository layer. I missed a simple @Component for the class. This could be a problem. Add this annotation @Component on top of the corresponding Implementation class
Upvotes: 3
Reputation: 31
It may happen because you didn't add spring web dependency so the IDE considers your application as Spring application not Spring boot
Upvotes: 1
Reputation: 2494
Try to use @ComponentScan
with basePackages
attribute to scan related bean like this :
@ComponentScan(basePackages={"com.xxxx.yyyy.reportdistributor"})
Upvotes: 0
Reputation: 67
Yes I know, seven year later but this can help to others. This is my Test class
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
public abstract class ApiTest
Everything's fine. but the problem was my folders, for example:
If you have the next structure -> main.java.com.api.service
So it's the same for the test -> test.java.com.api.service
Upvotes: 0
Reputation: 1
@ComponentScan(basePackages = "com.x.y") in your desired place. That's it.
Upvotes: 0
Reputation: 1199
Looks like you are trying to run your tests without actually activating your WebEnvironment.
The solution is quite simple, just add @SpringBootTest
annotation with a required config.
Here is an example:
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) // Run the server for testing.
public class mockTest {
@Autowired
// Should work now
}
Upvotes: 1
Reputation: 389
I have updated my Intellij IDEA version to 2022.2.3 like below:
IntelliJ IDEA 2022.2.3 (Ultimate Edition)
Build #IU-222.4345.14
Runtime version: 17.0.4.1+7-b469.62 aarch64
It solved my problem.
Upvotes: 0
Reputation: 36
As for intelliJ version 2021.2.3 (Ultimate Edition) I changed "incorrect injection point autowiring in spring bean compontents to warning which works for me
Upvotes: 1
Reputation: 2710
Add Spring annotation @Repository
over the repository class.
I know it should work without this annotation. But if you add this, IntelliJ will not show error.
@Repository
public interface YourRepository ...
...
If you use Spring Data with extending Repository
class it will be conflict packages. Then you must indicate packages directly.
import org.springframework.data.repository.Repository;
...
@org.springframework.stereotype.Repository
public interface YourRepository extends Repository<YourClass, Long> {
...
}
And next you can autowired your repository without errors.
@Autowired
YourRepository yourRepository;
It probably is not a good solution (I guess you are trying to register repository twice). But work for me and don't show errors.
Maybe in the new version of IntelliJ can be fixed: https://youtrack.jetbrains.com/issue/IDEA-137023
Upvotes: 103
Reputation: 583
I removed this code. no more errors
Other way to check: add the annotation @Service to your Repository class and it should work.
Upvotes: 1
Reputation: 141
I was having the issue. Just use
@SpringBootTest
@AutoConfigureMockMvc
annotation with the test class.
Upvotes: 1
Reputation: 11
I was having the same problem and solved it using "Invalidate Caches..." under the File menu.
Upvotes: 1
Reputation: 1387
As most synchronisation errors between IntelliJ (IDE) and development environments.
Specially if you have automated tests or build that pass green all the way through.
Invalidate Cache and Restart solved my problem.
Upvotes: 4
Reputation: 141
Have you checked that you have used @Service
annotation on top of your service implementation?
It worked for me.
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserServices {}
Upvotes: 13
Reputation: 450
add the annotation @Service
to your Repository class and it should work.
Upvotes: 1
Reputation: 4313
just add below two annotations to your POJO.
@ComponentScan
@Configuration
public class YourClass {
//TODO
}
Upvotes: 4
Reputation: 91
simple you have to do 2 steps
@Autowired
to @Resource
.==>> change @Autowired to @Resource
Upvotes: 8
Reputation: 131
Use @EnableAutoConfiguration
annotation with @Component
at class level. It will resolve this problem.
For example:
@Component
@EnableAutoConfiguration
public class ItemDataInitializer {
@Autowired
private ItemReactiveRepository itemReactiveRepository;
@Autowired
private MongoOperations mongoOperations;
}
Upvotes: 13
Reputation: 93
in my situation my class folder was in wrong address so check if your class is in correct package.
Upvotes: 4
Reputation: 639
IntelliJ IDEA Ultimate
Add your main class to IntelliJ Spring Application Context, for example Application.java
File
-> Project Structure..
left side: Project Setting -> Modules
right side: find in your package structure
Spring
and add +
Application.java
Upvotes: 4
Reputation: 21
I solved the problem by installing mybatis plugin in IDEA. When I installed Mybatis Plugin, it disappeared.
Upvotes: 0
Reputation: 136
in my Case, the Directory I was trying to @Autowired was not at the same level,
after setting it up at the same structure level, the error disappeared
hope it can helps some one!
Upvotes: 4
Reputation: 241
Check if you missed @Service annotation in your service class, that was the case for me.
Upvotes: 24