Junit5の環境でJunit4のテストソースを動かしたいのですが、PowerMockito.mockStatic()でClassNotPreparedExceptionになってしまいます。
環境はjdk-1.8.0、gradle6.3です。
以下はgradleになります。
gradle
1plugins { 2 id 'org.springframework.boot' version '2.2.6.RELEASE' 3 id 'io.spring.dependency-management' version '1.0.9.RELEASE' 4 id 'java' 5 id 'eclipse' 6 id "com.github.spotbugs" version "4.0.5" 7 id 'checkstyle' 8 id 'jacoco' 9} 10 11group = 'jp.co.ana.cas' 12version = '0.0.1-SNAPSHOT' 13sourceCompatibility = '1.8' 14 15configurations { 16 developmentOnly 17 runtimeClasspath { 18 extendsFrom developmentOnly 19 } 20 compileOnly { 21 extendsFrom annotationProcessor 22 } 23 // SLF4Jを使わない設定 24 all*.exclude module : 'spring-boot-starter-logging' 25} 26 27repositories { 28 mavenCentral() 29} 30 31dependencies { 32 implementation 'org.springframework.boot:spring-boot-starter-activemq' 33 implementation 'org.springframework.boot:spring-boot-starter-web' 34 implementation 'org.springframework.boot:spring-boot-starter-web-services' 35 compileOnly 'org.projectlombok:lombok' 36 runtimeOnly 'org.postgresql:postgresql' 37 developmentOnly 'org.springframework.boot:spring-boot-devtools' 38 annotationProcessor 'org.projectlombok:lombok' 39 40 // Log4J2を使うので追加 41 implementation 'org.springframework.boot:spring-boot-starter-log4j2' 42 43 //log4j 44 implementation 'log4j:log4j:latest.release' 45 // OpenAPI用 46 implementation 'io.springfox:springfox-swagger2:2.8.0' 47 implementation 'io.springfox:springfox-swagger-ui:2.8.0' 48 implementation 'org.openapitools:jackson-databind-nullable:0.1.0' 49 50 implementation 'org.apache.activemq:activemq-pool:5.9.0' 51 52 //追加 53 implementation 'commons-io:commons-io:2.6' 54 implementation 'org.codehaus.groovy:groovy-all:3.0.3' 55 implementation 'org.apache.ws.security:wss4j:1.6.19' 56 57 compile fileTree(dir: 'lib', include: '*.jar') 58 testImplementation "org.mockito:mockito-core:latest.release" 59 testImplementation "org.mockito:mockito-junit-jupiter" 60 testImplementation 'org.easymock:easymock:latest.release' 61 testImplementation 'org.powermock:powermock-api-easymock:latest.release' 62 //Junit 63 testImplementation( 64 'junit:junit:4.12', 65 'org.junit.jupiter:junit-jupiter-api:5.4.2', 66 'org.powermock:powermock-module-junit4:latest.release', 67 'org.powermock:powermock-api-mockito2:latest.release' 68 ) 69 testRuntime( 70 'org.junit.jupiter:junit-jupiter-engine:5.4.2', 71 'org.junit.vintage:junit-vintage-engine:5.4.2' 72 ) 73}
以下が対象のテストソースになります。
java
1@RunWith(PowerMockRunner.class) 2@PrepareForTest({DriverManager.class, ReportListDao.class}) 3public class ReportListDaoTest { 4 private String driverUrl = "jdbc://testtest/test"; 5 private String dbUser = "testUser"; 6 private String dbPasswd = "testPasswd"; 7 private String insertSql = "INSERT INTO report_list (file_name,file_data) values(?,?);"; 8 9 @Mock 10 Connection dbConn; 11 12 @InjectMocks 13 ReportListDao reportDao = new ReportListDao(); 14 15 @BeforeEach 16 public void initEachTest() { 17 MockitoAnnotations.initMocks(this); 18 } 19 20 @Test 21 public void testInsertReportToDB() { 22 PowerMockito.mockStatic(DriverManager.class); 23 PreparedStatement ps = mock(PreparedStatement.class); 24 25 try { 26 when(DriverManager.getConnection(anyString(), anyString(), anyString())).thenReturn(dbConn); 27 when(dbConn.prepareStatement(insertSql)).thenReturn(ps); 28 } catch (SQLException e) { 29 e.printStackTrace(); 30 } 31 32 ReportListDao reportDaoSpy = spy(reportDao); 33 34 Whitebox.setInternalState(reportDaoSpy, "driverUrl", driverUrl); 35 Whitebox.setInternalState(reportDaoSpy, "dbUser", dbUser); 36 Whitebox.setInternalState(reportDaoSpy, "dbPasswd", dbPasswd); 37 Whitebox.setInternalState(reportDaoSpy, "insertSql", insertSql); 38 39 ReportListDto reportDto = new ReportListDto(); 40 reportDto.setReportName("report_123456.pdf"); 41 reportDto.setReportData("falkfgajtgafjapoit="); 42 43 reportDaoSpy.insertReportToDB(reportDto); 44 } 45}
他に必要なものがあれば言ってください。
よろしくお願いします。
あなたの回答
tips
プレビュー