Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

ScoreCleaner cleaner = action -> action.run(); // or Runnable::run
@Test
void delete() {
var profile = ScoringProfile.builder().id(profileId).build();
when(versioner.nextVersion(profile)).thenReturn(version);
when(persisterSupplier.getPersister(profileId, version)).thenReturn(persister);
recalculateService.calculateAndPersist(profile);
ScoreCleaner cleaner = mock(ScoreCleaner.class);
@Test
void delete() {
// I don't care whether the profile is default of not
var profile = ScoringProfile.builder().id(profileId).build();
when(versioner.nextVersion(profile)).thenReturn(version);
when(persisterSupplier.getPersister(profileId, version)).thenReturn(persister);
recalculateService.calculateAndPersist(profile);
class CalculateScoresServiceTest {
ScorePersisterSupplier persisterSupplier = mock(ScorePersisterSupplier.class);
ScoreVersioner versioner = mock(ScoreVersioner.class);
ScoreCalculator calculator = mock(ScoreCalculator.class);
int inactiveScoresToKeep = 2;
ScorePersister persister = mock(ScorePersister.class);
String profileId = "::profileId::";
int version = 1;
CalculateScoresService recalculateService = new CalculateScoresService(
static Stream<Arguments> deleteParams() {
return Stream.of(
Arguments.of(true, inactiveScoresToKeep),
Arguments.of(false, 0)
);
}
@ParameterizedTest
@MethodSource("deleteParams")
void delete(boolean isDefault, int expectedScoresToKeep) {
@Test
void deleteDefault() {
var profile = ScoringProfile.builder().id(profileId).defaultProfile(true).build();
setupStubs(profile);
recalculateService.calculateAndPersist(profile);
verifyMocks(profile, inactiveScoresToKeep);
}
inOrder.verify(persister).deleteInactive(profileId, inactiveScoresToKeep);
inOrder.verify(versioner).deleteInactive(profileId, inactiveScoresToKeep);
inOrder.verify(calculator).calculateScores(profile, persister);
inOrder.verify(versioner).calculationFinished(version);
inOrder.verify(versioner).activateVersion(profile, version);
inOrder.verify(persister).deleteInactive(profileId, inactiveScoresToKeep);
inOrder.verify(versioner).deleteInactive(profileId, inactiveScoresToKeep);
@ExtendWith(PactConsumerTestExt::class)
@PactTestFor(providerName = "inventory")
class InventoryRepositoryPactTest {
@Pact(consumer = "pricing")
fun `some items available pact`(builder: PactDslWithProvider): RequestResponsePact =
builder
.given("some items are available")
.uponReceiving("inventory check")
.method("POST")
.path("/inventory/check")
class PriceCartUseCaseTest {
val inventory = mockk<Inventory>()
val pricingEngine = mockk<PricingEngine>()
val useCase = PriceCartUseCase(inventory, pricingEngine)
val cartToPrice = Cart(listOf(CartItem("::sku1::", 2), CartItem("::sku2::", 1)))
val availableCart = Cart(listOf(CartItem("::sku1::", 1), CartItem("::sku2::", 0)))
val unavailableCart = Cart(listOf(CartItem("::sku1::", 0), CartItem("::sku2::", 0)))
val pricedCard = PricedCart(emptyList(), 0.toBigDecimal())
@mgryszko
mgryszko / PricingControllerPactTest.kt
Created March 19, 2021 16:11
PricingControllerPactTest
@ExtendWith(SpringExtension::class)
@Provider("pricing")
@PactBroker(host = "localhost", port = "9292")
@WebMvcTest
class PricingControllerPactTest {
@MockkBean
lateinit var useCase: PriceCartUseCase
@TestTemplate
@ExtendWith(PactVerificationSpringProvider::class)
{
"description": "get cart total",
"request": {
"method": "POST",
"path": "/cart/total",
"body": {
"items": [{
"quantity": 4,
"sku": "croissants"
}, {