samedi 26 mars 2016

Error creating bean with name 'exceptionTranslation' when using @RunWith(PowerMockRunner.class). Maven

I am having a problem with JUnit test using Powermock. I want to mock final class object looking like this - created via builder:

public final class Request implements Serializable {
private static final long serialVersionUID = 1L;

public static ReferenceStep builder() {
    return new Builder();
}

I need to test the method that uses this object (Request) but don't need to test the Request itself. So I wrote a test:

import junit.framework.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.Rule;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
import org.mockito.Mock;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class) // <-this leads to error
@PrepareForTest( { Request.class })
public class Test1 {
private static final String acc1 = "acc1";
private static final String acc2 = "acc2";
private aService aService;
private tService tService;

@Before
public void setupTestConditionsBeforeEachTest() throws Exception {
    Factory Factory = (Factory) Class.forName(
            "example.FactoryImpl").newInstance();
    aService = bankFactory.getAccountService();
    tService = bankFactory.getTransferService();        
    Factory.setupInitialData();     
}
@Test

public void doTransfer() throws Exception {
    aService.createAccount(CASH_ACCOUNT_1,100);
    aService.createAccount(CASH_ACCOUNT_2,0);

            TransactionLeg leg1 = new TransactionLeg(CASH_ACCOUNT_1,MoneyUtils.toMoney("10.00", "EUR"));
            TransactionLeg leg2 = new TransactionLeg(CASH_ACCOUNT_1,MoneyUtils.toMoney("-10.00", "EUR"));
//used later for Request
            List<TransactionLeg> legs = new ArrayList<TransactionLeg>();
            legs.add(leg1);
            legs.add(leg2);
            Request tested = PowerMockito.mock(Request.class);
            PowerMockito.when(tested.getLegs()).thenReturn(legs);
            PowerMockito.when(tested.getTransactionRef()).thenReturn("First transaction");
            PowerMockito.when(tested.getTransactionType()).thenReturn("Type1");

            tService.transferFunds(tested);
}
}
}

So the method to be tested is transferFunds. I only need Request class to pass as parameter to this function. Ok I run this test and get an error:

Tests in error:
doTransfer(...Test1): Error creating bean with name 'exceptionTranslation'                             defined in class path resource [.../config/PersistenceJPAConfig.class]:            Initialization of bean failed; nested exception is     org.springframework.beans.factory.BeanCrea
tionException: Error creating bean with name 'entityManagerFactoryBean'     defined in class path resource [.../config/PersistenceJPAConfig.class]:     Invocation of init method failed; nested exception is     javax.persistence.PersistenceException: [PersistenceUnit: banktest_pu] Unable to
build EntityManagerFactory

Using Maven. pom looks like this:

    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-core</artifactId>
        <version>1.9.5</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.9</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-api-mockito</artifactId>
        <version>1.5</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-core</artifactId>
        <version>1.5</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-module-junit4</artifactId>
        <version>1.5</version>
        <scope>test</scope>
    </dependency>

It seems that the problem is in Spring configuration. But I don't know where to dig in the problem. Alternatively, is there any way to avoid using mock? The class Transfer is made using builder() so I don't see how to put test values in it(and I also cannot change any part of the code)

Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire