简体   繁体   中英

Getting package does not exists during wiring of beans in Spring test

I am running a simple spring application with a test but I am getting the below error,

[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] ------------------------------------------------------------------------

Below are my java classes and 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>

I've tried to run a sample test against your pom.xml:

I've copy-pasted it into a sample project, and the test has passed (I've used a very simple sample test and not a spring stuff):

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);
    }
}

So the problem is likely with a version of maven/sufrefire plugin or maybe java interaction, and not with your pom.

org.apache.maven.surefire.booter.ForkedBooter is loaded way before any test runs (its a part of surefire plugin).

In terms of resolution, I suggest you checking the version of java (spring 5 requires java 8 at least, BTW) and maven, and maybe even upgrade a surefire plugin.

Since you haven't provided any specific versions, its hard to say something more concrete.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM