使用Junit

时间:2017-10-15 12:46:49

标签: java spring junit powermock powermockito

单元测试设置

@RunWith(PowerMockRunner.class)
@PrepareForTest(PGWService.class)
public class PGWServiceTest {
public static final String TEST_CLIENTTRX_ID = "12345";
public static final int TEST_SITE_ID = 0;
public static final long TEST_USER_ID = 12345L;
public static final int TEST_CURRENCY_ID = 1;
public static final String TEST_ACCOUNT_TYPE = "Test account type";
public static final boolean TEST_IS_DEDUPING = false;
public static final byte[] TEST_SSN_CRYPT = new byte[] { 1, 2, 3 };
public static final byte[] TEST_TAX_ID_CRYPT = new byte[] { 1, 2, 3 };
public static final String TEST_SSN_LAST_FOUR = "1234";
public static final String TEST_TAX_ID_LAST_FOUR = "1234";

@Mock
PGWBaseRequest request;

@Mock
PGWClient client;

PGWService pGWService = new PGWService();

PGWGetAccountTaxIDResponse expectedResponse = this
        .buildGPWGetAccountIdResponse(TEST_SSN_CRYPT, TEST_TAX_ID_CRYPT,
                TEST_SSN_LAST_FOUR, TEST_TAX_ID_LAST_FOUR);

@Before
public void setUp() throws Exception {
    PowerMockito.mockStatic(PGWClient.class);
    PowerMockito.mockStatic(PGWBaseRequest.class);
    PowerMockito.when(PGWClient.getInstance()).thenReturn(client);
    PowerMockito
            .whenNew(PGWBaseRequest.class)
            .withArguments(TEST_CLIENTTRX_ID, TEST_SITE_ID, TEST_USER_ID,
                    TEST_CURRENCY_ID, TEST_ACCOUNT_TYPE, TEST_IS_DEDUPING)
            .thenReturn(request);
    PowerMockito.when(client.getTaxIdInformation(request)).thenReturn(
            expectedResponse);
}

@Test
public void testGetTaxIdInformationHappyPath() throws PGWException {

    PGWGetAccountTaxIDResponse actualResponse = pGWService
            .getTaxIdInformation(TEST_CLIENTTRX_ID, TEST_SITE_ID,
                    TEST_USER_ID, TEST_CURRENCY_ID, TEST_ACCOUNT_TYPE,
                    TEST_IS_DEDUPING);
    Assert.assertEquals(expectedResponse.getSSNLastFour(),
            actualResponse.getSSNLastFour());
}

private PGWGetAccountTaxIDResponse buildGPWGetAccountIdResponse(
        byte[] ssnCrypt, byte[] taxIdCrypt, String ssnLastFour,
        String taxIdLastFour) {
    PGWGetAccountTaxIDResponse response = new PGWGetAccountTaxIDResponse();
    response.setSSNCrypt(ssnCrypt);
    response.setSSNLastFour(ssnLastFour);
    response.setTaxIDCrypt(taxIdCrypt);
    response.setTaxIDLastFour(taxIdLastFour);
    return response;

}

}

我尝试运行单元测试,但在测试中失败并出现以下错误

  

java.lang.ExceptionInInitializerError at   sun.reflect.GeneratedSerializationConstructorAccessor7.newInstance(未知   来源)at   java.lang.reflect.Constructor.newInstance(Constructor.java:526)at   org.objenesis.instantiator.sun.SunReflectionFactoryInstantiator.newInstance(SunReflectionFactoryInstantiator.java:40)   在org.objenesis.ObjenesisBase.newInstance(ObjenesisBase.java:59)at   org.mockito.internal.creation.jmock.ClassImposterizer.createProxy(ClassImposterizer.java:128)   在   org.mockito.internal.creation.jmock.ClassImposterizer.imposterise(ClassImposterizer.java:63)   在   org.powermock.api.mockito.internal.mockcreation.MockCreator.createMethodInvocationControl(MockCreator.java:111)   在   org.powermock.api.mockito.internal.mockcreation.MockCreator.mock(MockCreator.java:60)   在org.powermock.api.mockito.PowerMockito.mock(PowerMockito.java:143)   在   org.powermock.api.extension.listener.AnnotationEnabler.standardInject(AnnotationEnabler.java:84)   在   org.powermock.api.extension.listener.AnnotationEnabler.beforeTestMethod(AnnotationEnabler.java:51)   在   org.powermock.tests.utils.impl.PowerMockTestNotifierImpl.notifyBeforeTestMethod(PowerMockTestNotifierImpl.java:90)   在   org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl $ PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:292)   在   org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl $ PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:127)   在   org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl $ PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:82)   在   org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl $ PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:282)   在   org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:84)   在org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:49)   在   org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:207)   在   org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:146)   在   org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl $ 1.run(PowerMockJUnit44RunnerDelegateImpl.java:120)   在   org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)   在   org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)   在   org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:118)   在   org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:104)   在   org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53)   在   org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:53)   在   org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)   在   org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)   在   org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)   在   org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)   在   org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)   在   org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)   引起:at. java.lang.NullPointerException   com.bdd.domain.biz.pgwclient.DynamicConfigListener.getAttributeValue(DynamicConfigListener.java:132)   在   com.bdd.domain.biz.pgwclient.PGWCommunicator.init(PGWCommunicator.java:95)   在   com.ebay.domain.biz.pgwclient.PGWCommunicator。(PGWCommunicator.java:83)   在   com.bdd.domain.biz.pgwclient.PGWCommunicator.getInstance(PGWCommunicator.java:62)   在com.bdd.domain.biz.pgwclient.PGWClient。(PGWClient.java:117)

1 个答案:

答案 0 :(得分:1)

当您尝试使用PowerMock模拟静态方法时,它会在内部创建该类的实例。所以,当你

PowerMockito.mockStatic(PGWClient.class);

在PowerMock中创建了类的新实例。您的PGWClient类默认构造函数此时会执行一些初始化调用静态方法。这会导致静态模拟失败。

你应该检查Power Mock Suppressing Unwanted Behavior

修改

从堆栈跟踪可以看出PGWClient正在访问实例 PGWCommunicator通过getInstance方法。里面有一些不受欢迎的行为。你可以

  1. 使用上面链接中的信息
  2. 来抑制其行为
  3. 创建一个默认(包)或受保护的setInstance方法来替换测试期间返回的对象
  4. 理想情况下,从单例实例束缚移动到DI。这样的代码是一种更容易测试的方式。