首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >开发Junit测试通知

开发Junit测试通知
EN

Code Review用户
提问于 2018-06-04 21:54:01
回答 1查看 365关注 0票数 0

我正处于测试类测试发送通知的开发阶段:

1-短信(用电话号码发送短信)

2-电子邮件(发送电子邮件)

3- WebSocket (发送websocket )

这是我第一次使用Junit。它正在工作,但我需要检查代码的质量。我的项目是一个spring引导应用程序rest。

下面的代码测试类

代码语言:javascript
复制
@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 listNotifications=new ArrayList();
        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 listNotifications=new ArrayList();

        EmailNotification emailNotification=new EmailNotification();
        emailNotification.setTemplateId(1);
        emailNotification.setEmail(new String[] {"test@yahoo.com"});
        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 listNotifications=new ArrayList();

        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")));

    }
}
EN

回答 1

Code Review用户

发布于 2018-06-05 06:09:28

一些可以提高代码可读性的小调整:

  • 您的代码确实可以从一些战略性放置的空白中获益。
  • 不要在方法开始时实例化对象,并且忘记5+行中的对象,而是尝试只在必要时创建对象。在存储基本类型时也有同样的想法。
  • 与赋值运算符之间的空格保持一致。

将上述想法结合在一起可能是这样的:

代码语言:javascript
复制
@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 listNotifications = new ArrayList();
    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")));
}
票数 1
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/195843

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档