Using mockConstruction with ProcessBuilder

2,129 views
Skip to first unread message

Roman Levin

unread,
Sep 10, 2020, 9:19:42 AM9/10/20
to mockito
Sorry if this is an incredibly basic question, but I'm new to Java, as well as to Mockito.

I'm using mockConstruction to mock out ProcessBuilder. What I don't understand is the proper way to configure the behavior of the Process mock that is returned when ProcessBuilder::start() is called.

In code form, I'm trying to do something like this:

void testedFunction() {
  String[] cmd = {"foo"};
  ProcessBuilder pb = ProcessBuilder("foo");
  Process process = pb.start();
  
  // How do I, for example, make `finished == true` here?
  boolean finished = process.waitFor(5, TimeUnit.SECONDS))
}

@Test
void test() {
  try (MockedConstruction<ProcessBuilder> mocked = mockConstruction(ProcessBuilder.class, withSettings().defaultAnswer(RETURNS_MOCKS))) {
    testedFunction();
  }
}

This question is applicable to any builder-patterned type, I guess, but I thought it would be better to be specific.

Tim van der Lippe

unread,
Sep 10, 2020, 11:49:28 AM9/10/20
to mockito
Rather than using `RETURNS_MOCKS`, you want more control over your mocks (good!). Therefore, I would change it such that you return a new mock for the `start` invocation. Then you can make sure that the `waitFor` on the mock (you just defined), returns what you want (in this case `true`).

Op do 10 sep. 2020 om 14:19 schreef Roman Levin <roman...@gmail.com>:
--
You received this message because you are subscribed to the Google Groups "mockito" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mockito+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mockito/0e48fb50-e531-43d1-abbf-1c28c58aef9bn%40googlegroups.com.

Roman Levin

unread,
Sep 10, 2020, 2:56:43 PM9/10/20
to mockito
On Thursday, September 10, 2020 at 5:49:28 PM UTC+2 tvande...@gmail.com wrote:
I would change it such that you return a new mock for the `start` invocation.
Thanks for your reply! Yeah, that's what I want to do, but I'm just not sure what the right incantation is to do that. Do I define a custom Answer? If so, could you maybe point me at the appropriate documentation for doing this correctly? I kind of tried to reverse engineer it from the Mockito source code, but I just don't have enough context to easily figure it out on my own.

Roman Levin

unread,
Sep 14, 2020, 9:28:57 AM9/14/20
to mockito

For the record, had to do something like:

MockedConstruction<ProcessBuilder> mocked = mockConstruction(
    ProcessBuilder.class,
    context -> new MockSettingsImpl<>().defaultAnswer(RETURNS_MOCKS),
    (builderMock, context) -> {
        Process processMock = mock(Process.class);
        when(builderMock.start()).thenReturn(processMock);
        when(processMock.waitFor(5L, TimeUnit.SECONDS)).thenReturn(true);

...

Don't know if it's the minimal or most elegant solution, but it works.
Reply all
Reply to author
Forward
0 new messages