Java 实现JUnit4(Mockito)时出错

Java 实现JUnit4(Mockito)时出错,java,unit-testing,mockito,junit4,Java,Unit Testing,Mockito,Junit4,这是我正在尝试实现的junit 请帮忙 下面是CreateHashMapAPI publicstatichashmap createHashMap(字符串a\u NameValStr,字符串a\u Delim) { HashMap w_KeyVal=新的HashMap(); if(LOGGER.isInfoEnabled())LOGGER.info(“CollectionUtil:createHashMap:Hashing字符串:”+a_NameValStr); 如果(a_namevaltr==

这是我正在尝试实现的junit

请帮忙

下面是CreateHashMapAPI

publicstatichashmap createHashMap(字符串a\u NameValStr,字符串a\u Delim)
{
HashMap w_KeyVal=新的HashMap();
if(LOGGER.isInfoEnabled())LOGGER.info(“CollectionUtil:createHashMap:Hashing字符串:”+a_NameValStr);
如果(a_namevaltr==null)返回w_KeyVal;
StringTokenizer w_StrTkn=新的StringTokenizer(a_NameValStr,a_Delim);
如果(w_StrTkn.countTokens()==0 | |(w_StrTkn.countTokens()%2)!=0)
{
LOGGER.warn(“CollectionUtil:createHashMap:要哈希的令牌数无效:“+a_NameValStr+”:“+w_StrTkn.countTokens());
返回w_KeyVal;
}
而(w_StrTkn.hasMoreTokens())w_KeyVal.put(w_StrTkn.nextToken(),w_StrTkn.nextToken());
返回w_KeyVal;
}

“我想模拟createHashMap()方法,它是静态方法。”您不能将其更改为非静态方法的确切原因是什么?嗨,jobin,我在问题中添加了createHashMap。您不能用Mockito模拟静态方法。考虑将它改为非静态方法,那么模拟静态方法还有什么选择?
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.runners.MockitoJUnitRunner;


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

   CollectionUtil mock = org.mockito.Mockito.mock(CollectionUtil.class);

    @Test
    public void test_removeHashedSettings() throws Exception {

        HashMap<String, String> w_abc = new HashMap<String, String>();
        w_abc.put("abc", "89");

        when(mock.createHashMap("abc:89",":")).thenReturn(w_abc);
        Assert.assertEquals(ReportUtil.removeHashedSettings("1", "abc:89", ":"),"abc:89:",0);
    }
}
 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.Power_Test.test_removeHashedSettings(Power_Test.java:34)
 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:50)
     at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
     at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
     at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
     at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
     at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
     at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
     at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
     at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
     at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
     at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
     at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
     at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
     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)
public static HashMap<String, String> createHashMap(String a_NameValStr, String a_Delim)
 {
  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());

  return w_KeyVal;
 }