How to Autowire repository interfaces in Components in Spring Boot - java

I am using Spring + Mysql and I can Autowire successfully my classes that extended from PagingAndSortingRepository<T,E> in my RepositoryRestController class.
I can autowire the my repositories in controller below.
package com.fallavi.api.user.controller;
import com.fallavi.api.MyConfig;
import com.fallavi.api.purchase.model.Purchase;
import com.fallavi.api.purchase.repository.PurchaseRepository;
import com.fallavi.api.reader.model.Reader;
import com.fallavi.api.reader.repository.ReaderRepository;
import com.fallavi.api.user.calculator.UserCreditHelper;
import com.fallavi.api.user.exceptions.UserCanNotFindException;
import com.fallavi.api.user.model.UserCreditEnoughModel;
import com.fallavi.api.user.model.UserCreditModel;
import com.fallavi.api.user.repository.UsersRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler;
import org.springframework.data.rest.webmvc.RepositoryRestController;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
#RepositoryRestController
#RequestMapping("/user")
public class UserCreditController {
#Autowired
private ReaderRepository readerRepository;
#Autowired
private UsersRepository usersRepository;
#Autowired
private PurchaseRepository purchaseRepository;
#GetMapping(
value = "/userHasCreditEnough/{reader_id}",
headers = "Content-Type=application/json")
public ResponseEntity<UserCreditEnoughModel> userHasCreditEnough(
#RequestHeader(value = "Authorization") String token,
#PathVariable Long reader_id) {
UserCreditHelper userCreditHelper = new UserCreditHelper();
// Find user ID from authorization code START //
Long userID = usersRepository.getUserIDByAuthToken(token);
if (userID == null) {
throw new UserCanNotFindException();
}
// Find user ID from authorization code END //
// Find user credit START //
List<Purchase> purchaseList = this.purchaseRepository.findByUserID(userID);
Integer userCreditLeft = userCreditHelper.userCreditLeft(purchaseList);
// Find user credit END //
// Find user's credit left for Reader START //
Reader reader = this.readerRepository.findByReaderID(reader_id);
boolean isUserCreditEnough = userCreditHelper.userHasCreditEnough(userCreditLeft, reader);
// Find user's credit left for Reader END //
return ResponseEntity.ok(new UserCreditEnoughModel(isUserCreditEnough, reader.getOnline()));
}
}
Sure I want to seperate all layers as service, repository and Controller it is why I creating a new helper class that is service layer.
#Service
public class UserCreditHelper {
#Autowired
UsersRepository usersRepository;
....some methods...
}
In order to call UserCreditHelper class in I am using ApplicationContext in my controller class sample below.
#RepositoryRestController
#RequestMapping("/user")
public class UserCreditController {
#Autowired
private ReaderRepository readerRepository;
#Autowired
private UsersRepository usersRepository;
#Autowired
private PurchaseRepository purchaseRepository;
#GetMapping(
value = "/userHasCreditEnough/{reader_id}",
headers = "Content-Type=application/json")
public ResponseEntity<String> userHasCreditEnough(
#RequestHeader(value = "Authorization") String token) {
// com.fallavi.api.user.calculator is the package of UserCreditHelper.
ApplicationContext ctx = new AnnotationConfigApplicationContext( "com.fallavi.api.user.calculator" );
return ResponseEntity.ok("test");
}
}
When I tried to request /userHasCreditEnough/{reader_id} endpoint it gives error alltime.
"Error creating bean with name 'userCreditHelper': Unsatisfied dependency expressed through field 'usersRepository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.fallavi.api.user.repository.UsersRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true)}",

You should create an interface which will be your service layer. Then inject that interface into the controller and call the desire methods in the desire end point of the controller. From there call the implantation you want from this injected interface.

Related

Parameter 0 of constructor in 'Controller' required a bean of type 'service' that could not be found. Not working: adding annotations, componentscans

