I'm in the development phase of a Test class to test send notifications :
1- SMS (send SMS using phone number )
2- Email (send email )
3- WebSocket (send websocket )
This is the first time I have used Junit. It's working but I need a check of the quality of code. My project is a spring boot application rest API.
the below code test Class
@RunWith(SpringRunner.class)
@WebMvcTest(NotificationController.class)
@Import(TestConfigForMail.class)
@ActiveProfiles("local")
public class NotificationTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private SpanAccessor tracer;
/**
* test send SMS notification
* @throws Exception
*/
@Test
public void testSMSNotification() throws Exception {
Notifications notification=new Notifications();
List<GeneralNotificationInfo> listNotifications=new ArrayList<GeneralNotificationInfo>();
SMSNotification smsNotification=new SMSNotification();
smsNotification.setTemplateId(1);
smsNotification.setSmsTo(new String[] {"+10002222"});
Message message=new Message();
message.setTitle("HELLO FROM SMSNOTIFICATION TEST");
message.setBody("HELLO FROM SMSNOTIFICATION TEST");
smsNotification.setMessage(message);
listNotifications.add(smsNotification);
notification.setNotfication(listNotifications);
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
this.mockMvc.perform(post(NotificationURI_Constants.SEND_NOTFICATION).contentType(
MediaType.APPLICATION_JSON).content(ow.writeValueAsString(notification)))
.andExpect(status().isOk())
.andExpect(jsonPath("$.result.status.SMSNotification", is("TRUE")));
}
/**
* test Email Notification
* @throws Exception
*/
@Test
public void testEmailNotification() throws Exception {
Notifications notification=new Notifications();
List<GeneralNotificationInfo> listNotifications=new ArrayList<GeneralNotificationInfo>();
EmailNotification emailNotification=new EmailNotification();
emailNotification.setTemplateId(1);
emailNotification.setEmail(new String[] {"[email protected]"});
Message messageEmail=new Message();
messageEmail.setTitle("HELLO EMAIL NOTIFICATION FROM TEST");
messageEmail.setBody("HELLO EMAIL NOTIFICATION FROM TEST BODY");
emailNotification.setMessage(messageEmail);
listNotifications.add(emailNotification);
notification.setNotfication(listNotifications);
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
this.mockMvc.perform(post(NotificationURI_Constants.SEND_NOTFICATION).contentType(
MediaType.APPLICATION_JSON).content(ow.writeValueAsString(notification)))
.andExpect(status().isOk())
.andExpect(jsonPath("$.result.status.EmailNotification", is("TRUE")));
}
/**
* test Web Socket Notification
* @throws Exception
*/
@Test
public void testWebSocketNotification() throws Exception {
Notifications notification=new Notifications();
List<GeneralNotificationInfo> listNotifications=new ArrayList<GeneralNotificationInfo>();
WebSocketNotification webSocketNotification=new WebSocketNotification();
webSocketNotification.setTemplateId(1);
webSocketNotification.setUsersID(Arrays.asList(new String[] {"john"}));
Message messageEmail=new Message();
messageEmail.setTitle("HELLO WEBSOCKET NOTIFICATION FROM TEST");
messageEmail.setBody("HELLO WEBSOCKET NOTIFICATION FROM TEST BODY");
webSocketNotification.setMessage(messageEmail);
listNotifications.add(webSocketNotification);
notification.setNotfication(listNotifications);
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
this.mockMvc.perform(post(NotificationURI_Constants.SEND_NOTFICATION).contentType(
MediaType.APPLICATION_JSON).content(ow.writeValueAsString(notification)))
.andExpect(status().isOk())
.andExpect(jsonPath("$.result.status.WebSocketNotification", is("TRUE")));
}
}