Created
July 14, 2021 10:53
-
-
Save Deviad/94d3a95172701940b2418d614c24f9a6 to your computer and use it in GitHub Desktop.
Example of a hand made mock in micronaut
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.projectcheckins | |
import com.projectcheckins.core.CheckinsRepository | |
import com.projectcheckins.core.UserCheckins | |
import io.micronaut.context.annotation.Replaces | |
import io.micronaut.context.annotation.Requires | |
import io.micronaut.core.annotation.NonNull | |
import io.micronaut.http.HttpRequest | |
import io.micronaut.http.HttpResponse | |
import io.micronaut.http.HttpStatus | |
import javax.inject.Singleton | |
import javax.validation.constraints.NotNull | |
import java.security.Principal | |
import java.time.LocalDate | |
class CheckinsControllerSpec extends EmbeddedServerSpecification { | |
@Override | |
String getSpecName() { | |
'CheckinsControllerSpec' | |
} | |
void "when a checkin is submitted a 201 is returned"() { | |
given: | |
HttpRequest<?> request = HttpRequest.POST('/checkins', new Checkin("I've been teaching Micronaut AWS Training")) | |
when: | |
HttpResponse<?> response = client.exchange(request) | |
then: | |
noExceptionThrown() | |
response.status == HttpStatus.CREATED | |
and: | |
embeddedServer.applicationContext.getBean(MockCheckinsRepository).invocations == 1 | |
cleanup: | |
embeddedServer.applicationContext.getBean(MockCheckinsRepository).invocations = 0 | |
} | |
@Requires(property = 'spec.name', value = 'CheckinsControllerSpec') | |
@Replaces(CheckinsRepository) | |
@Singleton | |
static class MockCheckinsRepository implements CheckinsRepository { | |
int invocations = 0 | |
@Override | |
void save(@NotNull @NonNull Principal principal, @NonNull @NotNull String message) { | |
invocations++ | |
} | |
@Override | |
Optional<List<UserCheckins>> findAtDate(LocalDate date) { | |
Optional.empty() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment