我正处于测试类测试发送通知的开发阶段:
1-短信(用电话号码发送短信)
2-电子邮件(发送电子邮件)
3- WebSocket (发送websocket )
这是我第一次使用Junit。它正在工作,但我需要检查代码的质量。我的项目是一个spring引导应用程序rest。
下面的代码测试类
@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")));
}
}发布于 2018-06-05 06:09:28
一些可以提高代码可读性的小调整:
将上述想法结合在一起可能是这样的:
@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")));
}https://codereview.stackexchange.com/questions/195843
复制相似问题