1

I have below test which works successfully while executing in IDE but when I do mvn install it fails with below msg

org.mockito.exceptions.misusing.MissingMethodInvocationException: 
when() requires an argument which has to be 'a method call on a mock'.

Below is the test class:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader = SpringockitoContextLoader.class,locations={"classpath:conf/test-context.xml"})

public class ServiceTest
{
    @ReplaceWithMock
    @Autowired
    private Object1 object1

    @Autowired
    private Service service;

    @Before
    public void setup()
    {
       MockitoAnnotations.initMocks(this);
    }   

    @Test
    public void test()
       {//below line is failing while doing mvn install
       String validateMe="1234"
       Mockito.when(object1.validate(validateMe)).thenReturn(true)
       }

}

Do I need any other extra configuration to have it work in maven ?

update Upon further analysis between the behaviour of the results from IDE and maven.I added below code in test method and found that while running from IDE object1 is coming as mock and is returning true but while running from maven test it is printing as false. Any idea why this would be haappening

new MockUtil().isMock(object1)
Rips
  • 1,964
  • 1
  • 23
  • 45
  • Do you need to use @Mock instead of @ReplaceWithMock? – BretC Jan 28 '15 at 17:54
  • @Bret RepaceWithMock is a springockito thing..it works well with spring beans and contexts.....as I said test runs as it is when run via IDE – Rips Jan 28 '15 at 17:55
  • This doesn't look correct to me.. does it even compile for you? What is "String" doing in there? Mockito.when(object1.validate(String validateMe)).thenReturn(true) – unigeek Jan 28 '15 at 19:19
  • You need an argument matcher, like anyString(), I suppose.. – unigeek Jan 28 '15 at 19:26
  • 1
    @unigeek ...I was using a sample as I am not supposed to share my company code...Original code is similar to this... – Rips Jan 29 '15 at 05:43
  • One thing I observed is new MockUtil().isMock(object1) comes as true while running from IDE but it is printing as false when running from mvn install – Rips Jan 29 '15 at 05:51
  • You have confirmed that your springockito jar is declared as a maven test scope dependency? I imagine so, but had to ask. – unigeek Jan 29 '15 at 12:02
  • Yes I have added both springockito and springockito-annotation..just now I found that if I comment out @ReplaceByMock with equaivalent xml mocking config,it works both in maven and IDE.Strange!! – Rips Jan 29 '15 at 12:09
  • That is odd, but probably a good enough solution for you! All I can figure is there is some classpath difference between your IDE and Maven. – unigeek Jan 29 '15 at 14:05

1 Answers1

0

I found that if I comment out @ReplaceByMock with equaivalent xml mocking config,it works both in maven and IDE.Strange!! equivalent xml is as below:

<mockito:mock id="object1" class="org.fs.service.validation.Object1" />

update I was merging the code from branch to trunk and again thought to run using annotations from the trunk.I was surprised to see it working.Probably there was some gotcha in my branch which caused this issue.

Rips
  • 1,964
  • 1
  • 23
  • 45