java when() 需要一个参数,该参数必须是“对模拟的方法调用”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35840428/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 00:39:07  来源:igfitidea点击:

when() requires an argument which has to be 'a method call on a mock'

javaspringunit-testing

提问by Jibran Shaikh

I am writing JUnit Test cases for my Controller class in Spring.In that I am getting a MissingMethodInvocationException saying when() requires an argument which has to be 'a method call on a mock' while invoking mocked object of WebTarget in when().then method.

我正在 Spring.In 中为我的 Controller 类编写 JUnit 测试用例,因为我收到一个 MissingMethodInvocationException 说 when() 需要一个参数,该参数必须是“模拟上的方法调用”,同时在 when() 中调用 WebTarget 的模拟对象。然后方法。

Here's the code.

这是代码。

@Controller 
public class JobController {   

    WebTarget target = null;   
    ClientConfig config = new ClientConfig();  
    Client client = ClientBuilder.newClient(config);`
    List<ScheduleJob> scheduleJobsList = new ArrayList<ScheduleJob>();

    @RequestMapping(value = "displayScheduledJobs")
    public ModelAndView displayScheduledJobs(@ModelAttribute(value = "Server") Server server) throws JAXBException {

        List<Server> serverList = serverService.getJobServerList(user.getAccountId());
        target = client.target(getBaseURI(server));
        String xml = target.path("rest").path("getScheduledJobs").request().accept(MediaType.APPLICATION_XML).get(String.class);
        if (xml.contains("<scheduleJob>") && xml.contains("</scheduleJob>")){
            //some code
       }
        model.addObject("scheduleJobsList", scheduleJobsList);
        return model;

     }

    private static URI getBaseURI(Server server) {
        if(server.getSecureFlag().equalsIgnoreCase("N")){
            return UriBuilder.fromUri("http://"+server.getServerIp()+":"+server.getServerPort()+"/jobserver").build();
        } else {
            return UriBuilder.fromUri("https://"+server.getServerIp()+":"+server.getServerPort()+"/jobserver").build();
        }
    }
}

Here's my Test Code.

这是我的测试代码。

@RunWith(MockitoJUnitRunner.class)
public class JobControllerTest {

    @Mock
    private WebTarget target;

    @Mock
    private ClientConfig config;

    @Mock
    private Client client;

    @Test
    public void testdisplayScheduledJobs() throws JAXBException {

        List<Server> serverList = new ArrayList<Server>();
        Server server = new Server();
        server.setSecureFlag("N");
        serverList.add(server);
        config = new ClientConfig();
        client = ClientBuilder.newClient(config);
        String xml = "<scheduledJobs>" + "</scheduledJobs>";

        when(serverService.getJobServerList(user.getAccountId())).thenReturn(serverList);
        when(client.target(getBaseURI(server))).thenReturn(target);


        when(target.path("rest")).thenReturn(target); //here the error is getting generated
        when(target.path("getScheduledJobs")).thenReturn(target);
        when(target.request()).thenReturn(builder);
        when(builder.accept(MediaType.APPLICATION_XML)).thenReturn(builder);
        when(builder.get(String.class)).thenReturn(xml);

        model = jobController.displayScheduledJobs(server, request);

        assertEquals("displayScheduledJobs", model.getViewName());

    }
}

Please suggest a way out for this

请为此提出一个出路

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.
2. inside when() you don't call method on mock but on some other object.

at com.techm.job.administration.console.controller.JobControllerTest.testdisplayScheduledJobs(JobControllerTest.java:309)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.junit.runners.model.FrameworkMethod.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access
client = ClientBuilder.newClient(config);
0(ParentRunner.java:42) at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:184) at org.junit.runners.ParentRunner.run(ParentRunner.java:236) 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:50) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

回答by user2004685

You are getting the error because of the following line in your code:

由于代码中的以下行,您收到错误消息:

public class Foo {

    @Mock
    private JerseyWebTarget target;

    @Mock
    private Builder requestBuilder;

    @Mock
    private Response serviceResponse;

    @InjectMocks
    private Foo foo = new Foo();

    @Rule
    public ExpectedException thrown = ExpectedException.none();

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

        Mockito.when(target.queryParam(Matchers.anyString(), Matchers.anyString()))
                .thenReturn(target);

        Mockito.when(target.queryParam(Matchers.anyString(), Matchers.anyString()))
                .thenReturn(target);

        Mockito.when(target.queryParam(Matchers.anyString(), Matchers.anyString()))
                .thenReturn(target);

        Mockito.when(target.queryParam(Matchers.anyString(), Matchers.anyString()))
                .thenReturn(target);

        Mockito.when(target.request(MediaType.APPLICATION_JSON_TYPE)).thenReturn(
                requestBuilder);
    }

    @Test
    public void testGetData() {
        List<String> responseList = new ArrayList<>();
        responseList.add("foobar");

        Mockito.when(requestBuilder.get(Matchers.eq(Response.class))).thenReturn(serviceResponse);

        Mockito.when(serviceResponse.getStatus()).thenReturn(200);

        Mockito.when(serviceResponse.readEntity(Matchers.any(GenericType.class))).thenReturn(
                responseList);

        List<String> resultList = foo.getData("foo", "bar");

        Mockito.verify(requestBuilder, Mockito.times(1)).get(Matchers.eq(Response.class));
        Mockito.verify(serviceResponse, Mockito.times(1)).getStatus();
        Mockito.verify(serviceResponse, Mockito.times(1)).readEntity(
                Matchers.any(GenericType.class));

        assertNotNull(resultList);
        assertEquals(1, resultList.size());
        assertEquals("true", resultList.get(0).getConfigSetValue());
    }
}

When you write this statement, it replaces the mock object of client which you have created above with the new Client and hence clientwill no longer be recognized as a mock by Mockito.

当您编写此语句时,它将用新客户端替换您在上面创建的客户端的模拟对象,因此client将不再被 Mockito 识别为模拟。

Also, I hope you are creating the mock of builderas you are doing when(builder.accept(MediaType.APPLICATION_XML)).thenReturn(builder);.

另外,我希望你创建的模拟builder,你正在做的when(builder.accept(MediaType.APPLICATION_XML)).thenReturn(builder);

In addition, I can't see any @InjectMocksstatements. You'll have to create an object of the class you are testing and annotate it with @InjectMocksannotation.

另外,我看不到任何@InjectMocks陈述。您必须创建您正在测试的类的对象并使用注释对其进行@InjectMocks注释。

Also, you'll have to initialize your mocks by doing MockitoAnnotations.initMocks(this);in your testdisplayScheduledJobs()method.

此外,您必须通过MockitoAnnotations.initMocks(this);在您的testdisplayScheduledJobs()方法中执行来初始化您的模拟。

Here is a sample code snippet which will give you more insights:

这是一个示例代码片段,它将为您提供更多见解:

##代码##