I'd like to create an integration test for my Spring Boot application checking that a controller returns the correct HTTP status when sending an email.
This is how my test looks like:
#SpringBootTest
#RunWith(SpringRunner.class)
#AutoConfigureMockMvc
#Profile("test")
public class EmailControllerTest {
#Autowired
private MockMvc mockMvc;
#Autowired
private ObjectMapper objectMapper;
#Rule
public SmtpServerRule smtpServerRule = new SmtpServerRule(2525);
private static final String RESOURCE_PATH = "/mail";
#Test
public void whenValidInput_thenReturns200() throws Exception {
final EmailNotification emailNotification = EmailNotification.builder()
.emailAddress("foo#bar.com")
.subject("TEST_SUBJECT")
.content("TEST_CONTENT")
.build();
mockMvc.perform(post(RESOURCE_PATH)
.contentType(APPLICATION_JSON)
.content(objectMapper.writeValueAsString(emailNotification))
).andExpect(status().isOk());
}
}
However it fails with the following excetpion:
No qualifying bean of type 'org.springframework.test.web.servlet.MockMvc' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true)}
I looked at the Spring Boot tutorials on integration testing but cannot see what's wrong.
This is the controller under test:
#RestController
public class EmailController {
private static final Logger LOG = LoggerFactory.getLogger(EmailController.class.getName());
private final EmailService emailService;
#Autowired
public EmailController(EmailService emailService) {
this.emailService = emailService;
}
#PostMapping(value = "/mail", consumes = MediaType.APPLICATION_JSON_VALUE)
public void send(#Valid #RequestBody EmailNotification emailNotification) {
try {
emailService.sendEmail(emailNotification);
} catch (MailException | MessagingException e) {
LOG.error("Error sending email: (recipient address: {}): {}", emailNotification.getEmailAddress(), e.getMessage());
}
}
}
Remove the #Profile and add #ActiveProfiles("test") from your test class they are not the same.
#Profile conditional evaluates the bean if the profile passed as argument is enabled, #ActiveProfiles change the profile to the profile passed as a argument, or in other words, activate it.
Related
I have a project with Spring-Boot + Spring-Security. In this project, typical I have MVC architecture.
My controller looks like:
#RestController
DemoController
{
public ResponseEntity<?> get(#AuthenticationPrinciple UserPrinciple userPrinciple)
{
return ...
}
}
I want to test this controller class with WebMvcTest. I know that I can handle it with SpringBootTest easily but SpringBootTest is not time-effective...
I've created a test class like:
#WebMvcTest(controllers = {DemoController.class})
DemoControllerTest
{
#Autowired
private MockMvc mockMvc;
#Test
void test_get()
{
this.mockMvc.perform(get("/...")
.header(AUTH_HEADER, getAuthorizationHeader())
.andExpect(status().isOk());
}
}
But I got error like userPrinciple is null or org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.sha.springbootbookseller.security.CustomUserDetailsService' available: expected at least 1 bean which qualifies as autowire candidate. How I can inject AuthenticationPrinciple in WebMvcTest?
Example Code: https://github.com/senolatac/spring-boot-book-seller
Test Class: https://github.com/senolatac/spring-boot-book-seller/blob/master/src/test/java/com/sha/springbootbookseller/controller/PurchaseHistoryControllerTest.java
Solved it with adding:
#WithMockUser
#ContextConfiguration
and mocking
#MockBean
private CustomUserDetailsService customUserDetailsService;
Final result looks like:
#WithMockUser
#WebMvcTest(controllers = {DemoController.class})
#ContextConfiguration(classes = {DemoController.class, JwtProvider.class, JwtFilter.class})
DemoControllerTest
{
#Autowired
private MockMvc mockMvc;
#MockBean
private CustomUserDetailsService customUserDetailsService;
#Test
void test_get()
{
this.mockMvc.perform(get("/...")
.header(AUTH_HEADER, getAuthorizationHeader())
.andExpect(status().isOk());
}
}
you can find the example code from: https://github.com/senolatac/spring-boot-book-seller
I want to write a simple test using #RestClientTest for the component below (NOTE: I can do it without using #RestClientTest and mocking dependent beans which works fine.).
#Slf4j
#Component
#RequiredArgsConstructor
public class NotificationSender {
private final ApplicationSettings settings;
private final RestTemplate restTemplate;
public ResponseEntity<String> sendNotification(UserNotification userNotification)
throws URISyntaxException {
// Some modifications to request message as required
return restTemplate.exchange(new RequestEntity<>(userNotification, HttpMethod.POST, new URI(settings.getNotificationUrl())), String.class);
}
}
And the test;
#RunWith(SpringRunner.class)
#RestClientTest(NotificationSender.class)
#ActiveProfiles("local-test")
public class NotificationSenderTest {
#MockBean
private ApplicationSettings settings;
#Autowired
private MockRestServiceServer server;
#Autowired
private NotificationSender messageSender;
#Test
public void testSendNotification() throws Exception {
String url = "/test/notification";
UserNotification userNotification = buildDummyUserNotification();
when(settings.getNotificationUrl()).thenReturn(url);
this.server.expect(requestTo(url)).andRespond(withSuccess());
ResponseEntity<String> response = messageSender.sendNotification(userNotification );
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
}
private UserNotification buildDummyUserNotification() {
// Build and return a sample message
}
}
But i get error that No qualifying bean of type 'org.springframework.web.client.RestTemplate' available. Which is right of course as i havn't mocked it or used #ContextConfiguration to load it.
Isn't #RestClientTest configures a RestTemplate? or i have understood it wrong?
Found it! Since i was using a bean that has a RestTemplate injected directly, we have to add #AutoConfigureWebClient(registerRestTemplate = true) to the test which solves this.
This was in the javadoc of #RestClientTest which i seem to have ignored previously.
Test which succeeds;
#RunWith(SpringRunner.class)
#RestClientTest(NotificationSender.class)
#ActiveProfiles("local-test")
#AutoConfigureWebClient(registerRestTemplate = true)
public class NotificationSenderTest {
#MockBean
private ApplicationSettings settings;
#Autowired
private MockRestServiceServer server;
#Autowired
private NotificationSender messageSender;
#Test
public void testSendNotification() throws Exception {
String url = "/test/notification";
UserNotification userNotification = buildDummyUserNotification();
when(settings.getNotificationUrl()).thenReturn(url);
this.server.expect(requestTo(url)).andRespond(withSuccess());
ResponseEntity<String> response = messageSender.sendNotification(userNotification );
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
}
private UserNotification buildDummyUserNotification() {
// Build and return a sample message
}
}
I am registering an ErrorHandler for my Spring Scheduler and would like to test that is is correctly registered in a SpringTest
So far I have tried:
Handler
#Component
public class ScheduledErrorHandler implements ErrorHandler {
#Autowired
private ErrorService errorService;
#Override
public void handleError(final Throwable t) {
errorService.handle(t);
}
}
Registering the Handler
#EnableScheduling
#Configuration
public class SchedulingConfiguration implements SchedulingConfigurer {
#Autowired
private ScheduledErrorHandler handler;
#Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
final ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
scheduler.setPoolSize(1);
scheduler.setErrorHandler(handler);
scheduler.initialize();
taskRegistrar.setScheduler(scheduler);
}
//...
}
Testing it's registered
#ContextConfiguration(classes = {
SchedulerConfiguration.class,
SchedulerErrorHandler.class
})
#RunWith(SpringRunner.class)
public class SchedulerErrorHandlerTest {
#MockBean
private ErrorService service;
#Autowired
private ExampleScheduledJob job;
#Test
public void verifyHandlerGetsCalled() {
// Wait until the job runs
if(!job.latch.await(5, SECONDS)) {
fail("Job never ran");
}
verify(service).handle(any(RuntimeException.class));
}
#Component
public static class ExampleScheduledJob {
private final CountDownLatch latch = new CountDownLatch(1);
#Scheduled(fixedRate=1000)
public void run() {
latch.countDown();
throw new RuntimeException("error");
}
}
}
However when I do this I get a DependencyNotFound error saying Spring cannot create my test class as no Bean named ExampleScheduledJob can be found. How can I register it only for the sake of this test?
Error creating bean with name
'com.example.demo.SchedulerErrorHandlerTest': Unsatisfied dependency
expressed through field 'job'; nested exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException: No
qualifying bean of type
'com.example.demo.SchedulerErrorHandlerTest$ExampleScheduledJob'
available: expected at least 1 bean which qualifies as autowire
candidate. Dependency annotations:
{#org.springframework.beans.factory.annotation.Autowired(required=true)}
This should work
#ContextConfiguration(classes = {
SchedulingConfiguration.class,
SchedulerErrorHandlerTest.ExampleScheduledJob.class,
ScheduledErrorHandler.class
})
#RunWith(SpringRunner.class)
You can register your test configuration class (ExampleScheduledJob) as indicated above. Since it is a static inner class, you need to use it like SchedulerErrorHandlerTest.ExampleScheduledJob
I'm trying to mock rest api call but facing an error while testing the controller class using WebMvcTest,
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.abc.center.entities.repositories.SomeRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1486)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585)
... 41 more
I have an project structure something like this,
I tried many ways but no luck, Below is my restcontroller and its Mockito test class and repository,
#Slf4j
#Component
#RequestMapping()
#Setter
public class SomeController {
// Variable initialization
#Autowired
private SometRepository someRepository;
public void sendData(RecordNo rocordNo, String xmlString, SomeFile file) throws ValidationException{
ClientHttpRequestFactory requestFactory = new
HttpComponentsClientHttpRequestFactory(HttpClients.createDefault());
RestTemplate restTemplate = new RestTemplate(requestFactory);
List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
messageConverters.add(new StringHttpMessageConverter(Charset.forName("UTF-8")));
restTemplate.setMessageConverters(messageConverters);
MultiValueMap<String,String> header = new LinkedMultiValueMap<>();
header.add("x-api-key",api_key);
header.add("Content-Type",content_type);
header.add("Cache-Control",cache_control);
HttpEntity<String> request = new HttpEntity<>(xmlString, header);
try {
restTemplate.postForEntity(getUri(rocordNo,file), request, String.class);
}catch (RestClientResponseException e){
throw new ValidationException(e.getResponseBodyAsString());
}
}
getUri(RecordNo rocordNo SomeFile file){
// here I need someRepository which is an interface
}
}
public interface TestRepository extends PagingAndSortingRepository<RecordNo, Integer> {
//queries to repositories
}
#RunWith(SpringJUnit4ClassRunner.class)
#WebMvcTest(SomeController.class)
public class SomeControllerTestClass {
private TestController serviceToTest;
private String xmlString = "String";
private MockMvc mockMvc;
#Autowired
private WebApplicationContext wac;
#Mock
private TestRepository testRepository;
#Before
public void init(){
serviceToTest.setTestRepository(testRepository);
this.mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
MockitoAnnotations.initMocks(this);
}
#Test
public void verifySafe2Call() throws Exception{
mockMvc.perform(MockMvcRequestBuilders.post("someuri")
.contentType(MediaType.APPLICATION_XML)
.accept(MediaType.APPLICATION_JSON)
.content(xmlString)).andExpect(jsonPath("$.responseMessage").value("Validation succeeded"))
.andExpect(jsonPath("$.responseCode").value("SUCCESS"))
.andDo(print());
}
Does my project structure is making any problem? I know it is not able to find the definition but not getting why it is so.
Any suggestion would be nice.
Try this,
add following code to the controller:
#Autowired
private SometRepository someRepository;
public void setRepository(SomeRepository repository) {
this.repository = repository;}
I don't see #EnableJpaRepositories annotation.
Your config should be annotated with this annotation and Spring will scan the needed packages for repositories.
#Configuration
#EnableJpaRepositories(basePackages = {"com.abc.center.entities.repositories"})
public class JpaConfig {
}
I had a similar problem writing a unit/integration test with #WebMvcTest and I solved it by adding #ComponentScan("com.abc.center.entities.repositories") to SomeController (in your case).
I've generated a Spring Boot web application using Spring Initializer, embedded Tomcat, Thymeleaf template engine, and package as an executable JAR file.
Technologies used:
Spring Boot 1.4.2.RELEASE, Spring 4.3.4.RELEASE, Thymeleaf 2.1.5.RELEASE, Tomcat Embed 8.5.6, Maven 3, Java 8
I have this email service that I want to test
#Service
public class MailClient {
protected static final Logger looger = LoggerFactory.getLogger(MailClient.class);
#Autowired
private JavaMailSender mailSender;
private MailContentBuilder mailContentBuilder;
#Autowired
public MailClient(JavaMailSender mailSender, MailContentBuilder mailContentBuilder) {
this.mailSender = mailSender;
this.mailContentBuilder = mailContentBuilder;
}
//TODO: in a properties
public void prepareAndSend(String recipient, String message) {
MimeMessagePreparator messagePreparator = mimeMessage -> {
MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage);
messageHelper.setFrom("nunito#calzada.com");
messageHelper.setTo(recipient);
messageHelper.setSubject("Sample mail subject");
String content = mailContentBuilder.build(message);
messageHelper.setText(content, true);
};
try {
if (looger.isDebugEnabled()) {
looger.debug("sending email to " + recipient);
}
mailSender.send(messagePreparator);
} catch (MailException e) {
looger.error(e.getMessage());
}
}
}
I've created this test class
#RunWith(SpringRunner.class)
public class MailClientTest {
#Autowired
private MailClient mailClient;
private GreenMail smtpServer;
#Before
public void setUp() throws Exception {
smtpServer = new GreenMail(new ServerSetup(25, null, "smtp"));
smtpServer.start();
}
#Test
public void shouldSendMail() throws Exception {
//given
String recipient = "nunito.calzada#gmail.com";
String message = "Test message content";
//when
mailClient.prepareAndSend(recipient, message);
//then
String content = "<span>" + message + "</span>";
assertReceivedMessageContains(content);
}
private void assertReceivedMessageContains(String expected) throws IOException, MessagingException {
MimeMessage[] receivedMessages = smtpServer.getReceivedMessages();
assertEquals(1, receivedMessages.length);
String content = (String) receivedMessages[0].getContent();
System.out.println(content);
assertTrue(content.contains(expected));
}
#After
public void tearDown() throws Exception {
smtpServer.stop();
}
}
But I got this error when running the test
Error creating bean with name 'com.tdk.service.MailClientTest': Unsatisfied dependency expressed through field 'mailClient'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.tdk.service.MailClient' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true)}
The problem is that you want to run an integration test without providing the beans!
Whenever you use #Autowired, you need to provide the beans required for the autowired component feed via context.
Therefore, you need to add a static class inside that test class with #Configuration annotation. Moreover, the test class also need to know which configuration must be used via #ContextConfiguration annotation. An example here:
#RunWith(SpringRunner.class)
#ContextConfiguration(classes = {MailClientTest.ContextConfiguration.class})
#SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class MailClientTest {
#Configuration
#TestPropertySource(locations = "classpath:application.properties",
properties ={"my.additiona.property=123"})
#ComponentScan(basePackages = {"com.tdk.service"})
public static class ContextConfiguration {
#Bean
public JavaMailSender mailSender{
return ... //create the instance here
}
#Bean
public MailContentBuilder mailContentBuilder() {
return ... //create the instance here
}
}
}
Anyway, as I already pointed out in a comment, don't waste your time reinventing the wheel. There is already a library out of there that does all this stuff for you. I'm talking about Spring Boot Email Tools.
I think it is worth to use that library and maybe contribute to the repository with new features instead of spending time for reimplementing email support with template engines.