File tree Expand file tree Collapse file tree
main/java/com/codeherk/taskapi
test/java/com/codeherk/taskapi/service Expand file tree Collapse file tree Original file line number Diff line number Diff line change 2525 <groupId >org.springframework.boot</groupId >
2626 <artifactId >spring-boot-starter-web</artifactId >
2727 </dependency >
28-
29- <dependency >
30- <groupId >com.h2database</groupId >
31- <artifactId >h2</artifactId >
32- <scope >runtime</scope >
33- </dependency >
3428 <dependency >
3529 <groupId >org.springframework.boot</groupId >
3630 <artifactId >spring-boot-starter-test</artifactId >
4236 <version >8.0.19</version >
4337 <scope >runtime</scope >
4438 </dependency >
39+ <dependency >
40+ <groupId >junit</groupId >
41+ <artifactId >junit</artifactId >
42+ <scope >test</scope >
43+ </dependency >
4544 </dependencies >
4645
47-
4846 <build >
4947 <plugins >
5048 <plugin >
Original file line number Diff line number Diff line change 1717public class TaskController {
1818
1919 @ Autowired
20- private final TaskService taskService ;
20+ private TaskService taskService ;
2121
22- TaskController (TaskService taskService ) {
23- this .taskService = taskService ;
24- }
22+ // Constructors are inferred
23+ // TaskController(){}
2524
2625
2726 // Aggregate root
Original file line number Diff line number Diff line change 1111@ Service
1212public class TaskService {
1313 @ Autowired
14- private final TaskRepository repository ;
14+ private TaskRepository repository ;
1515
16- public TaskService (TaskRepository repository ) {
17- this .repository = repository ;
18- }
16+ // public TaskService() {}
1917
2018 public List <Task > getTasks () {
2119 return repository .findAll ();
Original file line number Diff line number Diff line change 1+ package com .codeherk .taskapi .service ;
2+
3+ import com .codeherk .taskapi .model .Task ;
4+ import com .codeherk .taskapi .repo .TaskRepository ;
5+ import org .junit .Test ;
6+ import org .junit .runner .RunWith ;
7+ import org .mockito .InjectMocks ;
8+ import org .mockito .Mock ;
9+ import org .mockito .Mockito ;
10+ import org .mockito .junit .MockitoJUnitRunner ;
11+
12+ import java .util .Optional ;
13+
14+ import static org .assertj .core .api .Assertions .assertThat ;
15+
16+ @ RunWith (MockitoJUnitRunner .class )
17+ public class TaskServiceTest {
18+
19+ @ Mock
20+ private TaskRepository taskRepository ;
21+
22+ @ InjectMocks
23+ private TaskService taskService ;
24+
25+ @ Test
26+ public void whenValidName_thenEmployeeShouldBeFound () {
27+ Task t = new Task ("Hello world!" );
28+ Mockito .when (taskRepository .findById (0L )).thenReturn (Optional .of (t ));
29+
30+ Long taskId = 0L ;
31+ Task found = taskService .getTaskById (taskId );
32+
33+ assertThat (found .getDescription ())
34+ .isEqualTo (found .getDescription ());
35+ }
36+ }
You can’t perform that action at this time.
0 commit comments