mercredi 18 mars 2020

Beans in Spring test

I have one user controller.

User controller:

@CrossOrigin(origins = "*", allowedHeaders = "*")
@RestController
@RequestMapping("users")
public class UserRestController {

private final UserService userService;

public UserRestController(final UserService userService) {
    this.userService = userService;
}

@GetMapping()
public User getInfo(
        final @AuthenticationPrincipal UserDetails userDetails
) {
    return userService.findByUsername(userDetails.getUsername());
}

@PutMapping()
public User update(
        final @AuthenticationPrincipal UserDetails userDetails,
        final @RequestBody User user
) {
    User userInSystem = userService.findByUsername(
            userDetails.getUsername()
    );
    return userService.update(user, userInSystem);
}
}

I make some test for him.

Test User controller:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = ApplicationArguments.class)
@Configuration
@EnableAutoConfiguration
public class TestUserRestController {

@Autowired
private UserRestController userRestController;

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

And in the end, I have an error when running the test:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.daniil.ostrouh.notes.TestUserRestController': Unsatisfied dependency expressed through field 'userRestController'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.daniil.ostrouh.notes.rest.UserRestController' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

Please tell me what I'm doing wrong

Aucun commentaire:

Enregistrer un commentaire