Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/fix pact tests #23

Merged
merged 2 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package se.richardalm.engineheater;

import jakarta.inject.Inject;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import org.jboss.resteasy.reactive.RestPath;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
package se.richardalm.engineheater;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.validation.constraints.NotEmpty;
import se.richardalm.engineheater.external.TemperatureService;
import se.richardalm.statistic.StatisticService;

@ApplicationScoped
public class WhenToStartService {
//@Inject
@Inject
TemperatureService temperatureService;

//@Inject
@Inject
StatisticService statisticService;

public WhenToStartService(TemperatureService temperatureService, StatisticService statisticService) {
this.temperatureService = temperatureService;
this.statisticService = statisticService;
}

public Integer whenToStart(@NotEmpty(message = "City must be specificed") String city,
@NotEmpty(message = "Car must be specified") String car) {
var tempterature = temperatureService.getTemperature(city);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ class StatisticResourceTest {
void getBills() {
var bill = given()
.contentType(ContentType.JSON)
.when().get("/bills")
.when()
.get("/bills")
.getBody();

assertThat(bill, is(notNullValue()));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
package se.richardalm.engineheater;

import io.quarkus.test.InjectMock;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.mockito.InjectMock;
import io.quarkus.test.junit.mockito.InjectSpy;
import jakarta.inject.Inject;
import org.eclipse.microprofile.rest.client.inject.RestClient;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import se.richardalm.engineheater.external.TemperatureApi;
import se.richardalm.engineheater.external.TemperatureService;
import se.richardalm.statistic.StatisticService;

import java.util.stream.Stream;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.isA;
import static org.mockito.Mockito.*;

@QuarkusTest
Expand All @@ -27,31 +23,20 @@ class WhenToStartServiceTest {
WhenToStartService service;

@InjectMock
@RestClient
TemperatureApi temperatureApi;
TemperatureService temperatureService;

@InjectSpy
StatisticService billingService;

@ParameterizedTest
@MethodSource("data")
void good(Integer temperature, Integer time) {
when(temperatureApi.getTemperature(isA(String.class))).thenReturn(temperature);
void calcuateCorrectTime(Integer temperature, Integer time) {
when(temperatureService.getTemperature("Luleå")).thenReturn(temperature);

var hours = service.whenToStart("Luleå", "ABC123");

assertThat(hours, is(time));
verify(billingService, times(1)).saveStatistic(any(), any());
}

@Test
void failure() {
when(temperatureApi.getTemperature(isA(String.class))).thenThrow(new RuntimeException());

var hours = service.whenToStart("Luleå", "ABC123");

assertThat(hours, is(4));
verify(billingService, times(1)).saveStatistic(any(), any());
verify(billingService, times(1)).saveStatistic("ABC123", time);
}

private static Stream<Arguments> data() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package se.richardalm.engineheater.external;

import io.quarkus.test.InjectMock;
import io.quarkus.test.junit.QuarkusTest;
import jakarta.inject.Inject;
import org.checkerframework.checker.units.qual.Temperature;
import org.eclipse.microprofile.rest.client.inject.RestClient;
import org.junit.jupiter.api.Test;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

@QuarkusTest
class TemperatureServiceTest
{
@InjectMock
@RestClient
TemperatureApi temperatureApi;

@Inject
TemperatureService service;

@Test
void failureGettingTemperature() {
when(temperatureApi.getTemperature("Luleå")).thenThrow(new RuntimeException());

var temperature = service.getTemperature("Luleå");

assertThat(temperature, is(-40));
}
}
Loading