我在使用Powermock进行JUnit测试时遇到问题。我想模拟看起来像这样的最终类对象 - 通过builder:
创建public final class Request implements Serializable {
private static final long serialVersionUID = 1L;
public static ReferenceStep builder() {
return new Builder();
}
我需要测试使用此对象的方法(Request),但不需要测试Request本身。所以我写了一个测试:
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);
}
}
}
因此要测试的方法是transferFunds。我只需要Request类作为参数传递给这个函数。好的,我运行此测试并收到错误:
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
使用Maven。 pom看起来像这样:
<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>
似乎问题出在Spring配置中。但我不知道在哪里挖掘问题。或者,有没有办法避免使用模拟?使用builder()进行Transfer类,所以我看不到如何将测试值放入其中(我也无法更改代码的任何部分)
提前致谢!