2

here is my junit which i am trying to implement.

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);
    }
}

I want to mock createHashMap() method which is static method. Below is an error trace which i am getting while running the above 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.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)

please help.

here is createHashMap Api

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;
 }
ShraddhaJ
  • 201
  • 1
  • 14
  • *"I want to mock createHashMap() method which is static method."* what is the exact reason why you cannot change that to a non static method? – Timothy Truckle Jul 11 '17 at 09:04
  • hi ,jobin I have Added createHashMap in the question. – ShraddhaJ Jul 11 '17 at 09:11
  • You can't mock static methods with Mockito. Consider changing it to a non static method – Alberto S. Jul 11 '17 at 09:11
  • so what is another option for mocking static methods? – ShraddhaJ Jul 11 '17 at 09:12
  • this will help.. https://stackoverflow.com/questions/21105403/mocking-static-methods-with-mockito – Jobin Jul 11 '17 at 09:19
  • Thanks Jobin. I have tried writing junit using powermock . but getting some dependency Error. https://stackoverflow.com/questions/45032761/dependencies-while-implementing-mocking-in-junit4-powermock can you please help me out in this? – ShraddhaJ Jul 11 '17 at 11:10
  • Hi, Shraddha, I reviewed your create method and don't see any reason to mock it. It does not communicate with other system, does not require network connection. So, use it directly by passing needed input parameters. – Dmytro Maslenko Jul 11 '17 at 16:15

0 Answers0