2017-05-30 29 views
3

私はJunitを初めて使用しています。以下は、私が実行しているjunitコードです。ここでmocking中のjunitのエラー

package com.de.base.util.general; 
import static org.junit.Assert.*; 
import static org.mockito.Mockito.when; 
import java.util.HashMap; 
import org.junit.Assert; 
import org.junit.Test; 
import org.junit.runner.RunWith; 
import org.mockito.InjectMocks; 
import org.mockito.Mock; 
import org.mockito.runners.MockitoJUnitRunner; 
import org.powermock.api.mockito.PowerMockito; 

@RunWith(MockitoJUnitRunner.class) 
public class JReportUtilTest { 
@InjectMocks 
ReportUtil w_res = new ReportUtil(); 

@Mock 
CollectionUtil w_util; 

@Test 
public void test_removeHashedSettings() throws Exception { 
    HashMap<String ,String> w_abc = new HashMap<String,String>(); 
    w_abc.put("abc","89"); 
    //CollectionUtil mock = org.mockito.Mockito.mock(CollectionUtil.class); 
    //PowerMockito.mockStatic(CollectionUtil.class,w_abc);   
    when(w_util.createHashMap("abc:89", ":")).thenReturn(w_abc); 
    assertEquals("abc:89:",ReportUtil.removeHashedSettings("1", "abc:89", ":")); 
} 
} 

私のAPI removedHashedSettingsin ReportUtil以下

public static String removeHashedSettings(String key, String a_settings, String deilimiter) throws Exception 
    { 
     if (!(key != null && key.trim().length() > 0)) 
      return a_settings; 
     if (!(a_settings != null && a_settings.trim().length() > 0)) 
      return a_settings; 

     HashMap hSettings = CollectionUtil.createHashMap(a_settings, deilimiter); 
     hSettings.remove(key); 
     return getSettingFromHash(hSettings, deilimiter); 
    } 

は私が嘲笑する必要がCollectionUtilでcreateHashMapためのコードです。ここで

public static HashMap<String, String> createHashMap(String a_NameValStr, String a_Delim)// throws Exception 
    { 
     HashMap<String, String> w_KeyVal = new HashMap<String, String>(); 
     if (LOGGER.isInfoEnabled()) LOGGER.info("CollectionUtil:createHashMap:Hashing string: "+ a_NameValStr); 

     if(a_NameValStr == null) return w_KeyVal; 

      StringTokenizer w_StrTkn = new StringTokenizer(a_NameValStr, a_Delim); 
      if(w_StrTkn.countTokens() == 0 || (w_StrTkn.countTokens()%2) != 0) 
      { 
       LOGGER.warn("CollectionUtil:createHashMap:Invalid number of tokens to hash: "+ a_NameValStr+":"+w_StrTkn.countTokens()); 
       return w_KeyVal; 
      } 

      while (w_StrTkn.hasMoreTokens()) w_KeyVal.put(w_StrTkn.nextToken(), w_StrTkn.nextToken()); 
     System.out.println(w_KeyVal); 
     return w_KeyVal; 
    } 

は、私のJUnitテストケースを実行している間、私は取得していますエラーです。

org.mockito.exceptions.misusing.MissingMethodInvocationException: 
when() requires an argument which has to be 'a method call on a mock'. 
For example: 
    when(mock.getArticles()).thenReturn(articles); 

Also, this error might show up because: 
1. you stub either of: final/private/equals()/hashCode() methods. 
    Those methods *cannot* be stubbed/verified. 
    Mocking methods declared on non-public parent classes is not supported. 
2. inside when() you don't call method on mock but on some other object. 

    at com.de.base.util.general.JReportUtilTest.test_removeHashedSettings(JReportUtilTest.java:32) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
    at java.lang.reflect.Method.invoke(Method.java:597) 
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45) 
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) 
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42) 
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) 
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263) 
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68) 
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47) 
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231) 
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60) 
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229) 
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50) 
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222) 
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300) 
    at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37) 
    at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62) 
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86) 
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192) 

私はmockito -all-1.10.19.jar、powermock-API-mockito-1.6.6.jar、powermockコア-1.6.6.jar、powermockモジュール-junit4-1.6を使用しています。 6.jar 誰も私がこの問題を解決するのを助けることができますか?

+0

CollectionUtilは、最終的なクラスですか? – sasankad

+0

最終クラスではありません – LPatil

+0

