Git Product home page Git Product logo

tdd-with-spring-2.0's Introduction

Documentation

Class-level annotations

  • @ExtentWith(MockitoExtension.class): Use to add Mockito behaviour into JUnit5 lifecycle. Just an extension to add third-party vendors like Mockito for test classes. We use it when we don't want to involve Spring.
  • @ExtendWith(SpringExtension.class): When we want to use @MockBean annotation involving Spring. We use this annotation which is replaces deprecated JUnit4 @RunWith(SpringJUnit4ClassRunner.class) annotation.

Dependency-level annotations

  • @InjectMocks: It will inject the dependency as well as also constructor-inject all the mocked dependencies (like repository-layer). Those dependencies annotated with @Mock.
  • @Mock: It will mock the class (like repository-layer).

Behaviours

  • Mocking

Mocking is the technique to mock (mimic) the data of your repository layer. When writing unit tests, our tests needs to be fast, so if hit the actual database connection it will take much time to run the test as well as we don't want to do testing on our persistance data.

  • Stubbing

Stubbing is another technique to mimic the data we need to test. Let's say we only want to save a new Todo if already not found in database. How to do testing? Since we have mocked the repository (and we also using H2 in-memory) so there will be no actual data present in database. Since we need to first check if Todo already exists, it won't be possible because of absence of any data. Stubbing allows us to stub the mock repository which means before assertion we will tell the repository to return newTodoToSave so when invoking addNewTodo method of your service it will throw exception since new Todo already found in database.

tdd-with-spring-2.0's People

Watchers

 avatar

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.