gpt4 book ai didi

java - 如何初始化atlassian jira IssueService?

转载 作者:太空宇宙 更新时间:2023-11-04 09:14:25 25 4
gpt4 key购买 nike

在生产中,com.atlassian.jira.bc.issue.IssueService 被注入(inject)到构造中,但如何在单元测试中模拟它?

生产代码:

class myObject {

IssueService issueService

public myObject( IssueService issueService, ...) {
this.issueService = issueService;
}

public void foo () {
...
IssueInputParameters iip = issueService.newIssueInputParameters(); // NULL POINTER EXCEPTION WHEN RUN BU UNIT TEST!
...
}
}

我尝试在测试中编写:

IssueService issueService = ComponentAccessor.getIssueService();
MyObject mo = new MyObject(issueService, ...);
mo.foo(); // NULL POINTER EXCEPTION!

ComponentAccessor.getIssueService(); 返回 null 而不是对象。

最佳答案

启动测试用例时,大多数 Jira 服务都无法访问范围,建议使用模拟版本,您可以执行以下操作

import static org.mockito.Mockito.mock;
import org.junit.Before;
import org.junit.Test;

public class FooUnitTest {


private IssueService mockIssueService;
private MyObject mo;

@Before
public void setup() {

mockIssueService = mock(IssueService.class);

mo = new MyObject(mockIssueService , ...);

}

@Test
public void testFoo(){
mo.foo();
}
}

关于java - 如何初始化atlassian jira IssueService?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59249419/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com