I'm trying to use Services and DTOs in my project (trying to learn what they are and how they should be used). First, I had only Controllers, Models and Repository. The backend worked as expected. Then, I thought I'd use DTOs and Services to interface the client with the repo. But my app did not work afterwards.
The project structure:
The MovieController:
package movie_API.movie.controllers;
import movie_API.movie.services.MovieService;
import movie_API.movie.services.beans.MovieDTO;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
#Controller
//#RequestMapping("/something") <- if you want a general something before the other resources
public class MovieController {
private final MovieService movieService;
public MovieController(MovieService movieService) {
this.movieService = movieService;
System.out.println("build controller");
}
#GetMapping("/movies")
public String movies(Model model) {
System.out.println("before getting in");
movieService.showMovies(model);
System.out.println("after getting in");
return "movie/movies-all";
}
}
The Movie Model + the corresponding constructors and getters and setters:
package movie_API.movie.models;
import javax.persistence.Entity;
import javax.persistence.Id;
#Entity
public class Movie {
private String id;
private String metascore;
private String title;
private String year;
private String description;
private String genreId;
The MovieRepository:
package movie_API.movie.repositories;
import movie_API.movie.models.Movie;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.ArrayList;
import java.util.List;
#Repository
public interface MovieRepository extends JpaRepository<Movie, String> {
default ArrayList<String> getGenreNames(List<Movie> movies, GenreRepository genreRepository) {
ArrayList<String> genreNames = new ArrayList<>();
String genreIdInMovie;
String genreName;
for (Movie m : movies) {
genreIdInMovie = m.getGenreId();
genreName = genreRepository.findById(genreIdInMovie).get().getName();
genreNames.add(genreName);
}
return genreNames;
}
}
The MovieService:
package movie_API.movie.services;
import movie_API.movie.models.Movie;
import movie_API.movie.repositories.GenreRepository;
import movie_API.movie.repositories.MovieRepository;
import movie_API.movie.services.beans.MovieDTO;
import org.jvnet.hk2.annotations.Service;
import org.springframework.ui.Model;
import java.util.List;
#Service
public class MovieService {
private final MovieRepository movieRepository;
private final GenreRepository genreRepository;
private List<Movie> movies;
private Movie movie;
public MovieService(MovieRepository movieRepository, GenreRepository genreRepository) {
this.movieRepository = movieRepository;
this.genreRepository = genreRepository;
}
public void showMovies(Model m) {
m.addAttribute("allMovies", getMovies());
m.addAttribute("allGenres", getGenreNames());
}
}
The MovieDTO + corresponding constructor and getters and setters:
package movie_API.movie.services.beans;
import java.io.Serializable;
public class MovieDTO implements Serializable {
private static final long serialVersionUID = -8040351309785589042L;
private String id;
private String metascore;
private String title;
private String year;
private String description;
private String genreId;
The MovieApplication:
package movie_API.movie;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class MovieApplication {
public static void main(String[] args) {
SpringApplication.run(MovieApplication.class, args);
}
}
The problem is that I receive an error saying that there is no bean called MovieService.
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2021-08-03 14:41:28.664 ERROR 3504 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of constructor in movie_API.movie.controllers.MovieController required a bean of type 'movie_API.movie.services.MovieService' that could not be found.
Action:
Consider defining a bean of type 'movie_API.movie.services.MovieService' in your configuration.
Process finished with exit code 1
After researching this matter, I found several solutions recommended:
Apparently, it is not reading the bean from the package, therefore I tried to specifically ask it to scan the component services by changing to #SpringBootApplication(scanBasePackages={"movie_API.movie.services"}). This worked in the sense that I didn't receive the "Failed to start" error anymore. But now the problem was that the Controller wasn't accessed anymore. All calls were returned by "404 Not found" and the prints I put in the Controller weren't called. I tried to put all packages in the list, but the "Failed to start" error returned.
Another solution was to double check whether the MovieRepository and the MovieService had the annotations #Repository and #Service. These were there initially as well, so it didn't help.
I also tried to make the MovieService in the MovieController to be outside the constructor and be autowired. Still the same problem (no bean).
You imported the wrong Service annotation in your MovieService class. The one you should import is: import org.springframework.stereotype.Service;
Step 1) Check the ServiceImpl class.
Step 2) Attach #Service to ServiceImpl class.
Step 3) Problem is solved now.

