如何覆盖使用JUnit模拟调用静态方法的方法? [英] How to cover a method calling a static method using JUnit mocking?

查看:594
本文介绍了如何覆盖使用JUnit模拟调用静态方法的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑两个类AB.

class A {  static int a(){} }

class B {  void something(){ int value=A.a(); .......}}

现在,我必须使用Junit测试用例覆盖类B,因此我创建了一个新类(类TestB)来覆盖类B.

Now I have to cover class B using Junit Test case and hence I create a new class (class TestB) to cover the class B.

class TestB {  @Test  public void testsomething(){...} }

在这里,我的问题是,是否有任何办法可以覆盖A.a()行,因为这是静态方法.我知道我不容易模拟它,因为其中没有对象.那么我将如何进行呢? 我正在使用JUnit和EasyMock.

Here my question is if there is any way I can cover the line A.a() as this is the static method. I know that I can't easy mock it because there is no object involved. So how would I proceed? I am using JUnit and EasyMock.

推荐答案

您将不得不使用PowerMock和easymock来模拟静态方法.

You will have to use PowerMock along with easymock to mock the static methods.

https://github.com/jayway/powermock/wiki/MockStatic

对于您的测试用例,模拟代码将如下所示

For your test case mock code will look like this

KeyStore aMock = PowerMockito.mock(A.class);
PowerMockito.when(A.a()).thenReturn(0);

这是一个为KeyStore.getInstance方法模拟静态方法的工作示例

Here is a working example to mock static method for KeyStore.getInstance method

KeyStoreService:

KeyStoreService:

package com.foo;

import java.io.IOException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;


public class KeyStoreService {

    public KeyStoreService(){

    }

    public void load() throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException{
        System.out.println("start");
        KeyStore ks = KeyStore.getInstance("");
        ks.load(null, null);
        System.out.println("end");
    }

}

测试类:

package com.foo.test;

import java.io.IOException;
import java.io.InputStream;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.cert.CertificateException;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Matchers;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import com.foo.KeyStoreService;

@PrepareForTest(KeyStoreService.class)
@RunWith(PowerMockRunner.class)
public class TestKeyStore {

    @Test
    public void test1() throws KeyStoreException, NoSuchProviderException, NoSuchAlgorithmException, CertificateException, IOException{
        PowerMockito.mockStatic(KeyStore.class);
        KeyStore keyStoreMock = PowerMockito.mock(KeyStore.class);
        KeyStoreService kss = new KeyStoreService();
        PowerMockito.when(KeyStore.getInstance(Matchers.anyString(), Matchers.anyString())).thenReturn(keyStoreMock);
        Mockito.doNothing().when(keyStoreMock).load(Mockito.any(InputStream.class), Mockito.any(char[].class));
        kss.load();
    }
}

这篇关于如何覆盖使用JUnit模拟调用静态方法的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