first commit

This commit is contained in:
azhwani 2020-08-17 15:12:50 +01:00
parent 2bad67ff38
commit eac28eb90f
7 changed files with 92 additions and 0 deletions

View File

@ -0,0 +1,12 @@
package com.baeldung.conditionalonproperty;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class NotificationApplication {
public static void main(String[] args) {
SpringApplication.run(NotificationApplication.class, args);
}
}

View File

@ -0,0 +1,26 @@
package com.baeldung.conditionalonproperty.config;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.baeldung.conditionalonproperty.service.EmailNotification;
import com.baeldung.conditionalonproperty.service.NotificationSender;
import com.baeldung.conditionalonproperty.service.SmsNotification;
@Configuration
public class NotificationConfig {
@Bean(name = "emailNotification")
@ConditionalOnProperty(prefix = "notification", name = "service", havingValue = "email")
public NotificationSender notificationSender() {
return new EmailNotification();
}
@Bean(name = "smsNotification")
@ConditionalOnProperty(prefix = "notification", name = "service", havingValue = "sms")
public NotificationSender notificationSender2() {
return new SmsNotification();
}
}

View File

@ -0,0 +1,10 @@
package com.baeldung.conditionalonproperty.service;
public class EmailNotification implements NotificationSender {
@Override
public String send(String message) {
return "Email Notification: " + message;
}
}

View File

@ -0,0 +1,5 @@
package com.baeldung.conditionalonproperty.service;
public interface NotificationSender {
String send(String message);
}

View File

@ -0,0 +1,10 @@
package com.baeldung.conditionalonproperty.service;
public class SmsNotification implements NotificationSender {
@Override
public String send(String message) {
return "SMS notification: " + message;
}
}

View File

@ -1,2 +1,4 @@
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto = update
notification.service=email

View File

@ -0,0 +1,27 @@
package com.baeldung.conditionalonproperty;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import com.baeldung.conditionalonproperty.config.NotificationConfig;
import com.baeldung.conditionalonproperty.service.EmailNotification;
import com.baeldung.conditionalonproperty.service.NotificationSender;
public class NotificationUnitTest {
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner();
@Test
public void whenValueSetToEmail_thenCreateEmailNotification() {
this.contextRunner.withPropertyValues("notification.service=email")
.withUserConfiguration(NotificationConfig.class)
.run(context -> {
assertThat(context).hasBean("emailNotification");
NotificationSender notificationSender = context.getBean(EmailNotification.class);
assertThat(notificationSender.send("Hello From Baeldung!")).isEqualTo("Email Notification: Hello From Baeldung!");
assertThat(context).doesNotHaveBean("smsNotification");
});
}
}