初始化前的单元测试

时间:2014-12-24 13:34:19

标签: spring testing junit

我想用JUnit测试我的java代码。 我正在使用当前版本的Spring。 现在我遇到了问题,单位测试在初始化后运行,而我的DataInitializer的数据在Repositorys中。

@Component
public class PizzaRunnerInitializer implements DataInitializer {

//here are the attributes and the constructor

@Override
    public void initialize() {
        this.addressManager.setCompanyAddress(this.addressManager.getAddress("Marcs Liebingsgruppe", "22", "210815", "Dresden"));

        initializeCategories();
        initializeCatalogs();
    }


private void initializeCategories(){
        categoryRepository.save(new Category("Getraenk", false));
        categoryRepository.save(new Category("Zutat", false));
        categoryRepository.save(new Category("Pizza", true));
        categoryRepository.save(new Category("Salat", false));
    }


    private void initializeCatalogs(){

        if (ingredientCatalog.findAll().iterator().hasNext() || simpleProductCatalog.findAll().iterator().hasNext() || extendableProductCatalog.findAll().iterator().hasNext()) {
            return;
        }

        Ingredient i1 = productManager.createIngredient("Teig", Money.of(EUR, 6.0), true);
        Ingredient i2 = productManager.createIngredient("Edammer", Money.of(EUR, 1.0), false);
        Ingredient i3 = productManager.createIngredient("Tomate", Money.of(EUR, 1.5), false);
        Ingredient i4 = productManager.createIngredient("Salami", Money.of(EUR, 1.0), false);
        Ingredient i5 = productManager.createIngredient("Ananas", Money.of(EUR, 2.0), false);
        Ingredient i6 = productManager.createIngredient("Feta", Money.of(EUR, 1.5), false);
        Ingredient i7 = productManager.createIngredient("Schinken", Money.of(EUR, 1.0), false);
        Ingredient i8 = productManager.createIngredient("Peperoni", Money.of(EUR, 2.0), false);
        Ingredient i9 = productManager.createIngredient("Gouda", Money.of(EUR, 1.0), false);
        Ingredient i10 = productManager.createIngredient("Zwiebel", Money.of(EUR, 1.0), false);
        Ingredient i11 = productManager.createIngredient("Tomatensauce", Money.of(EUR, 0.5), true);
        Ingredient i12 = productManager.createIngredient("Champignons", Money.of(EUR, 0.5), false);
        Ingredient i13 = productManager.createIngredient("Kopfsalat", Money.of(EUR, 1.0), false);
        Ingredient i14 = productManager.createIngredient("Rucola", Money.of(EUR, 0.5), false);
        Ingredient i15 = productManager.createIngredient("Schnittlauch", Money.of(EUR, 0.5), false);
        Ingredient i16 = productManager.createIngredient("DressingA", Money.of(EUR, 0.5), false);
        Ingredient i17 = productManager.createIngredient("DressingB", Money.of(EUR, 0.5), false);

        stockManager.registerInInventory(i1.getIdentifier(), 10);
        stockManager.registerInInventory(i2.getIdentifier(), 10);
        stockManager.registerInInventory(i3.getIdentifier(), 10);
        stockManager.registerInInventory(i4.getIdentifier(), 10);
        stockManager.registerInInventory(i5.getIdentifier(), 10);
        stockManager.registerInInventory(i6.getIdentifier(), 10);
        stockManager.registerInInventory(i7.getIdentifier(), 10);
        stockManager.registerInInventory(i8.getIdentifier(), 10);
        stockManager.registerInInventory(i9.getIdentifier(), 10);
        stockManager.registerInInventory(i10.getIdentifier(), 10);
        stockManager.registerInInventory(i11.getIdentifier(), 10);
        stockManager.registerInInventory(i12.getIdentifier(), 10);
        stockManager.registerInInventory(i13.getIdentifier(), 10);
        stockManager.registerInInventory(i14.getIdentifier(), 10);
        stockManager.registerInInventory(i15.getIdentifier(), 10);
        stockManager.registerInInventory(i16.getIdentifier(), 10);
        stockManager.registerInInventory(i17.getIdentifier(), 10);


        SimpleProduct s1 = productManager.createSimpleProduct("CocaCola", Money.of(EUR, 2.5));
        SimpleProduct s2 = productManager.createSimpleProduct("PepsiCola", Money.of(EUR, 2.5));
        SimpleProduct s3 = productManager.createSimpleProduct("Fanta", Money.of(EUR, 2.5));
        SimpleProduct s4 = productManager.createSimpleProduct("Sprite", Money.of(EUR, 2.5));
        SimpleProduct s5 = productManager.createSimpleProduct("Wasser", Money.of(EUR, 2.5));

        stockManager.registerInInventory(s1.getIdentifier(), 10);
        stockManager.registerInInventory(s2.getIdentifier(), 10);
        stockManager.registerInInventory(s3.getIdentifier(), 10);
        stockManager.registerInInventory(s4.getIdentifier(), 10);
        stockManager.registerInInventory(s5.getIdentifier(), 10);


        List<Ingredient> list = new LinkedList<Ingredient>();
        list.add(i1);
        list.add(i11);
        list.add(i2);
        ExtendableProduct e1 = productManager.createExtendableProduct("Magherita", "Pizza", Money.of(EUR, 8.0), list);
        List<Ingredient> list1 = new LinkedList<Ingredient>();
        list1.add(i1);
        list1.add(i2);
        list1.add(i5);
        list1.add(i7);
        list1.add(i11);
        ExtendableProduct e2 = productManager.createExtendableProduct("Hawaii", "Pizza", Money.of(EUR, 10.0), list1);
        List<Ingredient> list2 = new LinkedList<Ingredient>();
        list2.add(i1);
        list2.add(i2);
        list2.add(i11);
        list2.add(i4);
        list2.add(i12);
        ExtendableProduct e3 = productManager.createExtendableProduct("Salami", "Pizza", Money.of(EUR, 10.0), list2);
        List<Ingredient> list3 = new LinkedList<Ingredient>();
        list3.add(i1);
        list3.add(i2);
        list3.add(i11);
        list3.add(i4);
        list3.add(i12);
        list3.add(i8);
        list3.add(i10);
        ExtendableProduct e4 = productManager.createExtendableProduct("Diavolo", "Pizza", Money.of(EUR, 11.5), list3);
        List<Ingredient> list4 = new LinkedList<Ingredient>();
        list4.add(i13);
        list4.add(i3);
        list4.add(i10);
        list4.add(i16);
        ExtendableProduct e5 = productManager.createExtendableProduct("SalatA", "Salat", Money.of(EUR, 4.0), list4);
        List<Ingredient> list5 = new LinkedList<Ingredient>();
        list5.add(i13);
        list5.add(i3);
        list5.add(i10);
        list5.add(i17);
        list5.add(i14);
        list5.add(i15);
        ExtendableProduct e6 = productManager.createExtendableProduct("SalatB", "Salat", Money.of(EUR, 4.0), list5);

        stockManager.registerInInventory(e1.getIdentifier(), 0);
        stockManager.registerInInventory(e2.getIdentifier(), 0);
        stockManager.registerInInventory(e3.getIdentifier(), 0);
        stockManager.registerInInventory(e4.getIdentifier(), 0);
        stockManager.registerInInventory(e5.getIdentifier(), 0);
        stockManager.registerInInventory(e6.getIdentifier(), 0);

    }

例如我想测试ProductManager的这个方法:

public List<Product> getByCategory(Category category){
    List<Product> list = new LinkedList<Product>();
    for(Product product: ingredientCatalog.findAll()){
        for(String productCategory: product.getCategories()){
            if(productCategory.equalsIgnoreCase(category.getName())) list.add(product);
        }
    }
    for(Product product: simpleProductCatalog.findAll()){
        for(String productCategory: product.getCategories()){
            if(productCategory.equalsIgnoreCase(category.getName())) list.add(product);
        }
    }
    for(Product product: extendableProductCatalog.findAll()){
        for(String productCategory: product.getCategories()){
            if(productCategory.equalsIgnoreCase(category.getName())) list.add(product);
        }
    }
    return list;
}

但如果我像这样测试它不是一个空列表。在列表中是我初始化的元素。

public class ProductManagerTest extends AbstractIntegrationTests{

@Autowired
ProductManager productManager;

@Autowired
CategoryRepository categoryRepository;


@Test
@Transactional
public void getByCategoryTest(){

    for(Category category: categoryRepository.findAll()){
        assertThat(productManager.getByCategory(category), is(equalTo(new LinkedList<Product>())));
    }
}
}
 @RunWith(SpringJUnit4ClassRunner.class)
 @SpringApplicationConfiguration(classes = Application.class)
 public abstract class AbstractIntegrationTests {


 }

我的问题是有没有办法在初始化之前运行测试,还是我必须关闭初始化程序?

顺便说一句: “圣诞快乐”

PS:该方法正在运行,但大学要求我测试我的所有代码

0 个答案:

没有答案
相关问题