我有一些JUnit测试用例,当我将它们单独作为JUnit运行时,它们可以工作,但它们不会在Maven测试目标中运行。它找不到任何支持类,甚至没有找到org / junit / Assert,它显然位于我的pom.xml中,带有scope = test。那是为什么?
这是我的测试用例声明和导入:
import static org.junit.Assert.assertEquals;
import java.util.Calendar;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.test.JobLauncherTestUtils;
import org.springframework.test.jdbc.JdbcTestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/load-BMS-data-job-launcher-context.xml" })
public class SimpleJobStepFunctionalTest {
以下是我得到的错误:
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.ibm.cio.cloud.cost.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.003 sec
Running com.ibm.cio.cloud.cost.SimpleJobStepFunctionalTest
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.001 sec <<< FAILURE!
com.ibm.cio.cloud.cost.SimpleJobStepFunctionalTest.testJobLaunching() Time elapsed: 0.001 sec <<< FAILURE!
java.lang.NullPointerException
at com.ibm.cio.cloud.cost.SimpleJobStepFunctionalTest.testJobLaunching(SimpleJobStepFunctionalTest.java:79)
Running com.ibm.cio.cloud.cost.unit.BlueReportItemProcessorUnitTest
Tests run: 3, Failures: 3, Errors: 0, Skipped: 0, Time elapsed: 0.003 sec <<< FAILURE!
com.ibm.cio.cloud.cost.unit.BlueReportItemProcessorUnitTest.testCountryExclusion821() Time elapsed: 0 sec <<< FAILURE!
java.lang.NoClassDefFoundError: org/junit/Assert
at com.ibm.cio.cloud.cost.unit.BlueReportItemProcessorUnitTest.testCountryExclusion821(BlueReportItemProcessorUnitTest.java:137)
Caused by: java.lang.ClassNotFoundException: org.junit.Assert
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 19 more
com.ibm.cio.cloud.cost.unit.BlueReportItemProcessorUnitTest.testSkippingBR() Time elapsed: 0 sec <<< FAILURE!
java.lang.NoClassDefFoundError: org/junit/Assert
at com.ibm.cio.cloud.cost.unit.BlueReportItemProcessorUnitTest.testSkippingBR(BlueReportItemProcessorUnitTest.java:165)
com.ibm.cio.cloud.cost.unit.BlueReportItemProcessorUnitTest.testSkippingIN() Time elapsed: 0 sec <<< FAILURE!
java.lang.NoClassDefFoundError: org/junit/Assert
at com.ibm.cio.cloud.cost.unit.BlueReportItemProcessorUnitTest.testSkippingIN(BlueReportItemProcessorUnitTest.java:193)
Results :
Failed tests: com.ibm.cio.cloud.cost.SimpleJobStepFunctionalTest.testJobLaunching()
com.ibm.cio.cloud.cost.unit.BlueReportItemProcessorUnitTest.testCountryExclusion821(): org/junit/Assert
com.ibm.cio.cloud.cost.unit.BlueReportItemProcessorUnitTest.testSkippingBR(): org/junit/Assert
com.ibm.cio.cloud.cost.unit.BlueReportItemProcessorUnitTest.testSkippingIN(): org/junit/Assert
Tests run: 5, Failures: 4, Errors: 0, Skipped: 0
这是我的pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ibm.cio.cloud.cost</groupId>
<artifactId>bluecost</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>bluecost</name>
<url>http://maven.apache.org</url>
<properties>
<jdk.version>1.8</jdk.version>
<spring.version>4.3.9.RELEASE</spring.version>
<spring.batch.version>3.0.8.RELEASE</spring.batch.version>
<mysql.driver.version>5.1.44</mysql.driver.version>
<db2.driver.version>10.1.0</db2.driver.version>
</properties>
<dependencies>
<!-- Spring Core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- Spring Core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- Spring Test -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
<!-- Spring Batch Test -->
<!-- https://mvnrepository.com/artifact/org.springframework.batch/spring-batch-test -->
<dependency>
<groupId>org.springframework.batch</groupId>
<artifactId>spring-batch-test</artifactId>
<version>2.2.0.RELEASE</version>
<scope>test</scope>
</dependency>
<!-- Spring Batch dependencies -->
<dependency>
<groupId>org.springframework.batch</groupId>
<artifactId>spring-batch-core</artifactId>
<version>${spring.batch.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.batch</groupId>
<artifactId>spring-batch-infrastructure</artifactId>
<version>${spring.batch.version}</version>
</dependency>
<!-- MySQL database driver -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.driver.version}</version>
</dependency>
<dependency>
<groupId>com.ibm.db2.jcc</groupId>
<artifactId>db2jcc_license_cisuz</artifactId>
<version>${db2.driver.version}</version>
</dependency>
<dependency>
<groupId>com.ibm.db2.jcc</groupId>
<artifactId>db2jcc</artifactId>
<version>${db2.driver.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>2.4.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-pool</groupId>
<artifactId>commons-pool</artifactId>
<version>1.6</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>bluecost-batch</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.9</version>
<configuration>
<downloadSources>true</downloadSources>
<downloadJavadocs>false</downloadJavadocs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
答案 0 :(得分:0)
来自this answer:
你的pom列出了JUnit 3.81但是那个版本的JUnit没有
org.junit.Assert
。
因此,当您单独运行测试时,仔细检查实际使用的是哪个版本的JUnit(4?,5?)。
OP JamesD确认在Eclipse项目类路径中按正确顺序设置JUnit4时的进度。
我有三个测试用例中的两个无故障运行。
最后一个因db初始化错误而失败:嵌套异常
java.sql.SQLSyntaxErrorException:
object name already exists:
ACCOUNT_MAP in statement [create table ACCOUNT_MAP]
例如,spring-projects/spring-boot issue 1712中显示了这一点,并在this commit和this one中修复了