Page 1 of 1

Problem with integration tests [SOLVED]

Posted: Mon Dec 03, 2012 9:53 am
by denis
Hi all,

We are trying to create some integration tests based on your test module (blc-integration) but we have a problem.

In effect, we would like to write a test for our custom customers (BusinessCustomer and ConsumerCustomer) but we have 2 custom entities that extends the blc customer (see this post viewtopic.php?f=13&t=761).

So we can't declare an entity override in the applicationContext-entity.

If we write a test (eg : for a ConsumerCustomer) :

Code: Select all

@Test(dependsOnGroups = "createCustomerIdGeneration", dataProvider = "setupMyCustomers", dataProviderClass = MyCustomerDataProvider.class)
   @Rollback(false)
   public void createConsumerCustomer(ConsumerCustomer customerInfo) {
         ConsumerCustomer customer = (ConsumerCustomer) customerService.createCustomerFromId(null);
         customer.setPassword(customerInfo.getPassword());
         customer.setUsername(customerInfo.getUsername());
         Long customerId = customer.getId();
         assertThat(customerId).isNotNull();
         
         customer = (ConsumerCustomer) customerService
               .saveCustomer(customer);
         assertThat(customerId).isEqualTo(customerId);
   }


There is an error :

Code: Select all

java.lang.ClassCastException: org.broadleafcommerce.profile.core.domain.CustomerImpl cannot be cast to my.company.core.profile.domain.ConsumerCustomer


Do you have an idea?

Thanks in advance

Re: Problem with integration tests

Posted: Mon Dec 03, 2012 11:03 am
by phillipuniverse
I assume that your ClassClastException comes from the first line of the createCustomer test. customerService.createCustomerFromId() creates a customer based on the bean id 'org.broadleafcommerce.profile.core.domain.Customer'. In your case, you don't have this overridden to anything, so it simply creates a BLC Customer and you get the class cast exception. You will have to subclass either CustomerService or CustomerDao to add your own method to create a consumer consumer or business customer as required.

Alternatively, you could just do 'new ConsumerCustomer();' and manually set the ID in this test (via customerService.findNextCustomerId()).

Re: Problem with integration tests

Posted: Mon Dec 03, 2012 12:06 pm
by denis
Hi Phillipuniverse,

Thanks for your answer.

Yeah the ClassCastException comes from the first line of the createCustomerTest.

We don't want subclass CustomerService or CustomerDao just for this test (it works on BLC admin without do that).

I will try by setting manually the ID, but i think that there will be a problem with the customerService.saveCustomer method then.

Thanks again.

Re: Problem with integration tests

Posted: Mon Dec 03, 2012 2:39 pm
by phillipuniverse
There won't be any problems with customerService.save() assuming that ConsumerCustomer extends CustomerImpl (which I'm pretty sure it does). All that customerService.save() does is set some customer properties (like if the customer is registered or w/e) and then forward to the dao, which calls entityManager.merge(customer). Just standard db stuff.