我想为 GetEventPlannersRequest 类编写测试。
该课程看起来:
class GetEventPlannersRequest(requestIn: GetEventPlannersRequest.Request,
okListener: RabbitResponse.RabbitResponseListener<GetEventPlannersResponse>,
errorListener: RabbitResponse.RabbitErrorListener
) : AbstractWfmRabbitRequest<GetEventPlannersRequest.Request, GetEventPlannersResponse>(requestIn, GetEventPlannersResponse::class.java, okListener, errorListener) {
override fun getExchange(): String {
return RabbitModuleConfig.EXCHANGE_MOBILE_OUT
}
override fun getRoutingKey(): String {
return WfmRabbitModuleConfig.ROUTING_KEY_WFM_EVENT_PLANNERS
}
override fun getType(): String {
return WfmRabbitModuleConfig.TYPE_WFM_EVENT_PLANNERS
}
所以在测试中,我做了这样的事情:
@Mock
lateinit var requestIn: GetEventPlannersRequest.Request
@Mock
lateinit var okListener: RabbitResponse.RabbitResponseListener<GetEventPlannersResponse>
@Mock
lateinit var errorListener: RabbitResponse.RabbitErrorListener
@Before
fun before() {
MockitoAnnotations.initMocks(this)
}
@Test
fun getExchange() {
val getEventPlannersRequest = GetEventPlannersRequest(requestIn, okListener, errorListener)
assertEquals(getEventPlannersRequest.exchange, "mobile.outgoing")
}
问题出在哪里?
问题在于父类 GetEventPlannersRequest
public AbstractRabbitRequest(K body, Class<T> clazz, RabbitResponse.RabbitResponseListener<T> listener, RabbitResponse.RabbitErrorListener error) {
super(FS.get().getSession().getTerminalId(), body, clazz, listener, error);
}
抛出 java.lang.NullPointerException 。
是否可以以某种方式模拟它?还是我需要以某种方式重构它?
更新:
现在,我添加了其他构造函数:
public AbstractRabbitRequest(String terminalId, K body, Class<T> clazz, RabbitResponse.RabbitResponseListener<T> listener, RabbitResponse.RabbitErrorListener error) {
super(terminalId, body, clazz, listener, error);
}
所以我也可以模拟terminalId并对其进行测试,但是如果可以模拟它,我也想知道如何:)