Junit 无法模拟方法

Junit 无法模拟方法,junit,mocking,mockito,powermockito,Junit,Mocking,Mockito,Powermockito,我是JUNITS新手,一直试图使用Mockito和PowerMockito为我的代码编写一些测试用例,但一直面临一个问题 类别代码: public class Example implements Callable<Void> { int startIndex; int endIndex; ConnectionPool connPool; Properties properties; public Example(int start, int

我是JUNITS新手,一直试图使用Mockito和PowerMockito为我的代码编写一些测试用例,但一直面临一个问题

类别代码:

public class Example implements Callable<Void> {
    int startIndex;
    int endIndex;
    ConnectionPool connPool;
    Properties properties;

    public Example(int start, int end,
           ConnectionPool connPool, Properties properties) {
        this.startIndex = start;
        this.endIndex = end;
        this.connPool= connPool;
        this.properties = properties;
    }

    @Override
    public Void call() throws Exception {
        long startTime = System.currentTimeMillis();
        try {

            List<String> listInput = new ArrayList<>();
            Service service = new Service(
                    dbConnPool, properties, startIndex, endIndex);

            service.getMethod(listInput);

            .
            .
            .
公共类示例实现了可调用{
国际标准指数;
内部索引;
连接池连接池;
属性;
公共示例(int开始、int结束、,
连接池(连接池,属性){
this.startIndex=开始;
this.endIndex=end;
this.connPool=connPool;
这个。属性=属性;
}
@凌驾
public Void call()引发异常{
long startTime=System.currentTimeMillis();
试一试{
List listInput=新建ArrayList();
服务=新服务(
dbConnPool、properties、startIndex、endIndex);
getMethod(listInput);
.
.
.
JUNIT代码:

@RunWith(PowerMockRunner.class)
@PrepareForTest()
public class ExampleTest {

    @Mock
    private ConnectionPool connectionPool;

    @Mock
    private Properties properties;

    @Mock
    private Service service = new Service(
            connectionPool, properties, 1, 1);

    @Mock
    private Connection connection;

    @Mock
    private Statement statement;

    @Mock
    private ResultSet resultSet;

    @InjectMocks
    private Example example = new Example(
            1, 1, connectionPool, properties);


    @Test
    public void testCall() throws Exception {
        List<String> listInput= new ArrayList<>();
        listInput.add("data1");

        when(service.getMethod(listInput)).thenReturn(listInput);
        example.call();
    }
@RunWith(PowerMockRunner.class)
@PrepareForTest()
公共类示例测试{
@嘲弄
私有连接池连接池;
@嘲弄
私人物业;;
@嘲弄
私人服务=新服务(
连接池,属性,1,1);
@嘲弄
专用连接;
@嘲弄
私人声明;
@嘲弄
私有结果集结果集;
@注射模拟
私有示例示例=新示例(
1、1、连接池、属性);
@试验
public void testCall()引发异常{
List listInput=新建ArrayList();
添加(“数据1”);
当(service.getMethod(listInput)),然后返回(listInput);
call();
}
问题:如何模拟服务类及其方法getMethod调用

说明:服务类有方法getMethod,它正在与DB交互。因此,由于我无法模拟此方法,代码将通过,然后我必须模拟getMethod中的所有对象,如connection、resultset等。否则它将抛出NullPointerException


请帮助我理解我做错了什么,如果可能的话,请为我如何接近JUNITS进行此类方法调用提供指导。

Mockito不会帮助你模拟对象,如果你的方法中有
新服务的调用。
相反,你需要使用

对于PowerMockito,有一个等价物:

PowerMockito.whenNew(Service.class)
            .withArguments(connectionPool, properties, 1, 1)
            .thenReturn(mockService);

请检查。

PowerMock带有EasyMock,但我使用的是mockito PowerMockito。我尝试了:Service mockService=PowerMockito.mock(Service.class);PowerMockito.whenNew(Service.class,connectionPool,properties,1,1)。andreurn(mockService);这是抛出错误。有什么建议吗?它显示了一个语法错误。我尝试了您提供的另一个解决方案,但它没有模拟方法调用,并且仍在运行。@AyushKumar您是否收到任何错误或它只是被忽略了?您是否用
@RunWith
@PrepareForTest
注释了测试类?是的。T该应用程序是一个独立的java应用程序,我使用了PrepareForTest,但它仍然会进入我试图模拟的类。@AyushKumar能否确保传递给构造函数的参数与模拟的参数相同?
PowerMockito.whenNew(Service.class)
            .withArguments(connectionPool, properties, 1, 1)
            .thenReturn(mockService);