'CollectionUtil'でモックしようとしているメソッドは静的なので、クラスには' @RunWith(PowerMockRunner.class) 'と' @PrepareForTest(CollectionUtil.class) 'が必要です。さらに、 'PowerMockito.mockStatic(CollectionUtil。class); ' – kism3t

答えて

3

は私の作業コードです:

import static org.junit.Assert.assertEquals; 

import java.util.HashMap; 

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

@RunWith(PowerMockRunner.class) 
@PrepareForTest(CollectionUtil.class) 
public class TestHarnesTest { 
    @InjectMocks 
    TestHarnes w_res = new TestHarnes(); 

    @Before 
    public void before() { 
     PowerMockito.mockStatic(CollectionUtil.class); 
    } 

    @Test 
    public void test_removeHashedSettings() throws Exception { 
     HashMap<String, String> w_abc = new HashMap<String, String>(); 
     w_abc.put("abc", "89"); 
     // CollectionUtil mock = org.mockito.Mockito.mock(CollectionUtil.class); 
     // PowerMockito.mockStatic(CollectionUtil.class,w_abc); 
     PowerMockito.when(CollectionUtil.createHashMap(Mockito.eq("abc:89"), Mockito.eq(":"))).thenReturn(w_abc); 
     assertEquals("abc:89:", TestHarnes.removeHashedSettings("1", "abc:89", ":")); 
    } 
} 

とTestHarnesクラス

public class TestHarnes { 

    public static String removeHashedSettings(final String key, final String a_settings, final String deilimiter) throws Exception { 
     if (!(key != null && key.trim().length() > 0)) { 
      return a_settings; 
     } 
     if (!(a_settings != null && a_settings.trim().length() > 0)) { 
      return a_settings; 
     } 

     HashMap hSettings = CollectionUtil.createHashMap(a_settings, deilimiter); 
     hSettings.remove(key); 
     return getSettingFromHash(hSettings, deilimiter); 
    } 

    private static String getSettingFromHash(final HashMap hSettings, final String deilimiter) { 
     return ""; 
    } 

} 
+0

PowerMockito.mockStatic(CollectionUtil.class)でエラーが発生しています。 – LPatil

+0

エラーは次のとおりです。 - org.powermock.api.support.membermodification.MemberModifierタイプを解決できません。それは間接的に必要な.classファイルから参照されます – LPatil

+0

yor CollectionUtilクラスとは何ですか?そこからFrameworkはそれですか? – kism3t

2

あなたはPowerMockランナーを使用していない:

@RunWith(PowerMockRunner.class) 

Mockitoは、静的メソッドを模擬することはできませんが、PowerMockはありません。

そして、あなたは、静的メソッドを持つクラスをモックする必要があります

PowerMockito.mockStatic(CollectionUtil.class); 

をとにかくよりよい設計はインスタンスメソッドで静的メソッドを置き換えています。
静的メソッドは自然にテスト可能ではなく、複雑で読みにくい回避策を作成する必要があります。
たとえば、単純なクラスをテストするために必要な依存関係の混在の複雑さを見てください。

あなたは、ドメインのコアロジックを実行し、を嘲笑する必要がないことはありませんヘルパーなどの静的メソッドの使用を維持する必要があります。

createHashMap() static methodの場合のように、メソッドがドメインのコアロジックを実行する場合、テスト中に従属クラス間に副作用が生じないようにするためには、おそらくモックする必要があります。
あなたが気づいたとおり、静的メソッドを実際にオーバーライドするようには設計されていないので、静的メソッドをモックするのは本当に不器用です。

さらに、ドメインのコアロジックでは、OOP(継承、多型、デザインパターンなど...)を活用する能力、静的メソッドでどのように達成するべきか? -

レガシーコードの場合は、実際には変更できませんが、それ以外の場合は受け入れられません。そうでない場合は、コードをリファクタリングする必要があります。ここで

+0

私は上記のように3つのpowermock jarを使用しています。しかし、私はまだあなたのコードを実装している間にこのエラーが発生しています。 – LPatil

+0

タイプorg.powermock.api.support.membermodification.MemberModifierを解決できません。必須の.classファイルから間接参照されます – LPatil

+0

依存関係が見つからないか、IDEが更新されません。私が複雑な回避策を作ることを強制すると言うと、シンプルなクラスをテストするために必要なミックス依存性の複雑さを見てください。 – davidxxx