<commit_before>/*
 * Copyright (c) 2007 Mockito contributors
 * This program is made available under the terms of the MIT License.
 */
package org.mockitousage.examples.configure;
import static org.mockito.Mockito.*;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import org.mockito.TestBase;
import org.mockito.configuration.DefaultReturnValues;
import org.mockito.configuration.MockitoConfiguration;
import org.mockito.configuration.ReturnValues;
import org.mockito.internal.invocation.InvocationOnMock;

public class ConfiguringDefaultReturnValuesTest extends TestBase {
    
    @Test
    public void shouldReturnMocksByDefaultInsteadOfNulls() throws Exception {
        MyObject m = mock(MyObject.class);
        MyObject returned = m.foo();
        assertNotNull(returned);
        assertNotNull(returned.foo());
    }

    interface MyObject {
        MyObject foo();
    }
    
    //Configuration code below is typically hidden in a base class/test runner/some kind of static utility
    
    @Before
    public void configureDefaultReturnValues() {
        MockitoConfiguration.instance().setReturnValues(new MyDefaultReturnValues());
    }
    
    @After
    public void resetDefaultReturnValues() {
        MockitoConfiguration.instance().resetReturnValues();
        
    }
    
    private final class MyDefaultReturnValues implements ReturnValues {
        public Object valueFor(InvocationOnMock invocation) {
            Object value = new DefaultReturnValues().valueFor(invocation);
            if (value != null || invocation.getMethod().getReturnType() == Void.TYPE) {
                return value;
            } else {
                return Mockito.mock(invocation.getMethod().getReturnType());
            }
        }
    }
}<commit_msg>extracted InvocationOnMock interface<commit_after>/*
 * Copyright (c) 2007 Mockito contributors
 * This program is made available under the terms of the MIT License.
 */
package org.mockitousage.examples.configure;
import static org.mockito.Mockito.*;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import org.mockito.TestBase;
import org.mockito.configuration.DefaultReturnValues;
import org.mockito.configuration.MockitoConfiguration;
import org.mockito.configuration.ReturnValues;
import org.mockito.internal.invocation.InvocationOnMock;

public class ConfiguringDefaultReturnValuesTest extends TestBase {
    
    @Test
    public void shouldReturnMocksByDefaultInsteadOfNulls() throws Exception {
        MyObject m = mock(MyObject.class);
        MyObject returned = m.foo();
        assertNotNull(returned);
        assertNotNull(returned.foo());
    }

    interface MyObject {
        MyObject foo();
    }
    
    //Configuration code below is typically hidden in a base class/test runner/some kind of static utility
    
    @Before
    public void configureDefaultReturnValues() {
        MockitoConfiguration.instance().setReturnValues(new MyDefaultReturnValues());
    }
    
    @After
    public void resetDefaultReturnValues() {
        MockitoConfiguration.instance().resetReturnValues();
    }
    
    private final class MyDefaultReturnValues implements ReturnValues {
        public Object valueFor(InvocationOnMock invocation) {
            Object value = new DefaultReturnValues().valueFor(invocation);
            if (value != null || invocation.getMethod().getReturnType() == Void.TYPE) {
                return value;
            } else {
                return Mockito.mock(invocation.getMethod().getReturnType());
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.08616904500548847, 'fraction_numerical': 0.0021953896816684962, 'mean_word_length': 3.189655172413793, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6544443', 'n_tokens_mistral': 945, 'n_tokens_neox': 919, 'n_words': 230}