Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Constructor not called when using mockito-inline (3.5.x) #2012

Closed
5 tasks done
fschmager opened this issue Aug 20, 2020 · 0 comments
Closed
5 tasks done

Constructor not called when using mockito-inline (3.5.x) #2012

fschmager opened this issue Aug 20, 2020 · 0 comments

Comments

@fschmager
Copy link

fschmager commented Aug 20, 2020

I'm using mock() withSettings().useConstructor(aParam) which worked with mockito-inline 3.4.6 but stopped working with 3.5.0.

It seems like the parameter is not being passed to the constructor anymore. I've put a breakpoint in the constructor and it doesn't get hit with 3.5.0 but it does get hit with 3.4.6.

Not an issue with mockito-core 3.5.0

I suspect a change as part of PR #2004 might be a clue. Commit messages talking about constructors...

Versions

  • mockito-inline 3.4.6 <- working
  • mockito-inline 3.5.0 <- not working
  • Java: 12.0.2, vendor: AdoptOpenJDK
  • OS: macos 10.15.6
  • junit jupiter: 5.6.2

Maven Dependencies

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-inline</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-junit-jupiter</artifactId>
        </dependency>

Example to reproduce

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.withSettings;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoSettings;

@MockitoSettings
public class ConstructorTest {

    private static final String SOME_VALUE = "some value";

    private SomethingAbstract somethingAbstract;

    @BeforeEach
    void setup() {
        somethingAbstract = mock(SomethingAbstract.class, withSettings()
                .useConstructor(SOME_VALUE)
                .defaultAnswer(Mockito.CALLS_REAL_METHODS));
    }

    @Test
    void shouldUseConstructor() {
        assertThat(somethingAbstract.getSomeField()).isSameAs(SOME_VALUE);
    }

    static abstract class SomethingAbstract {
        private final String someField;

        SomethingAbstract(String someField) {
            this.someField = someField;
        }

        public String getSomeField() {
            return someField;
        }
    }
}

This is the error when running with 3.5.x

java.lang.AssertionError: 
Expecting:
 <"some value">
and actual:
 <null>
to refer to the same object

Checklist

  • The mockito message in the stacktrace have useful information, but it didn't help
  • The problematic code (if that's possible) is copied here;
    Note that some configuration are impossible to mock via Mockito
  • Provide versions (mockito / jdk / os / any other relevant information)
  • Provide a Short, Self Contained, Correct (Compilable), Example of the issue
    (same as any question on stackoverflow.com)
  • Read the contributing guide
@raphw raphw closed this as completed in 7ee8518 Aug 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant