2014-05-16 129 views
1

我試圖測試我@Service@Repository類在我的項目有彈簧引導啓動測試和@Autowired不工作了,我測試的類。@ComponentScan不測試工作與彈簧引導啓動測試

單元測試:

@RunWith(SpringRunner.class) 
@SpringBootTest 
@ContextConfiguration(classes = HelloWorldConfiguration.class 
//@SpringApplicationConfiguration(classes = HelloWorldRs.class) 
//@ComponentScan(basePackages = {"com.me.sbworkshop", "com.me.sbworkshop.service"}) 
//@ConfigurationProperties("helloworld") 
//@EnableAutoConfiguration 
//@ActiveProfiles("test") 
// THIS CLASS IS IN src/test/java/ AND BUILDS INTO target/test-classes 
public class HelloWorldTest { 

    @Autowired 
    HelloWorldMessageService helloWorldMessageService; 

    public static final String EXPECTED = "je pense donc je suis-TESTING123"; 

    @Test 
    public void testGetMessage() { 
     String result = helloWorldMessageService.getMessage(); 
     Assert.assertEquals(EXPECTED, result); 
    } 
} 

服務:

@Service 
@ConfigurationProperties("helloworld") 
// THIS CLASS IS IN /src/main/java AND BUILDS INTO target/classes 
public class HelloWorldMessageService { 

    private String message; 

    public String getMessage() { 
     return message; 
    } 

    public void setMessage(String message) { 
     this.message=message; 
    } 

} 

在單元測試的評論類註釋代表我試圖得到這個工作的各種事情。測試和項目包都在相同的包路徑中,並且@ComponentScan在我的入口點(主類爲@RestController類)下正常工作。該服務@ComponentScan的和@Autowire的罰款,我在src /主/ Java方面@RestController類,但並沒有在測試中。爲了使@Autowired正常工作,我需要在我的@Configuration課程中再次將其添加爲@Bean。該類在其他方面的範圍很好,我可以在測試中引用並實例化它。問題似乎是@ComponentScan似乎不能正確地遍歷我的測試運行器類路徑中的多個條目,在這種情況下是/ target/test-classes和/ target/classes。

我使用的是IDE的IntelliJ IDEA 13

UPDATE - 這裏有HelloWorldRs和它的配置:

@RestController 
@EnableAutoConfiguration 
@ComponentScan 
public class HelloWorldRs { 

    // SPRING BOOT ENTRY POINT - main() method 
    public static void main(String[] args) { 
     SpringApplication.run(HelloWorldRs.class); 
    } 

    @Autowired 
    HelloWorldMessageService helloWorldMessageService; 

    @RequestMapping("/helloWorld") 
    public String helloWorld() { 
     return helloWorldMessageService.getMessage(); 
    } 

} 

...

@Configuration 
public class HelloWorldConfiguration { 

    @Bean 
    public Map<String, String> map() { 
     return new HashMap<>(); 
    } 

    // This bean was manually added as a workaround to the @ComponentScan problem 
    @Bean 
    public HelloWorldMessageService helloWorldMessageService() { 
     return new HelloWorldMessageService(); 
    } 

    // This bean was manually added as a workaround to the @ComponentScan problem 
    @Bean 
    public HelloWorldRs helloWorldRs() { 
     return new HelloWorldRs(); 
    } 
} 
+1

'HelloWorlsRs'看起來像什麼? – geoand

+0

你的測試用例不是一個配置,所有這些評論的東西都是不相關的。 – chrylis

+0

查看HelloWorldRs更新後的帖子等。 –

回答

1

我不知道這是否會成爲解決方案,但不要使用默認包(即不要將* .java直接放在「src/main/java」中),並且絕對不要使用默認包中包含或@EnableAutoConfiguration。您將在啓動時終止您的應用程序,因爲它會嘗試掃描類路徑中的所有內容(包括所有的Spring庫)。

+0

點 - 只是試圖讓組件加載,因此嘗試從src/main/java加載,以確保沒有錯過任何東西 –

0

首先,我建議使用較新的@RunWith(SpringRunner.class)但沒什麼區別,它僅僅是短(推薦)。

其次,從@EnableAutoConfiguration我看到你正在使用的彈簧啓動 - 這當然是一件好事。爲什麼不是直接使用@ComponentScan有一些很好的理由。你可以嘗試以下嗎?

@RunWith(SpringRunner.class) 
@SpringBootTest 
@ContextConfiguration(classes=YourApplication_or_other_Configuration.class) 
public class HelloWorldTest { 
... etc.