2017-11-25 243 views
0

Spring Data JPASpring Boot을 사용하고 있습니다. 응용 프로그램의 레이아웃은 나의 목적이@SpringBootConfiguration을 찾을 수 없습니다. 테스트에서 @ContextConfiguration 또는 @SpringBootTest (classes = ...)를 사용해야합니다.

main 
    +-- java 
     +-- com/lapots/game/monolith 
      +-- repository/relational 
       +--RelationalPlayerRepository.java 
      +-- web 
       +--GrandJourneyMonolithApplication.java 
       +-- config 
        +-- RelationalDBConfiguration.java 
test 
    +-- java 
     +-- com/lapots/game/monolith 
      +-- repository/relational 
       +-- RelationalPlayerRepositoryTests.java 
      +-- web 
       +-- GrandJourneyMonolithApplicationTests.java 

저장소 내가

@Configuration 
@EnableJpaRepositories(basePackages = "com.lapots.game.monolith.repository.relational") 
@EntityScan("com.lapots.game.monolith.domain") 
public class RelationalDBConfiguration { 
} 

내 주요 응용 프로그램은 다음과 같습니다 구성을 제공 또한 저장소이

public interface RelationalPlayerRepository extends JpaRepository<Player, Long> { 
} 

모양입니다

@SpringBootApplication 
@ComponentScan("com.lapots.game.monolith") 
public class GrandJourneyMonolithApplication { 

    @Autowired 
    private RelationalPlayerRepository relationalPlayerRepository; 

    public static void main(String[] args) { 
     SpringApplication.run(GrandJourneyMonolithApplication.class, args); 
    } 

    @Bean 
    public CommandLineRunner initPlayers() { 
     return (args) -> { 
      Player p = new Player(); 
      p.setLevel(10); 
      p.setName("Master1909"); 
      p.setClazz("warrior"); 

      relationalPlayerRepository.save(p); 
     }; 
    } 
} 
저장소이

@RunWith(SpringRunner.class) 
@SpringBootTest 
public class GrandJourneyMonolithApplicationTests { 

    @Test 
    public void contextLoads() { 
    } 

} 

시험이

@RunWith(SpringRunner.class) 
@DataJpaTest 
public class RelationalPlayerRepositoryTests { 

    @Autowired 
    private TestEntityManager entityManager; 

    @Autowired 
    private RelationalPlayerRepository repository; 

    @Test 
    public void testBasic() { 
     Player expected = createPlayer("Master12", "warrior", 10); 
     this.entityManager.persist(expected); 
     List<Player> players = repository.findAll(); 
     assertThat(repository.findAll()).isNotEmpty(); 
     assertEquals(expected, players.get(0)); 
    } 

    private Player createPlayer(String name, String clazz, int level) { 
     Player p = new Player(); 
     p.setId(1L); 
     p.setName(name); 
     p.setClazz(clazz); 
     p.setLevel(level); 
     return p; 
    } 
} 

처럼 보인다하지만 테스트를 실행하려고하면 내가

Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.041 sec <<< FAILURE! - in com.lapots.game.monolith.repository.relational.RelationalPlayerRepositoryTests 
initializationError(com.lapots.game.monolith.repository.relational.RelationalPlayerRepositoryTests) Time elapsed: 0.006 sec <<< ERROR! 
java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test 
at org.springframework.util.Assert.state(Assert.java:70) 
at org.springframework.boot.test.context.SpringBootTestContextBootstrapper.getOrFindConfigurationClasses(SpringBootTestContextBootstrapper.java:202) 
at org.springframework.boot.test.context.SpringBootTestContextBootstrapper.processMergedContextConfiguration(SpringBootTestContextBootstrapper.java:137) 
at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildMergedContextConfiguration(AbstractTestContextBootstrapper.java:409) 
at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildDefaultMergedContextConfiguration(AbstractTestContextBootstrapper.java:323) 
at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildMergedContextConfiguration(AbstractTestContextBootstrapper.java:277) 
at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildTestContext(AbstractTestContextBootstrapper.java:112) 
at org.springframework.boot.test.context.SpringBootTestContextBootstrapper.buildTestContext(SpringBootTestContextBootstrapper.java:82) 
at org.springframework.test.context.TestContextManager.<init>(TestContextManager.java:120) 
at org.springframework.test.context.TestContextManager.<init>(TestContextManager.java:105) 
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTestContextManager(SpringJUnit4ClassRunner.java:152) 
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.<init>(SpringJUnit4ClassRunner.java:143) 
at org.springframework.test.context.junit4.SpringRunner.<init>(SpringRunner.java:49) 
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:104) 
at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86) 
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59) 
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26) 
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59) 
at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33) 
at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:283) 
at org.apache.maven.surefire.junit4.JUnit4Provider.executeWithRerun(JUnit4Provider.java:173) 
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:153) 
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:128) 
at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:203) 
at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:155) 
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:103) 

은 무엇입니까 오류를 얻을처럼 응용 프로그램

시험 본다 문제? 이

@Data 
@Entity 
@Table(schema = "app", name = "players") 
public class Player { 
    @Id 
    @GeneratedValue 
    private Long id; 

    @Transient 
    Set<Player> comrades; 

    @Column(unique = true) 
    private String name; 

    private int level; 

    @Column(name = "class") 
    private String clazz; 
} 
+0

'RelationalPlayerRepositoryTests'에'@ContextConfiguration (classes = GrandJourneyMonolithApplication.class)'주석을 달면 어떻게됩니까? –

+0

@SamBrannen'데이터 소스를 끼워 넣지 못했습니다 ' – lapots

답변

0

같은 도메인 Player loooks SpringBoot이는 설정 파일을 찾을 수있는 프로젝트의 위에서 아래로 클래스 경로를 검색하여 시작합니다. 귀하의 설정은 다른 파일에 있으며 문제의 원인입니다. 위 설정을 모노리스 패키지로 옮기면 모든 것이 잘될 것입니다.

+0

이라는 오류가 있지만 테스트에서 구성 클래스를 지정할 수 없어야합니까? – lapots

+0

또한'configuration' 클래스를 옮겼습니다. 도움이되지 않았습니다 – lapots

+0

위의 대답은 정확해야합니다. 응용 프로그램을 [설명서에 제안] (https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#using-boot-structuring-your-code)로 구성하면 테스트를 통해 검색하여 구성을 찾습니다. 자동 구성을 사용하지 않도록 설정하는 특수 효과로 인해 추가 문제가있을 수 있습니다. '@ EnableJpaRepositories','@ EntityScan' 및 @ @ ComponentScan도 제거하십시오. –