Eclipse Junit:java.lang.ClassNotFoundException

时间:2018-07-23 12:40:08

标签: java eclipse unit-testing gradle junit

我有以下问题:

我有一个使用gradle的Java项目。项目结构为:

src
- main
-- java
-- resources
- test
-- java
-- resources

我有以下测试课程:

package it.main.tests;

import static org.junit.Assert.assertEquals;

import org.junit.Test;

public class MainTest {

    @Test
    public void mainTests() {
        String s = "ciao";

        assertEquals(s, "ciao");
    }
}

如果我运行以下命令:渐变测试,一切正常。如果我尝试从Eclipse中运行 mainTests 方法(右键单击该方法,“运行方式”,JUnit测试),则将出现以下异常:

Class not found it.main.tests.MainTest
java.lang.ClassNotFoundException: it.main.tests.MainTest
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.loadClass(RemoteTestRunner.java:770)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.loadClasses(RemoteTestRunner.java:499)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:522)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)

这是 build.gradle 文件:

apply plugin: 'java'
apply plugin: 'war'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
    jcenter()
}

compileJava {
    options.fork = true
}

dependencies {
    // Add all the jar dependencies from the lib folder. 
    compile fileTree(dir: 'src/main/webapp/WEB-INF/lib', include: ['*.jar'])
    compile fileTree(dir: '//opt/jenkins/wcs_artifacts/WCS_trunk', include: ['beanstyler.jar', 'UnycoIntegrationBean.jar', 'UnycoModels.jar'])
    compile group: 'javax.servlet', name: 'javax.servlet-api', version: '4.0.1'
    compile group: 'mysql', name: 'mysql-connector-java', version: '8.0.11'
    testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: "5.2.0"
    testCompile group: 'org.junit.platform', name: 'junit-platform-launcher', version: "1.2.0"
}

task createWCSAdminWar(type: War, dependsOn: [test, classes]) {
    baseName = 'WCSAdmin'
    destinationDir = file("$buildDir/dist")
}

task createUnycoAdminWar(type: War, dependsOn: [test, classes]) {
    baseName = 'UnycoAdmin'
    destinationDir = file("$buildDir/dist")
}

task createDists(dependsOn: [createWCSAdminWar, createUnycoAdminWar])

defaultTasks 'build'

这是 .classpath 文件

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry combineaccessrules="false" exported="true" kind="src" path="/UnycoIntegrationMessages"/>
    <classpathentry combineaccessrules="false" exported="true" kind="src" path="/UnycoModels"/>
    <classpathentry kind="src" path="src/main/java"/>
    <classpathentry kind="src" path="src/test/java"/>
    <classpathentry kind="src" path="src/main/resources"/>
    <classpathentry kind="src" path="src/test/resources"/>
    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8/"/>
    <classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.web.container"/>
    <classpathentry kind="con" path="org.eclipse.buildship.core.gradleclasspathcontainer">
        <attributes>
            <attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="output" path="bin"/>
</classpath>

webapp / WEB-INF / lib / 内部,我有 junit-4.12.jar 文件

有人可以帮助我吗?谢谢

0 个答案:

没有答案