Skip to content

Commit 1a0f66f

Browse files
committed
Unit test the analytics-microservice
1 parent 62eeb4e commit 1a0f66f

File tree

6 files changed

+104
-13
lines changed

6 files changed

+104
-13
lines changed

analytics-service/src/test/java/com/example/analytics_service/AbstractIntegrationTests.java renamed to analytics-service/src/test/java/com/example/analytics_service/intergration_tests/AbstractIntegrationTests.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.example.analytics_service;
1+
package com.example.analytics_service.intergration_tests;
22

33
import com.example.analytics_service.dto.ProductViewDTO;
44
import com.example.analytics_service.events.ProductEvent;
@@ -8,18 +8,12 @@
88
import org.springframework.boot.test.context.SpringBootTest;
99
import org.springframework.cloud.stream.function.StreamBridge;
1010
import org.springframework.context.annotation.Import;
11-
import org.springframework.core.ParameterizedTypeReference;
12-
import org.springframework.http.MediaType;
1311
import org.springframework.kafka.test.context.EmbeddedKafka;
1412
import org.springframework.test.annotation.DirtiesContext;
1513
import org.springframework.test.web.reactive.server.WebTestClient;
1614
import reactor.core.publisher.Flux;
1715
import reactor.test.StepVerifier;
1816

19-
import java.time.Duration;
20-
import java.util.List;
21-
import java.util.function.Consumer;
22-
2317
import static org.junit.jupiter.api.Assertions.assertEquals;
2418

2519
@Import(TestcontainersConfiguration.class)

analytics-service/src/test/java/com/example/analytics_service/AnalyticsServiceTests.java renamed to analytics-service/src/test/java/com/example/analytics_service/intergration_tests/AnalyticsServiceTests.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
package com.example.analytics_service;
1+
package com.example.analytics_service.intergration_tests;
22

3-
import com.example.analytics_service.dto.ProductViewDTO;
43
import org.junit.jupiter.api.Test;
54
import org.springframework.test.annotation.DirtiesContext;
65

7-
import java.util.stream.Collectors;
8-
96
import static org.junit.jupiter.api.Assertions.assertEquals;
107

118
public class AnalyticsServiceTests extends AbstractIntegrationTests{

analytics-service/src/test/java/com/example/analytics_service/TestAnalyticsServiceApplication.java renamed to analytics-service/src/test/java/com/example/analytics_service/intergration_tests/TestAnalyticsServiceApplication.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
package com.example.analytics_service;
1+
package com.example.analytics_service.intergration_tests;
22

3+
import com.example.analytics_service.AnalyticsServiceApplication;
34
import org.springframework.boot.SpringApplication;
45

56
public class TestAnalyticsServiceApplication {

analytics-service/src/test/java/com/example/analytics_service/TestcontainersConfiguration.java renamed to analytics-service/src/test/java/com/example/analytics_service/intergration_tests/TestcontainersConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.example.analytics_service;
1+
package com.example.analytics_service.intergration_tests;
22

33
import org.springframework.boot.test.context.TestConfiguration;
44
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.example.analytics_service.unit_tests;
2+
3+
import com.example.analytics_service.repo.ProductViewRepo;
4+
import com.example.analytics_service.service.ProductViewService;
5+
import org.junit.jupiter.api.extension.ExtendWith;
6+
import org.mockito.InjectMocks;
7+
import org.mockito.Mock;
8+
import org.mockito.junit.jupiter.MockitoExtension;
9+
10+
@ExtendWith(MockitoExtension.class)
11+
public class AbstractUnitTest {
12+
@Mock
13+
protected ProductViewRepo repo;
14+
15+
@InjectMocks
16+
protected ProductViewService service;
17+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package com.example.analytics_service.unit_tests;
2+
3+
import com.example.analytics_service.entity.ProductView;
4+
import com.example.analytics_service.events.ProductEvent;
5+
import org.junit.jupiter.api.DisplayName;
6+
import org.junit.jupiter.api.Test;
7+
import org.mockito.Mockito;
8+
import reactor.core.publisher.Flux;
9+
import reactor.core.publisher.Mono;
10+
import reactor.test.StepVerifier;
11+
12+
public class ProductViewServiceTests extends AbstractUnitTest{
13+
14+
@Test
15+
@DisplayName("Test findAll")
16+
void testFindAll_whenInvoked_ThenReturnFluxOfProducts() {
17+
//Arrange
18+
var flux = Flux.just(
19+
ProductView.builder().viewCount(25L).productCode("P111").build(),
20+
ProductView.builder().viewCount(15L).productCode("P151").build(),
21+
ProductView.builder().viewCount(5L).productCode("P113").build()
22+
);
23+
24+
Mockito.when(repo.findTop5ByOrderByViewCountDesc()).thenReturn(flux);
25+
26+
//Act and Assert
27+
28+
service.products()
29+
.collectList()
30+
.as(StepVerifier::create)
31+
.expectNextMatches(list -> list.size() == 3)
32+
.verifyComplete();
33+
}
34+
35+
@Test
36+
@DisplayName("Test consume() for new product")
37+
void testConsume_WhenNewProductIsConsumed_ThenItsViewCountInitializedAtOne(){
38+
//Arrange
39+
var event = ProductEvent.View.builder().code("P111").build();
40+
var expectedProductView = Mono.fromSupplier(() ->
41+
ProductView.builder().viewCount(1L).productCode("P111").build());
42+
43+
Mockito.when(repo.findByProductCode(Mockito.anyString())).thenReturn(Mono.empty());
44+
Mockito.when(repo.save(Mockito.any(ProductView.class))).thenReturn(expectedProductView);
45+
46+
//Act
47+
service.consume().apply(event)
48+
.as(StepVerifier::create)
49+
.verifyComplete();
50+
51+
//Assert
52+
Mockito.verify(repo, Mockito.times(1)).save(Mockito.argThat(
53+
x-> x.getViewCount() == 1 && x.getProductCode().equals("P111")));
54+
55+
}
56+
57+
58+
@Test
59+
@DisplayName("Test consume() for existing product")
60+
void testConsume_WhenExistingProductIsConsumed_ThenItsViewCountIncremented(){
61+
//Arrange
62+
var event = ProductEvent.View.builder().code("P111").build();
63+
64+
var existingProductView = Mono.fromSupplier(() ->
65+
ProductView.builder().viewCount(17L).productCode("P111").build());
66+
var expectedProductView = Mono.fromSupplier(() ->
67+
ProductView.builder().viewCount(18L).productCode("P111").build());
68+
69+
Mockito.when(repo.findByProductCode(Mockito.anyString())).thenReturn(existingProductView);
70+
Mockito.when(repo.save(Mockito.any(ProductView.class))).thenReturn(expectedProductView);
71+
72+
//Act
73+
service.consume().apply(event)
74+
.as(StepVerifier::create)
75+
.verifyComplete();
76+
77+
//Assert
78+
Mockito.verify(repo, Mockito.times(1)).save(Mockito.argThat(
79+
x-> x.getViewCount() == 18L && x.getProductCode().equals("P111")));
80+
81+
}
82+
}

0 commit comments

Comments
 (0)