0
\$\begingroup\$

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")));
 }
}
Sᴀᴍ Onᴇᴌᴀ
29.5k16 gold badges45 silver badges201 bronze badges
asked Jun 4, 2018 at 21:54
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

A few small tweaks that can improve the readability of your code:

  • Your code could really benefit from a few strategically placed white spaces.
  • Instead of instantiating objects at the start of a method and forgetting about them for 5+ lines, try creating your objects only when you must. Same idea applies when storing primitive types.
  • Be consistent with the spaces between your assignment operators.

Bringing the above ideas together might look something like this:

@Test
public void testSMSNotification() throws Exception {
 Message message = new Message();
 message.setTitle("HELLO FROM SMSNOTIFICATION TEST");
 message.setBody("HELLO FROM SMSNOTIFICATION TEST");
 SMSNotification smsNotification = new SMSNotification();
 smsNotification.setTemplateId(1);
 smsNotification.setSmsTo(new String[] {"+10002222"});
 smsNotification.setMessage(message);
 List<GeneralNotificationInfo> listNotifications = new ArrayList<GeneralNotificationInfo>();
 listNotifications.add(smsNotification);
 Notifications notification = new Notifications();
 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")));
}
answered Jun 5, 2018 at 6:09
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.