使用 Powermockito 断言静态方法的参数值
Asserting argument values of a static method using Powermockito
我的简化 class 文件如下所示:
public class NotificationHelper{
public static void sendNotification(String id){
switch (id) {
case mobile:
String message = "Created new account"
break;
case email:
String message = "Hi, An account is created with our website using your email id. This is a notification regarding the same."
break;
default:
throw new Exception("id is neither phone number nor email id");
}
notify(id, message);
}
public static void notify(String id, String message){
//Code to send notification
}
}
我正在编写一个 junit 测试 class 来测试方法 sendNotification
,同时模拟通知方法。目标是断言传递给 notify
方法的 id 和 message 变量的值。
您需要创建一个 spy。使用 mockito API:
@RunWith(PowerMockRunner.class)
@PrepareForTest(NotificationHelper.class)
@PowerMockRunnerDelegate(PowerMockRunnerDelegate.DefaultJUnitRunner.class) // for @Rule
public class NotificationHelperTest {
@Rule
public ExpectedException expectedException = ExpectedException.none();
@Before
public void setUp() throws Exception {
PowerMockito.spy(NotificationHelper.class);
}
@Test
public void testSendNotificationForEmail() throws Exception {
NotificationHelper.sendNotification("email");
PowerMockito.verifyStatic();
NotificationHelper.notify("email", "Hi, An account is created with our website using your email id. This is a notification regarding the same.");
}
@Test
public void testSendNotificationForMobile() throws Exception {
NotificationHelper.sendNotification("mobile");
PowerMockito.verifyStatic();
NotificationHelper.notify("mobile", "Created new account");
}
@Test
public void testSendNotification() throws Exception {
this.expectedException.expect(Exception.class);
this.expectedException.expectMessage("id is neither phone number nor email id");
NotificationHelper.sendNotification("foobar");
}
}
请注意,我确实更正了您的 NotificationHelper
:
public class NotificationHelper {
public static void sendNotification(String id) throws Exception {
// TODO: use an enum
String message;
switch (id) {
case "mobile":
message = "Created new account";
break;
case "email":
message = "Hi, An account is created with our website using your email id. This is a notification regarding the same.";
break;
default:
throw new Exception("id is neither phone number nor email id");
}
notify(id, message);
}
public static void notify(String id, String message){
//Code to send notification
}
}
使用 PowerMock 1.6.2
测试
另请注意,如果您避免static
。
,测试总是容易得多
我的简化 class 文件如下所示:
public class NotificationHelper{
public static void sendNotification(String id){
switch (id) {
case mobile:
String message = "Created new account"
break;
case email:
String message = "Hi, An account is created with our website using your email id. This is a notification regarding the same."
break;
default:
throw new Exception("id is neither phone number nor email id");
}
notify(id, message);
}
public static void notify(String id, String message){
//Code to send notification
}
}
我正在编写一个 junit 测试 class 来测试方法 sendNotification
,同时模拟通知方法。目标是断言传递给 notify
方法的 id 和 message 变量的值。
您需要创建一个 spy。使用 mockito API:
@RunWith(PowerMockRunner.class)
@PrepareForTest(NotificationHelper.class)
@PowerMockRunnerDelegate(PowerMockRunnerDelegate.DefaultJUnitRunner.class) // for @Rule
public class NotificationHelperTest {
@Rule
public ExpectedException expectedException = ExpectedException.none();
@Before
public void setUp() throws Exception {
PowerMockito.spy(NotificationHelper.class);
}
@Test
public void testSendNotificationForEmail() throws Exception {
NotificationHelper.sendNotification("email");
PowerMockito.verifyStatic();
NotificationHelper.notify("email", "Hi, An account is created with our website using your email id. This is a notification regarding the same.");
}
@Test
public void testSendNotificationForMobile() throws Exception {
NotificationHelper.sendNotification("mobile");
PowerMockito.verifyStatic();
NotificationHelper.notify("mobile", "Created new account");
}
@Test
public void testSendNotification() throws Exception {
this.expectedException.expect(Exception.class);
this.expectedException.expectMessage("id is neither phone number nor email id");
NotificationHelper.sendNotification("foobar");
}
}
请注意,我确实更正了您的 NotificationHelper
:
public class NotificationHelper {
public static void sendNotification(String id) throws Exception {
// TODO: use an enum
String message;
switch (id) {
case "mobile":
message = "Created new account";
break;
case "email":
message = "Hi, An account is created with our website using your email id. This is a notification regarding the same.";
break;
default:
throw new Exception("id is neither phone number nor email id");
}
notify(id, message);
}
public static void notify(String id, String message){
//Code to send notification
}
}
使用 PowerMock 1.6.2
测试另请注意,如果您避免static
。