Klasa testowa
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class RestaurantServiceAddRestaurantTest extends Specification {
private final static String DB_URL_PROPERTIES = "spring.datasource.url"
private final static String DB_URL = "jdbc:postgresql://localhost:%d/restaurantDb?currentSchema=restaurant"
@Shared
PgSqlContainer pgSqlContainer
static PgSqlContainer pgSqlContainerStatic
@Autowired
RestaurantEntry restaurantEntry
@Autowired
RestaurantRepository restaurantRepository
@DynamicPropertySource
static void postgresProperties(DynamicPropertyRegistry registry) {
registry.add(DB_URL_PROPERTIES, { ->
String.format(DB_URL, pgSqlContainerStatic.getPort())
})
}
def setupSpec() {
pgSqlContainer = new PgSqlContainer()
pgSqlContainer.startContainer()
pgSqlContainerStatic = pgSqlContainer
}
RestaurantDto prepareRestaurantDto(String name){
RestaurantDto restaurantDto = new RestaurantDto()
restaurantDto.setName(name)
return restaurantDto
}
def "is PostgresSql working properly"() {
expect:
pgSqlContainer.isRunning()
}
def "should add restaurant for user"() {
given:
def restaurantDto = prepareRestaurantDto("name")
when:
restaurantEntry.addRestaurant(restaurantDto)
then:
restaurantRepository.findAll().size() == 1
}
}
Chodzi mi o metodę "should add restaurant for user" przy dodawaniu restauracji pobieram Id z headera.
W serwisie (RestaurantService) który implementuje RestaurantEntry jest metoda i tutaj nie mogę sobie poradzić jak to zmokować żeby zwracała jakąś wartość aktualnie lecie exception.
UUID getUserIdFromRequest(){
return UUID.fromString(request.getHeader("id"));
}