Giter Site home page Giter Site logo

nomock's Introduction

Nomock

Spring Data JPA repositories backed by HashMap (alternative for mocks).

If you dont want to use Testcontainers or H2 database, its worth to considering in-memory repository instead of mocks.

Features

  • CrudRepository (fully supported)
  • PagingAndSortingRepository (fully supported)
  • JpaRepository (queries by Example not implemented)

What are the benefits of using HashMap over repository mocks?

  • to write tests, we dont have to investigate how service use repository
  • service can be tested as blackbox
    • give input and verify output
    • refactor with ease

Requirements

  • entity must have ID field annotated with @Id

Example

Simple Spring Boot app:

@Service
class BookService {

    private final BookRepository bookRepository;

    @Autowired
    BookService(BookRepository bookRepository) {
        this.bookRepository = bookRepository;
    }
}
@Repository
interface BookRepository extends JpaRepository<BookEntity, Long> {
}
@Entity
public final class BookEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String name;

    //...

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        BookEntity that = (BookEntity) o;
        return Objects.equals(id, that.id);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }
}

How to test BookService using in-memory repository?

  1. Create new repository class, that
    • extends one of three available repositories: InMemoryJpaRepository or InMemoryCrudRepository or InMemoryPagingAndSortingRepository. In this case we use InMemoryJpaRepository
    • implements entity`s repository interface
class InMemoryBookRepository extends InMemoryJpaRepository<BookEntity, Long> implements BookRepository {
    public InMemoryBookRepository() {
        super(1L, IdGenerators.IncrementalLongIdGenerator);
    }
}
  1. Pass instance of this class to service: BookServiceTest.java
public class BookServiceTest {

    BookService bookService = new BookService(new InMemoryBookRepository());

    @Test
    public test() {
        //...
    }
}
  1. Everything is ready to write tests

Check out entire code of this example:

nomock's People

Contributors

potat0x avatar

Watchers

 avatar

Forkers

ashutosh16 nawrok

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.