Using the ant javac adapter

遇到ANT构建过程中调用ECLIPSE JDT时出现ClassNotFound错误?本文将指导您如何通过确保jar文件正确放置、检查类名书写和避免空格来解决这一问题。
Class not found: org.eclipse.jdt.core.JDTCompilerAdapter

ant调用eclipse的jdt出现这个错误,检查类名书写是否正确,是否有空格。 <property name="build.compiler" value="org.eclipse.jdt.core.JDTCompilerAdapter"/>

关键是把jdtCompilerAdapter.jar和org.eclipse.jdt.core_3.x.x.jar 复制到 ant/lib目录下。
apply plugin: 'jacoco' //gradlew clean & gradlew createOfflineTestCoverageReport & gradlew jacocoTestReport tasks.withType(Test) { jacoco.includeNoLocationClasses = true jacoco.excludes = ['jdk.internal.*'] } configurations { jacocoAnt jacocoRuntime } jacoco { toolVersion = "0.8.5" } def offline_instrumented_outputDir = "$buildDir.path/intermediates/classes-instrumented/debugCoverage" tasks.withType(Test) { jacoco.includeNoLocationClasses = true jacoco.excludes = ['jdk.internal.*'] } def coverageSourceDirs = [ 'src/main/java' ] task jacocoTestReport(type: JacocoReport, dependsOn: "test") { group = "Reporting" description = "Generate Jacoco coverage reports" classDirectories.from = fileTree( dir: './build/intermediates/javac/debugCoverage/classes/', excludes: [ '**/R.class', '**/R$*.class', '**/Manifest*.*', '**/*Test*.*', 'android/**/*.*', '**/BuildConfig.*', '**/**Bean.class', '**/inputmethod/*', '**/config/*.*', '**/flex/*.*', '**/AssetsUtils.class', '**/OkHttpHelper.java', '**/StableFocusRecyclerView.*', '**/databinding/**', '**/adaptor/**', '**/base/utils/**', '**/ui/activity/**', '**/ui/dialog/**', '**/ui/fragment/**', '**/ui/widget/**', ] ) getSourceDirectories().setFrom(files(coverageSourceDirs)) getExecutionData().setFrom(files('./build/jacoco/testDebugUnitTest.exec')) } jacocoTestReport { reports { xml.enabled true html.enabled true html.destination file("build/test-results/jacocoHtml") } } /* This task is used to create offline instrumentation of classes for on-the-fly instrumentation coverage tool like Jacoco. See jacoco classId * and Offline Instrumentation from the jacoco site for more info. * * In this case, some classes mocked using PowerMock were reported as 0% coverage on jacoco & Sonarqube. The issue between PowerMock and jacoco * is well documented, and a possible solution is offline Instrumentation (not so well documented for gradle). * * In a nutshell, this task: * - Pre-instruments the original *.class files * - Puts the instrumented classes path at the beginning of the task's classpath (for report purposes) * - Runs test & generates a new exec file based on the pre-instrumented classes -- as opposed to on-the-fly instrumented class files generated by jacoco. * * It is currently not implemented to run prior to any other existing tasks (like test, jacocoTestReport, etc...), therefore, it should be called * explicitly if Offline Instrumentation report is needed. * * Usage: gradle clean & gradle createOfflineInstrTestCoverageReport & gradle jacocoTestReport * - gradle clean //To prevent influence from any previous task execution * - gradle createOfflineInstrTestCoverageReport //To generate *.exec file from offline instrumented class * - gradle jacocoTestReport //To generate html report from newly created *.exec task */ task createOfflineTestCoverageReport(dependsOn: ['instrument', 'testDebugCoverageUnitTest']) { group = "reporting" doLast { ant.taskdef(name: 'report', classname: 'org.jacoco.ant.ReportTask', classpath: configur3+ations.jacocoAnt.asPath) ant.report() { executiondata { ant.file(file: "$buildDir.path/jacoco/testDebugCoverageUnitTest.exec") } structure(name: 'Example') { classfiles { fileset(dir: "$project.buildDir/intermediates/javac/debugCoverage") } sourcefiles { fileset(dir: 'src/main/java') } } //Uncomment if we want the task to generate jacoco html reports. However, the current script does not exclude files. //An alternative is to used jacocoTestReport after this task finishes //html(destdir: "$buildDir.path/reports/jacocoHtml") } } } createOfflineTestCoverageReport.dependsOn(clean) /* * Part of the Offline Instrumentation process is to add the jacoco runtime to the class path along with the path of the instrumented files. */ gradle.taskGraph.whenReady { graph -> if (graph.hasTask(instrument)) { tasks.withType(Test) { doFirst { systemProperty 'jacoco-agent.destfile', buildDir.path + '/jacoco/testDebugCoverageUnitTest.exec' classpath = files(offline_instrumented_outputDir) + classpath + configurations.jacocoRuntime } } } } /* * Instruments the classes per se */ task instrument(dependsOn:'compileDebugUnitTestSources') { doLast { println 'Instrumenting classes' ant.taskdef(name: 'instrument', classname: 'org.jacoco.ant.InstrumentTask', classpath: configurations.jacocoAnt.asPath) ant.instrument(destdir: offline_instrumented_outputDir) { fileset(dir: "$buildDir.path/intermediates/javac/debugCoverage") } } } ========================================== package com.st.systemsettings.presenter; import android.content.Context; import android.net.NetworkInfo; import com.st.systemsettings.R; import com.st.systemsettings.adapter.SystemAdapter; import com.st.systemsettings.bean.SystemBean; import com.st.systemsettings.bean.SystemSoftwareInfo; import com.st.systemsettings.contract.SystemContract; import com.st.systemsettings.manager.OtaManager; import com.st.systemsettings.manager.SystemManager; import com.st.systemsettings.manager.WifiNetManager; import com.st.systemsettings.utils.UserModeUtils; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.Mockito; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; import static org.junit.Assert.*; import static org.mockito.Mockito.*; @RunWith(PowerMockRunner.class) @PrepareForTest({SystemManager.class, OtaManager.class, WifiNetManager.class, UserModeUtils.class}) public class SystemPresenterImplTest { private SystemPresenterImpl systemPresenter; @Mock private SystemContract.View mockView; @Mock private Context mockContext; @Before public void setUp() throws Exception { mockView = mock(SystemContract.View.class); mockContext = mock(Context.class); when(mockView.getContext()).thenReturn(mockContext); // 模拟单例对象 SystemManager mockSystemManager = mock(SystemManager.class); OtaManager mockOtaManager = mock(OtaManager.class); WifiNetManager mockWifiNetManager = mock(WifiNetManager.class); UserModeUtils mockUserModeUtils = mock(UserModeUtils.class); // mock 单例的 getInstance() 方法 PowerMockito.mockStatic(SystemManager.class); PowerMockito.when(SystemManager.getInstance()).thenReturn(mockSystemManager); PowerMockito.mockStatic(OtaManager.class); PowerMockito.when(OtaManager.getInstance()).thenReturn(mockOtaManager); PowerMockito.mockStatic(WifiNetManager.class); PowerMockito.when(WifiNetManager.getInstance()).thenReturn(mockWifiNetManager); PowerMockito.mockStatic(UserModeUtils.class); PowerMockito.when(UserModeUtils.getInstance()).thenReturn(mockUserModeUtils); when(mockUserModeUtils.isChildMode()).thenReturn(false); // 初始化被测试类 systemPresenter = new SystemPresenterImpl(); systemPresenter.registerView(mockView); systemPresenter.init(); } @Test public void testGetSystemList_UsingReflection() throws Exception { Method method = SystemPresenterImpl.class.getDeclaredMethod("getSystemList"); method.setAccessible(true); // 允许访问私有方法 List<SystemBean> systemList = (List<SystemBean>) method.invoke(systemPresenter); assertNotNull(systemList); assertEquals(6, systemList.size()); assertEquals(R.string.home_item_system, systemList.get(0).getLabel()); assertEquals(R.string.system_label_about_machine, systemList.get(1).getLabel()); assertEquals(R.string.system_label_online_update, systemList.get(2).getLabel()); assertEquals(R.string.system_label_local_upgrade, systemList.get(3).getLabel()); assertEquals(R.string.system_label_language_settings, systemList.get(4).getLabel()); assertEquals(R.string.system_label_reset_default, systemList.get(5).getLabel()); } @Test public void testInit_RegistersViewAndInitializesManagers() { verify(mockView, times(1)).getContext(); assertNotNull(systemPresenter); } } 上面分别是我的待测试的代码和单元测试代码,请继续帮我完成单元测试的代码
07-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值