0

I have a Controller, that is annotated with @RestController. For GET requests it is implemented like this:

@GetMapping
public Map<String, Object> getFilteredCars(
      @QuerydslPredicate(root = Car.class) Predicate predicate,
      @RequestParam(name = "page", defaultValue = "0") int page,
      @RequestParam(name = "size", defaultValue = "150") int size
) {
    return this.carService.getFilteredCars(
            predicate,
            PageRequest.of(page, size)
    );
}

The Controller works fine, I can query with different parameters and get a page with a defined size.

Now I want to test the controller.

@RunWith(MockitoJUnitRunner.class)
@SpringBootTest
class CarControllerTest {

@Mock
private CarService carService;

@InjectMock
private CarController carController;

@BeforeEach
void init(){
    RestAssured.baseURI = "http://localhost";
    RestAssured.port = 8810;
}

@Test
void givenUrl_whenSuccessOnGetCarsAndReturnsPage_thenCorrect() {

   when(this.CarService.getFilteredCars(any(),any()))
       .thenReturn(expectedResponse);

    given()
        .standaloneSetup(new CarController(this.carService))
    .when()
        .get("/cars?color=red&page=0&size=200")
    .then()
        .statusCode(200);
}

Running this gave me the exception:

No primary or default constructor found for interface com.querydsl.core.types.Predicate

Like in this question I added

@EnableSpringDataWebSupport 

Now I get a new exception:

java.lang.IllegalStateException: Failed to load ApplicationContext
...
Caused by: org.springframework.beans.factory.support.BeanDefinitionOverrideException: 
Invalid bean definition with name 'pageableResolver' defined in class path resource [org/springframework/data/rest/webmvc/config/RepositoryRestMvcConfiguration.class]: 
Cannot register bean definition 
[Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration; 
factoryMethodName=pageableResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/data/rest/webmvc/config/RepositoryRestMvcConfiguration.class]] for bean 'pageableResolver': 
There is already [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.data.web.config.HateoasAwareSpringDataWebConfiguration; 
factoryMethodName=pageableResolver; initMethodName=null; destroyMethodName=(inferred); 
defined in class path resource [org/springframework/data/web/config/HateoasAwareSpringDataWebConfiguration.class]] bound.
...

I don't understand what is happening. Has anyone an idea, how to resolve the problem?

Chris
  • 5,109
  • 3
  • 19
  • 40

0 Answers0