Code Monkey home page Code Monkey logo

Comments (2)

skinny85 avatar skinny85 commented on June 26, 2024

Right now, you can save some boilerplate by using MockitoAnnotations.initMocks:

import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.specnaz.junit.SpecnazJUnit;

import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

public class UsingMockitoSpec extends SpecnazJUnit {
    @Mock
    List<String> list;

    {
        describes("A Mockito test", it -> {
            it.beginsEach(() -> {
                MockitoAnnotations.initMocks(this);
            });

            it.should("initialize the mocks correctly", () -> {
                assertThat(list.get(99)).isNull();
            });
        });
    }
}

Whatever design we come up with, I would assume it would do something very similar to above. The question then becomes, is it worth to develop a feature just to save one beginsEach method? (I don't know; this is not a sarcastic question, I'm honestly curious whether it's a good trade-off).

Another thing is that there might be legitimate cases when you want to init the mocks in a beginsAll instead of a beginsEach - the implemented solution would have to allow you to switch between the 2 modes (I would most likely make the beginsEach behavior the default).

Pros of developing this solution:

  • save a little boilerplate

Cons:

  • introduce some (potentially) surprising behavior (it might not be obvious when and how are the mocks being initialized)
  • increased implementation complexity

If we do choose to implement this feature, probably the MockitoSession API would come in handy.

from specnaz.

skinny85 avatar skinny85 commented on June 26, 2024

This has been done, but in a rather different way than I originally planned. It's possible thanks to the JUnit Rules support that has been added in version 1.3.

Example:

public class MockitoExampleSpec extends SpecnazJUnit {
    public Rule<MockitoRule> mockitoRule = Rule.of(() -> MockitoJUnit.rule());

    @Mock
    private List<Integer> listMock;

    {
        describes("Using the JUnit Mockito Rule in Specnaz", it -> {
            it.should("initialize fields annotated with @Mock", () -> {
                when(listMock.get(0)).thenReturn(400 + 56);

                assertEquals(456, (int) listMock.get(0));
            });
        });
    }
}

Because of that, I'm resolving this one.

from specnaz.

Related Issues (10)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.