PowerMock whenSpring 组件构造函数出现新问题
PowerMock whenNew Problem On Spring Component Constructor
我有如下 Spring 服务:
@Service
public class SendWithUsService
{
private SendWithUs mailAPI;
public SendWithUsService()
{
this.mailAPI = new SendWithUs();
}
public void sendEmailEvent(Dto data)
{
try
{
SendWithUsSendRequest request = new SendWithUsSendRequest()...;
mailAPI.send(request);
}
catch (Exception e)
{
...
}
}
}
我的测试代码如下所示:
@RunWith(PowerMockRunner.class)
@PowerMockIgnore({"javax.net.ssl.*"})
@PrepareForTest(SendWithUsService.class)
public class SendWithUsServiceTest
{
@InjectMocks
private SendWithUsService sendWithUsService;
@Mock
private SendWithUs mailAPI;
@Test
public void sendEmailEvent_successfully() throws Exception
{
whenNew(SendWithUs.class).withAnyArguments().thenReturn(mailAPI);
Dto emailData = ...;
sendWithUsService.sendEmailEvent(emailData);
...
}
}
在这里,PowerMock whenNew 方法不起作用。但是当我像在 sendEmailEvent 方法内部那样在构造函数外部调用它时,它会被触发。
有办法处理吗?
作品:
public void sendEmailEvent(Dto data)
{
this.mailAPI = new SendWithUs();
...
}
无效:
public SendWithUsService()
{
this.mailAPI = new SendWithUs();
}
我是这样解决的:
@RunWith(PowerMockRunner.class)
@PowerMockIgnore({"javax.net.ssl.*"})
@PrepareForTest(SendWithUsService.class)
public class SendWithUsServiceTest
{
@InjectMocks
private SendWithUsService sendWithUsService;
@Mock
private SendWithUs mailAPI;
@Before
public void setUp() throws Exception {
whenNew(SendWithUs.class).withAnyArguments().thenReturn(mailAPI);
MockitoAnnotations.initMocks(this);
}
@Test
public void sendEmailEvent_successfully() throws Exception
{
Dto emailData = ...;
sendWithUsService.sendEmailEvent(emailData);
...
}
}
我有如下 Spring 服务:
@Service
public class SendWithUsService
{
private SendWithUs mailAPI;
public SendWithUsService()
{
this.mailAPI = new SendWithUs();
}
public void sendEmailEvent(Dto data)
{
try
{
SendWithUsSendRequest request = new SendWithUsSendRequest()...;
mailAPI.send(request);
}
catch (Exception e)
{
...
}
}
}
我的测试代码如下所示:
@RunWith(PowerMockRunner.class)
@PowerMockIgnore({"javax.net.ssl.*"})
@PrepareForTest(SendWithUsService.class)
public class SendWithUsServiceTest
{
@InjectMocks
private SendWithUsService sendWithUsService;
@Mock
private SendWithUs mailAPI;
@Test
public void sendEmailEvent_successfully() throws Exception
{
whenNew(SendWithUs.class).withAnyArguments().thenReturn(mailAPI);
Dto emailData = ...;
sendWithUsService.sendEmailEvent(emailData);
...
}
}
在这里,PowerMock whenNew 方法不起作用。但是当我像在 sendEmailEvent 方法内部那样在构造函数外部调用它时,它会被触发。
有办法处理吗?
作品:
public void sendEmailEvent(Dto data)
{
this.mailAPI = new SendWithUs();
...
}
无效:
public SendWithUsService()
{
this.mailAPI = new SendWithUs();
}
我是这样解决的:
@RunWith(PowerMockRunner.class)
@PowerMockIgnore({"javax.net.ssl.*"})
@PrepareForTest(SendWithUsService.class)
public class SendWithUsServiceTest
{
@InjectMocks
private SendWithUsService sendWithUsService;
@Mock
private SendWithUs mailAPI;
@Before
public void setUp() throws Exception {
whenNew(SendWithUs.class).withAnyArguments().thenReturn(mailAPI);
MockitoAnnotations.initMocks(this);
}
@Test
public void sendEmailEvent_successfully() throws Exception
{
Dto emailData = ...;
sendWithUsService.sendEmailEvent(emailData);
...
}
}