在春季测试中的Bean接线期间不获取软件包

时间:2018-09-22 10:32:38

标签: java spring junit

我正在运行一个带有测试的简单spring应用程序,但出现以下错误,

[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] /home/prateek/Workspace/Java/spring_in_action/Chapter 2 : Wiring beans/src/test/java/com/prateek/spring/test/CDPlayerTest.java:[11,38] package com.prateek.spring.soundsystem does not exist
[ERROR] /home/prateek/Workspace/Java/spring_in_action/Chapter 2 : Wiring beans/src/test/java/com/prateek/spring/test/CDPlayerTest.java:[12,38] package com.prateek.spring.soundsystem does not exist
[ERROR] /home/prateek/Workspace/Java/spring_in_action/Chapter 2 : Wiring beans/src/test/java/com/prateek/spring/test/CDPlayerTest.java:[13,38] package com.prateek.spring.soundsystem does not exist
[ERROR] /home/prateek/Workspace/Java/spring_in_action/Chapter 2 : Wiring beans/src/test/java/com/prateek/spring/test/CDPlayerTest.java:[21,17] cannot find symbol
  symbol:   class CompactDisc
  location: class com.prateek.spring.test.CDPlayerTest
[ERROR] /home/prateek/Workspace/Java/spring_in_action/Chapter 2 : Wiring beans/src/test/java/com/prateek/spring/test/CDPlayerTest.java:[17,34] cannot find symbol
  symbol: class CDPlayerConfig
[INFO] 5 errors 
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------

下面是我的java类和pox.xml

1.CDPlayerTest.java

package com.prateek.spring.test;

import static org.junit.Assert.assertNotNull;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.prateek.spring.soundsystem.CDPlayerConfig;
import com.prateek.spring.soundsystem.CompactDisc;


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {CDPlayerConfig.class})
public class CDPlayerTest {

    @Autowired
    private CompactDisc cd;

    @Test
    public void cdSHouldNotBeNull() {
        System.out.println("Testing..................");
        assertNotNull(cd);
    }
}

2.CDPlayerConfig.java

package com.prateek.spring.soundsystem;

    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;

    @Configuration
    @ComponentScan (basePackages= {"com.prateek.spring.soundsystem","com.prateek.spring.test"})
    public class CDPlayerConfig {
        public CDPlayerConfig() {
        }
    }

3.Song.java

package com.prateek.spring.soundsystem;

import org.springframework.stereotype.Component;

@Component
public class Song implements CompactDisc{
    private String title = "A random song title";
    private String artist = "Prateek Joshi";

    public Song() {
        // TODO Auto-generated constructor stub
    }

    public void play() {
        System.out.println("Playing "+title+" by "+ artist);
    }

}

4.CompactDisc.java

    package com.prateek.spring.soundsystem;

    public interface CompactDisc {
        void play();
    }


5. 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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.prateek.spring</groupId>
    <artifactId>component-scan</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>5.0.9.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.8.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>5.0.9.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context-support -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>5.1.0.RELEASE</version>
        </dependency>
        <!-- Spring test -->
        <!-- https://mvnrepository.com/artifact/junit/junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>4.3.18.RELEASE</version>
        </dependency>


    </dependencies>

</project>

1 个答案:

答案 0 :(得分:1)

我已经尝试对您的pom.xml进行示例测试:

我已将其复制粘贴到示例项目中,并且测试已通过(我使用的是非常简单的示例测试,而不是spring的东西):

package com.prateek.spring;

import org.junit.Assert;
import org.junit.Test;


public class SampleTest {

    @Test
    public void test() {
        System.out.println("testing");
        Assert.assertTrue( 1 + 1 == 2);
    }
}

所以问题可能出在Maven / sufrefire插件版本或Java交互上,而不是pom。

org.apache.maven.surefire.booter.ForkedBooter是在任何测试运行之前加载的方式(它是surefire插件的一部分)。

在分辨率方面,我建议您检查Java版本(Spring 5至少需要Java 8,BTW)和maven,甚至可以升级surefire插件。

由于您未提供任何特定版本,因此很难说出更具体的内容。