Spring DAO 测试失败 - 说“需要 JUnit 4.12 或更高版本”

Posted

技术标签:

【中文标题】Spring DAO 测试失败 - 说“需要 JUnit 4.12 或更高版本”【英文标题】:Spring DAO Test Fails - says "requires JUnit 4.12 or higher" 【发布时间】:2017-10-08 11:05:31 【问题描述】:

我正在尝试使用 Spring 编写 DAO 测试。当我运行测试时,它会在下面的堆栈跟踪中出错。当我相信我包含正确版本的 JUnit 时,我不知道为什么会出现错误。

java.lang.ExceptionInInitializerError
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:31)
    at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:24)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
    at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:29)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
    at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:24)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createUnfilteredTest(JUnit4TestLoader.java:84)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:70)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:43)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:444)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.lang.IllegalStateException: SpringJUnit4ClassRunner requires JUnit 4.12 or higher.
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.<clinit>(SpringJUnit4ClassRunner.java:102)
    ... 17 more

我不明白的是,在我的 pom.xml 中,我包含了 JUnit 4.9(我检查了 JUnit 的已解析依赖项,它也指示 4.9):

<dependencies>
    <!-- Spring -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>$org.springframework-version</version>
        <exclusions>
            <!-- Exclude Commons Logging in favor of SLF4j -->
            <exclusion>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
             </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>$org.springframework-version</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>$org.springframework-version</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>$org.springframework-version</version>
        <scope>test</scope>
    </dependency>

    <!-- AspectJ -->
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>$org.aspectj-version</version>
    </dependency>   

    <!-- Logging -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>$org.slf4j-version</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>jcl-over-slf4j</artifactId>
        <version>$org.slf4j-version</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>$org.slf4j-version</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.15</version>
        <exclusions>
            <exclusion>
                <groupId>javax.mail</groupId>
                <artifactId>mail</artifactId>
            </exclusion>
            <exclusion>
                <groupId>javax.jms</groupId>
                <artifactId>jms</artifactId>
            </exclusion>
            <exclusion>
                <groupId>com.sun.jdmk</groupId>
                <artifactId>jmxtools</artifactId>
            </exclusion>
            <exclusion>
                <groupId>com.sun.jmx</groupId>
                <artifactId>jmxri</artifactId>
            </exclusion>
        </exclusions>
        <scope>runtime</scope>
    </dependency>

    <!-- @Inject -->
    <dependency>
        <groupId>javax.inject</groupId>
        <artifactId>javax.inject</artifactId>
        <version>1</version>
    </dependency>

    <!-- Servlet -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>jsp-api</artifactId>
        <version>2.1</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>

    <!-- Test -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.9</version>
        <scope>test</scope>
    </dependency>    
    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-core</artifactId>
        <version>1.9.5</version>
        <scope>test</scope>
    </dependency>

    <!-- Database -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.21</version>
    </dependency>

</dependencies>

以下是相关的 Java 文件:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = TestDatabaseConfig.class, PersonDao.class)
@Transactional
public class PersonDaoTest 

    @Autowired
    IPersonDao personDao;

    Person expectedPerson;

    @Before
    public void setUp() 

        expectedPerson = new PersonBuilder()
                .firstName("Elvis")
                .lastName("Presley")
                .build();
    

    @Test
    public void getAll() 

        personDao.insert(expectedPerson);

        List<Person> actualPersons = personDao.getAll();

        assertNotNull(actualPersons);
        assertEquals(1, actualPersons.size());
    

    @After
    public void tearDown() 

    

以及配置:

@Configuration
public class TestDatabaseConfig 

    @Bean
    public DataSource dataSource() 

        DriverManagerDataSource dataSource = new DriverManagerDataSource();

        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/springWeb_test");
        dataSource.setUsername("springweb");
        dataSource.setPassword("springweb");

        return dataSource;
    

    @Bean
    public JdbcTemplate jdbcTemplate() 

        JdbcTemplate jdbcTemplate = new JdbcTemplate();
        jdbcTemplate.setDataSource(dataSource());

        return jdbcTemplate;
    

【问题讨论】:

【参考方案1】:
<!-- Test -->
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.9</version>
    <scope>test</scope>
</dependency> 

您的 jUnit 版本是 4.9。您需要像这样将其更新到 4.12 或更高版本:

<!-- Test -->
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
</dependency> 

【讨论】:

我同意 @hishammuddin-sani 认为这可以解决问题,但消息指出我可以使用 4.12 或更高版本...4.9 应该可以工作! 4.12 高于 4.9,如 4.9、4.10、4.11、4.12 哦,好吧,你是对的。我认为4.9高于4.12。版本命名有点混乱。感谢您的帮助! 即使在更新我的 POM 文件以使用版本 4.12 后,我仍然收到此错误。有什么想法吗?【参考方案2】:

这是SpringJUnit4ClassRunner类的源代码:

public class SpringJUnit4ClassRunner extends BlockJUnit4ClassRunner 

private static final Log logger = LogFactory.getLog(SpringJUnit4ClassRunner.class);

private static final Method withRulesMethod;

static 
    if (!ClassUtils.isPresent("org.junit.internal.Throwables", SpringJUnit4ClassRunner.class.getClassLoader())) 
        throw new IllegalStateException("SpringJUnit4ClassRunner requires JUnit 4.12 or higher.");
    

    withRulesMethod = ReflectionUtils.findMethod(SpringJUnit4ClassRunner.class, "withRules",
            FrameworkMethod.class, Object.class, Statement.class);
    if (withRulesMethod == null) 
        throw new IllegalStateException("SpringJUnit4ClassRunner requires JUnit 4.12 or higher.");
    
    ReflectionUtils.makeAccessible(withRulesMethod);
...

所以只需升级junit版本

【讨论】:

以上是关于Spring DAO 测试失败 - 说“需要 JUnit 4.12 或更高版本”的主要内容,如果未能解决你的问题,请参考以下文章

spring mvc 注入dao失败。怎么办?

springboot 整合 mybatis dao一直自动注入失败

将ehcache添加到DAO类后Junit测试失败,无法在测试类中实例化DAO

IntelliJ IDEA Spring+Mybatis dao bean对象注入失败

如何使用 Spring Security 对 Spring 4 DAO 方法进行单元测试?

单元测试怎么注入service