java-testing-junit-mockito

Featured

Use when writing tests for Java (Quarkus or Spring) code - JUnit 5 structure, Mockito for collaborators, and framework-native integration tests, test-first

Testing & QA 91 stars 13 forks Updated today Apache-2.0

Install

View on GitHub

Quality Score: 89/100

Stars 20%
65
Recency 20%
100
Frontmatter 20%
70
Documentation 15%
100
Issue Health 10%
50
License 10%
100
Description 5%
100

Skill Content

# Java Testing (JUnit 5 + Mockito) ## Overview Test-first in Java, same discipline as everywhere (see tdd-workflow): write the failing test, watch it fail, minimal code to pass. This skill is the Java mechanics for that cycle. **Core principle:** Unit-test the domain and services as plain Java without booting the framework. Reserve framework-integration tests for the boundary (HTTP, persistence). ## Test layers | What | Test type | Tools | |------|-----------|-------| | Domain entity / value object | Pure unit, no mocks | JUnit 5 | | Service (mock its ports) | Unit with mocks | JUnit 5 + Mockito | | Resource/Controller + DB | Integration (boots framework) | `@QuarkusTest` / `@SpringBootTest`, Testcontainers for DB | Most tests are the first two — fast, no container. Boot the framework only where you're testing wiring, serialization, or SQL. ## JUnit 5 + Mockito unit test ```java class TaskServiceTest { private final TaskRepository repo = mock(TaskRepository.class); private final TaskService service = new TaskService(repo); // constructor injection pays off @Test void create_persists_and_returns_task() { Task t = service.create("Write the report"); verify(repo).persist(any(Task.class)); // or save(...) for Spring assertEquals("Write the report", t.title()); } @Test void create_rejects_blank_title() { assertThrows(InvalidTaskTitle.class, () -> service.create(" ")); verifyNoInteractions(repo); ...

Details

Author
makifbaysal
Repository
makifbaysal/tasktrooper
Created
1 weeks ago
Last Updated
today
Language
Go
License
Apache-2.0

Similar Skills

Semantically similar based on skill content — not just same category