Skip to content

Instantly share code, notes, and snippets.

def product = mockProduct("sky", "name", "BranName", 120d, 90d, ["http://theiconic.com.au/someImage.jpg","http://theiconic.com.au/someOtherImage.jpg"])
CatalogProductEntity mockProduct(String sku, String name, String brand, Double price, Double specialPrice, List<String> imageUrls){
return Mock(CatalogProductEntity) {
it.getSku() >> sku
it.getName() >> name
it.getBrand() >> Mock(BrandEntity){
it.getName() >> brand
}
it.getPrice() >> price
it.getSpecialPrice() >> specialPrice
it.getImages() >> imageUrls.collect{ url->
private CatalogProductEntity mockProduct(String sku, String name, String brandName, Double price, Double specialPrice, List<String> imageUrls){
BrandEntity brand = mock(BrandEntity.class);
when(brand.getName()).thenReturn(brandName);
ArrayList<ImageEntity> images = new ArrayList<>();
for(String imageUrl : imageUrls){
ImageEntity imageEntity = mock(ImageEntity.class);
when(imageEntity.getUrl()).thenReturn(imageUrl);
package com.theiconic.spockexamples.bag.domain.usecases;
import com.theiconic.spockexamples.bag.domain.repositories.BagRepository;
import com.theiconic.spockexamples.bag.domain.usecases.RemoveProductFromBagUseCase;
import com.theiconic.spockexamples.common.entities.BrandEntity;
import com.theiconic.spockexamples.common.entities.ImageEntity;
import com.theiconic.spockexamples.shop.entities.CatalogProductEntity;
import com.theiconic.spockexamples.shop.entities.CategoryEntity;
import org.junit.Before;
1 * useCase.repository.removeItem(simpleSku) >> Observable.just(out);
package com.theiconic.spockexamples.bag.domain.usecases;
import com.theiconic.spockexamples.common.domain.usecases.ObservableUseCase;
import com.theiconic.spockexamples.bag.domain.repositories.BagRepository;
import javax.inject.Inject;
import rx.Observable;
public class RemoveProductFromBagUseCase implements ObservableUseCase<String, Boolean> {
package com.theiconic.spockexamples.bag.domain.usecases
import com.theiconic.spockexamples.bag.domain.repositories.BagRepository
import com.theiconic.spockexamples.bag.domain.usecases.RemoveProductFromBagUseCase
import rx.Observable
import rx.observers.TestSubscriber
import spock.lang.Specification
class RemoveProductFromBagUseCaseSpec extends Specification{
protected RemoveProductFromBagUseCase useCase;
package com.theiconic.spockexamples.common.domain.usecases
import com.theiconic.spockexamples.common.domain.usecases.ValidateEmailUseCase
import spock.lang.Specification
import spock.lang.Unroll
class ValidEmailUseCaseSpec extends Specification {
ValidateEmailUseCase useCase;
@ian-ellis
ian-ellis / spock-examples-validate-email-usecase.java
Last active January 16, 2017 22:33
Validate Email Use Case
package com.theiconic.spockexamples.common.domain.usecases;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.inject.Inject;
public class ValidateEmailUseCase implements UseCase<String,Boolean> {
private static final Pattern VALID_EMAIL_ADDRESS_REGEX =
Pattern.compile("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,6}$", Pattern.CASE_INSENSITIVE);
@ian-ellis
ian-ellis / spock-examples-addition-spec.groovy
Created January 16, 2017 22:28
Simple Addition Spock Specification to demonstrate syntax
package com.theiconic.spockexamples
import spock.lang.Specification
class SimpleAdditionSpec extends Specification {
def 'addition'() {
given: 'two integers'
def a = 1
def b = 2