Implement Interface with custom method and JpaRepository

so I'm working on an API Rest, registration for students, using Domain Driven Design. So my problem is: I'm trying to add a custom method with the JpaRepository. My Project hierarchy is:
com.wizardry.witchcraft.domain.model.StudentModel;
com.wizardry.witchcraft.infraestructure.repository.CustomRepositoryImpl;
com.wizardry.witchcraft.domain.repository.IStudentRepository;
com.wizardry.witchcraft.domain.repository.ICustomRepository;
com.wizardry.witchcraft.api.controller.TesteController2;
com.wizardry.witchcraft.api.controller.StudentController;
So first I've created the method Implementation
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.springframework.stereotype.Repository;
import com.wizardry.witchcraft.domain.model.StudentModel;
import com.wizardry.witchcraft.domain.repository.ICustomRepository;
#Repository
public class CustomRepositoryImpl implements ICustomRepository {
#PersistenceContext
private EntityManager manager;
#Override
public List<StudentModel> findCustom (String name){
//METHOD
}
}
And the Interface:
import com.wizardry.witchcraft.domain.model.StudentModel;
public interface ICustomRepository {
List<StudentModel> findCustom(String name);
}
And to test it out I created a new Controller to don't mess with my working one, TesteController2, and it worked fine. So my next step was extended the ICustomRepository in the IStudentRepository, made the changes in TesteController2 and then Spring won't find my findCustom method anymore, It tries to create the method as a JPA keyword and return and error. This is my repository interface:
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import com.wizardry.witchcraft.domain.model.StudentModel;
#Repository
public interface IStudentRepository extends ICustomRepository, JpaRepository<StudentModel, Long> {
List<StudentModel> queryByName(String name, #Param ("id") Long school);
List<StudentModel> queryFirstByNameContaining(String name);
List<StudentModel> queryTop2ByNameContaining(String name);
int countByWizardingSchoolModelId(Long school);
}
And the TesteController2:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import com.wizardry.witchcraft.domain.model.StudentModel;
import com.wizardry.witchcraft.domain.repository.IStudentRepository;
#RestController
#RequestMapping(value = "/test")
public class TesteController2 {
#Autowired
private IStudentRepository iStudentRepository;
#ResponseStatus(HttpStatus.ACCEPTED)
#GetMapping
public List<StudentModel> findCustom2(String name) {
return iStudentRepository.findCustom(name);
}
}
PS: I have a Service Layer com.wizardry.witchcraft.domain.service.RegisterStudentService however
the method in question does not go through it(yet!) because I'm testing, I tried to pass by the service and see what happen but give the same ERROR.
*
ERROR: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'studentController': Unsatisfied dependency expressed through field 'registerStudentService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'registerStudentService': Unsatisfied dependency expressed through field 'iStudentRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'IStudentRepository' defined in com.wizardry.witchcraft.domain.repository.IStudentRepository defined in #EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.List com.wizardry.witchcraft.domain.repository.ICustomRepository.findCustom(java.lang.String)! No property findCustom found for type StudentModel!
*
Thanks in Advance! I'm really lost here.
Just rename your class CustomRepositoryImpl to IStudentRepositoryImpl and it should work.
#Repository
public class IStudentRepositoryImpl implements ICustomRepository {
#PersistenceContext
private EntityManager manager;
#Override
public List<StudentModel> findCustom (String name){
//METHOD
}
}
Below is the documentation from Spring
Configuration If you use namespace configuration, the repository
infrastructure tries to autodetect custom implementations by scanning
for classes below the package we found a repository in. These classes
need to follow the naming convention of appending the namespace
element's attribute repository-impl-postfix to the found repository
interface name. This postfix defaults to Impl.
https://docs.spring.io/spring-data/data-commons/docs/current-SNAPSHOT/reference/html/#repositories.custom-implementations

NullPointerException: null while trying to do a MongoDB FindByID

So i'm trying to connect java spring to mongoDB and using a findById, but it always gives the null pointer error.
2020-08-04 13:54:01.893 ERROR 8312 --- [nio-8080-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
java.lang.NullPointerException: null
at pt.project.ProvaConceito_BackEnd.mongoDB.UserService.findById(UserService.java:29) ~[classes/:na]
at pt.project.ProvaConceito_BackEnd.mongoDB.mongoDBService.getUserByID(mongoDBService.java:19) ~[classes/:na]
The structure of this project is:
Java
MongoDB
mongoDBService
UserService
Pojos
User
Repositories
UserRepository
I'm gonna share the code I have right now:
mongoDBService
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import pt.project.ProvaConceito_BackEnd.pojos.User;
#RestController
#CrossOrigin(origins="http://localhost:4200")
public class mongoDBService {
UserService userService = new UserService();
#RequestMapping("/concept/user")
public User getUserByID(Integer id) {
return userService.findById(1);
}
}
UserService
package pt.project.ProvaConceito_BackEnd.mongoDB;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pt.project.ProvaConceito_BackEnd.pojos.User;
import pt.project.ProvaConceito_BackEnd.repositories.UserRepository;
import java.util.List;
#Service
public class UserService {
#Autowired(required = false)
private UserRepository userRepository;
public void save(String nome, int idade, String morada) {
userRepository.save(new User(nome, idade, morada));
}
public List<User> findAll() {
return userRepository.findAll();
}
public long count() {
return userRepository.count();
}
public User findById(Integer id) {
return userRepository.findById(id).orElse(null);
}
public void delete(Integer id) {
userRepository.deleteById(id);
}
}
Users (Pojo)
package pt.project.ProvaConceito_BackEnd.pojos;
import lombok.Getter;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
#Document(collection = "Users")
#Getter
public class User {
#Id
private Integer id;
private String nome;
private int idade;
private String morada;
public User(String nome, int idade, String morada) {
this.nome = nome;
this.idade = idade;
this.morada = morada;
}
}
UserRepository
package pt.project.ProvaConceito_BackEnd.repositories;
import org.springframework.data.mongodb.repository.MongoRepository;
import pt.project.ProvaConceito_BackEnd.pojos.User;
public interface UserRepository extends MongoRepository<User, Integer> {
}
What am I doing wrong here? I think the problem is on mongoDBService...
in this line:
UserService userService = new UserService();
Because I think that it's not being injected, but I don't know how to solve that...
EDIT 1
I have my main class inside pt.project.ProvaConceito_BackEnd:
ProvaConceitoBackEndApplication
package pt.project.ProvaConceito_BackEnd;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class ProvaConceitoBackEndApplication {
public static void main(String[] args) {
SpringApplication.run(ProvaConceitoBackEndApplication.class, args);
}
}
Do not use new to create object that way because spring will not have any knowledge of that object and will not be able to inject it.
Since you have already annotated the UserService with #service you should use #Autowired annotation
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import pt.project.ProvaConceito_BackEnd.pojos.User;
#RestController
#CrossOrigin(origins="http://localhost:4200")
public class mongoDBService {
#Autowired
private UserService userService;
#RequestMapping("/baieuropa/user")
public User getUserByID(Integer id) {
return userService.findById(1);
}
}
Also annotate the UserRepository class with #Repository
package pt.project.ProvaConceito_BackEnd.repositories;
import org.springframework.data.mongodb.repository.MongoRepository;
import pt.project.ProvaConceito_BackEnd.pojos.User;
#Repository
public interface UserRepository extends MongoRepository<User, Integer> {
}
Remove required = false from UserService class
Make sure to have a class with #SpringBootApplication annotation in pt.project.ProvaConceito_BackEnd package.
You need to update your repository and User.class to have an ID type of string instead of Integer. This is required for mongo repositories. May not completely solve your problem but will be a step in the right direction
#Repository
public interface UserRepository extends MongoRepository<User, String> {
}
I would follow the suggestions of other contributors to remove the "new" keyword from your mongoDBService and use the #Repository annotation too.
Autowiring only works if all components that it relies on are autowired too. So in your case because the mongoDBService uses new for the UserService then it expects the Repository to use "new" too. You should instead autowire at all levels and remove the required=false from the autowiring in the UserService.
The repository requires the #Repository annotation otherwise when Spring does its component scan the repository wont be picked up without it. #Repository #RestController etc are all just stereotypes for #Component with varying degrees of additional functionality

How do I make Autowired work inside a Configuration class?

I'm trying to autowire an attribute (myService) which is tagged as a #Service, inside a #Configuration class, but I get a NullPointer.
If instead, I autowire myService in non-configuration classes, I have no issues.
Here's the #Service I'm having issues autowiring:
package com.myapp.resources;
#Service
class MyService {
public List<String> getRoutingKeys() {
List<String> routingKeys;
//Do stuff
return routingKeys;
}
public String aMethod() {
return "hello";
}
}
Here's the #Configuration class where I can't autowire the Service
package com.myapp.messaging;
import com.myapp.resources;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.List;
#Configuration
public class RabbitConfiguration {
private List<String> routingKeys = writeRoutingKeys();
#Autowired
private MyService myService;
private List<String> writeRoutingKeys() {
boolean test = myService == null;
System.out.println("is the service null? " + test); //output: true!!!
return myService.getRoutingKeys(); //here I get a NullPointer
}
//Methods with bean declarations for RabbitMQ
}
If it helps, here's my mainclass:
package com.myapp;
import com.myapp.resources;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import java.util.List;
#SpringBootApplication
public class Application {
public static void main(String[] args) {
ConfigurableApplicationContext appContext = SpringApplication.run(Application.class, args);
MyService myService = (MyService) appContext.getBean(MyService.class);
boolean test = myService == null;
System.out.println("is the service null? " + test); //output: false
//Do stuff
}
}
If it helps, here's a different class (a #RestController) where I'm able to autowire the service
package com.myapp.resources;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
#RestController
public class MyController {
#Autowired
private MyService myService;
#GetMapping("/endpoint")
public String myRestMethod() {
boolean test = myService == null;
System.out.println("is the service null? " + test); //output: false
return myService.aMethod();
}
}
I've also tried adding the #ComponentScan in the Configuration class, but I still get a NullPointer
package com.myapp.messaging;
//list of imports...
#Configuration
#ComponentScan("com.myapp.demo")
public class RabbitConfiguration {
#Autowired
private MyService myService;
//...
}
Spring will only inject the dependencies after or when a bean is instantiated (Depending if constructor injection is used or not). However , you are now accessing the dependency MyService during the field initialisation which happens before initialising a bean .Hence , it cannot access MyService during field initialisation as it is not injected yet.
You can simply fix it by changing to use constructor injection and initialise routingKeys inside a constructor at the same time :
#Configuration
public class RabbitConfiguration {
private List<String> routingKeys ;
private MyService myService;
#Autowired
public RabbitConfiguration(MyService myService){
this.myService = myService
this.routingKeys = writeRoutingKeys();
}
private List<String> writeRoutingKeys() {
return myService.getRoutingKeys();
}
}
Or simply :
#Autowired
public RabbitConfiguration(MyService myService){
this.myService = myService
this.routingKeys = myService.getRoutingKeys();
}
I would suggest injecting the service through any #Bean creation method that needs it:
#Bean
public MyBean create(MyService myService)
and then pass the service into the writeRoutingKeys(MyService myService) method to process it accordingly.
Per documentation:
#Configuration classes are processed quite early during the
initialization of the context and forcing a dependency to be injected
this way may lead to unexpected early initialization. Whenever
possible, resort to parameter-based injection as in the example above.

Can't Autowire JpaRepository in Junit test - Spring boot application

Hi All
I’m trying to execute a Junit test in a Spring boot application, the Junit should test some CRUD operations, I’m using Spring Repositories specifically JpaRepository.
The Repository calss:
package au.com.bla.bla.bla.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import au.com.bla.bla.bla.entity.Todo;
public interface TodoRepository extends JpaRepository<Todo, Integer> {
}
TodoController class
package au.com.bla.bla.bla.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import au.com.bla.bla.bla.entity.Todo;
import au.com.bla.bla.bla.repository.TodoRepository;
import java.util.List;
import java.util.Map;
#RestController
#CrossOrigin
public class TodoController
{
static final String TEXT = "text";
#Autowired
private TodoRepository todoRepository;
...
#RequestMapping(path = "/todo", method = RequestMethod.POST)
public Todo create(#RequestBody Map<String, Object> body)
{
Todo todo = new Todo();
todo.setCompleted(Boolean.FALSE);
todo.setText(body.get(TEXT).toString());
todoRepository.save(todo);
return todo;
}
...
The JUnit:
package au.com.bla.bla.bla.controller;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.http.MediaType.APPLICATION_JSON;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import au.com.bla.bla.bla.repository.TodoRepository;
#WebMvcTest(TodoController.class)
#RunWith(SpringRunner.class)
public class TodoControllerTest {
#Autowired
private MockMvc mvc;
#Autowired
private TodoController subject;
#Before
public void setUp() {
}
#Test
public void testCreate() throws Exception {
String json = "{\"text\":\"a new todo\"}";
mvc.perform(post("/todo").content(json)
.contentType(APPLICATION_JSON)
.accept(APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.id").value(3))
.andExpect(jsonPath("$.text").value("a new todo"))
.andExpect(jsonPath("$.completed").value(false));
assertThat(subject.getTodos()).hasSize(4);
}
...
Problem:
When executing the Junit I end up with this exception:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'todoController': Unsatisfied dependency expressed through field 'todoRepository': No qualifying bean of type [au.com.bla.bla.bla.repository.TodoRepository] found for dependency [au.com.bla.bla.bla.repository.TodoRepository]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true)}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [au.com.bla.bla.bla.repository.TodoRepository] found for dependency [au.com.bla.bla.bla.repository.TodoRepository]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:569) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:349) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
…
Can anyone help with this ?
Thanks in advance
Your error is actually the expected behavior of #WebMvcTest.
You basically have 2 options to perform tests on your controller.
1. #WebMvcTest - need to use #MockBean
With #WebMvcTest, a minimal spring context is loaded, just enough to test the web controllers. This means that your Repository isn't available for injection:
Spring documentation:
#WebMvcTest will auto-configure the Spring MVC infrastructure and
limit scanned beans to #Controller, #ControllerAdvice, #JsonComponent,
Filter, WebMvcConfigurer and HandlerMethodArgumentResolver.
Assuming the goal is just to test the Controller, you should inject your repository as a mock using #MockBean.
You could have something like:
#RunWith(SpringRunner.class)
#WebMvcTest(TodoController.class)
public class TodoControllerTest {
#Autowired
private MockMvc mvc;
#Autowired
private TodoController subject;
#MockBean
private TodoRepository todoRepository;
#Test
public void testCreate() throws Exception {
String json = "{\"text\":\"a new todo\"}";
mvc.perform(post("/todo").content(json)
.contentType(APPLICATION_JSON)
.accept(APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.id").value(3))
.andExpect(jsonPath("$.text").value("a new todo"))
.andExpect(jsonPath("$.completed").value(false));
Mockito.verify(todoRepository, Mockito.times(1)).save(any(Todo.class));
}
}
2. #SpringBootTest - you can #Autowire beans
If you want to load the whole application context, then use #SpringBootTest: http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-testing.html
You'd have something like this:
#RunWith(SpringRunner.class)
#SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class TodoControllerTest {
private MockMvc mvc;
#Autowired
TodoController subject;
#Autowired
private WebApplicationContext context;
#Before
public void setup() {
this.mvc = MockMvcBuilders.webAppContextSetup(context).build();
}
#Test
public void testNoErrorSanityCheck() throws Exception {
String json = "{\"text\":\"a new todo\"}";
mvc.perform(post("/todo").content(json)
.contentType(APPLICATION_JSON)
.accept(APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.id").value(3))
.andExpect(jsonPath("$.text").value("a new todo"))
.andExpect(jsonPath("$.completed").value(false));
assertThat(subject.getTodos()).hasSize(4);
}
}

Categories

Resources