简体   繁体   中英

How to set Mock to have a default behavior and can override it in some test

I want to mock a dependency and return a default value in most test cases since most of them should not care about the values returned but there are some certain cases like I would like to test like the dependency returns some weird values or just throw. So I am modeling it in this way. Most cases, it should return a nice and valid value.

Test Setup which return the 20L by default for all test classes.

Dependency dependency = Mockito.mock(Dependency.class);
when(dependency.returnSomeVal()).thenReturn(20L);

In a specific test cases class, I would like to override the behavior like below:

when(dependency.returnSomeVal()).thenThrow(); //failure cases
when(dependency.returnSomeVal()).thenReturn(Weird_Val); //failure cases

But I don't find a good solution to override the existing behavior? Any idea?

You can reset the mock and add behavior. In the test, do

Mockito.reset(dependency);
when(dependency.returnSomeVal()).thenThrow(); //failure cases
when(dependency.returnSomeVal()).thenReturn(Weird_Val); //failure cases

Resetting will remove all mocked behavior on this class though. If you want to remock only some methods, then you have to create the mock from scratch.

I ended using myself this pattern to mock a bunch of methods of a class providing configurations.

In a @Before method I setup a bunch of stubs for a mocked object that provide a correct configuration for each test. Afterwards, in each test it was extremely convenient to only override one of those stubs to provide a different configuration and test a different error case.

I think the response from Hari Menon is correct but it somehow defeats the purpose explained in the question. If the mock is reset, all the stubs would need to be added again, making this pattern very confusing (it would be better to not use any overriding than using reset in this case, the code would be way more straightforward).

The comments added to the question provide indeed an indirect answer on how to achieve this, and why it works, but it took me a bit to get it working.

In spite of one of the comments, I made everything work by using in my @Before fixture when().thenReturn() and overriding the concrete stub with doReturn().when()

Example:

public class WorkerTest {

    private ConfigProvider mockedConfigProvider = mock(ConfigProvider.class);

    @Before
    public void setup() {
    // Setup stubs with a correct config
        when(mockedConfigProvider.getValue("property1")).thenReturn("value1");
        when(mockedConfigProvider.getValue("property2")).thenReturn("value2");
        when(mockedConfigProvider.getValue("property3")).thenReturn("value3");
        when(mockedConfigProvider.getValue("property4")).thenReturn("value4");
    }

    @Test
    public void test_GoodConfig(){
        // The config object gets injected in the test worker
        Worker testWorker = new Worker(mockedConfigProvider);
        // testWorker.execute() returns true if everything went well
        assertTrue(testWorker.execute());
    }

    @Test
    public void test_BadConfigProp1(){
        // Test now with a broken 'property1', overriding that stub.
        doReturn(null).when(mockedConfigProvider).getValue("property1");
        Worker testWorker = new Worker(mockedConfigProvider);
        // testWorker.execute() returns false if there is a problem.
        assertFalse(testWorker.execute());
    }

    @Test
    public void test_BadConfigProp2(){
    // This test needs to only override the result of property2
    doReturn("crazy result").when(mockedConfigProvider).getValue("property2");
    ...

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM