2016-12-16 73 views
0

我學習我和春天創造的一切在相同的封裝接口和類junit.framework.AssertionFailedError同時測試春天

public interface CompactDisc { 
    public void play(); 
} 

-

@Component 
public class StgPeppers implements CompactDisc{ 
    private String title = "Stg. Pepper's Lonely Hearts Club Band"; 
    private String artist = "The Beatles"; 
    @Override 
    public void play(){ 
     System.out.println("Playing "+title+" by "+artist); 
    } 
} 

-

@Configuration 
@ComponentScan 
public class CDPlayerConfig { 
} 

這是使用

import junit.framework.Assert; 
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; 

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

    @Autowired 
    private CompactDisc cd; 

    @Test 
    public void cdShouldNotBeNull() { 
     Assert.assertNotNull(cd); 
    } 

} 

我下面從一本書中這些例子中,它要求以測試它的工作但不知道如何做到這一點,所以我同一個包下創建一個主類,只是這是否

public class Main { 

    public static void main(String[] args){ 
     new CDPlayerTest().cdShouldNotBeNull(); 
    } 

} 

在控制檯我有

Exception in thread "main" junit.framework.AssertionFailedError 
at junit.framework.Assert.fail(Assert.java:55) 
at junit.framework.Assert.assertTrue(Assert.java:22) 
at junit.framework.Assert.assertNotNull(Assert.java:256) 
at junit.framework.Assert.assertNotNull(Assert.java:248) 
at automaticWiring.CDPlayerTest.cdShouldNotBeNull(CDPlayerTest.java:30) 
at automaticWiring.Main.main(Main.java:17) 

所有類的進口是正確的,即使省略了,我使用的是Netbeans的web應用程序與Spring Web MVC框架,我也只好JUnit的4.12 jar添加到庫中,因爲它WASN 「T在那裏開始。 Spring代碼有什麼問題,或者我的測試方法不正確?

的web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"> 
<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>/WEB-INF/applicationContext.xml</param-value> 
</context-param> 
<listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
</listener> 
<servlet> 
    <servlet-name>dispatcher</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <load-on-startup>2</load-on-startup> 
</servlet> 
<servlet-mapping> 
    <servlet-name>dispatcher</servlet-name> 
    <url-pattern>*.htm</url-pattern> 
</servlet-mapping> 
<session-config> 
    <session-timeout> 
     30 
    </session-timeout> 
</session-config> 
<welcome-file-list> 
    <welcome-file>redirect.jsp</welcome-file> 
</welcome-file-list> 
</web-app> 
+0

您從未初始化成員'cd',並且我相信您不應該直接調用測試方法,而是讓JUnit調用它。 –

+0

我應該如何初始化它? – Maver

+0

右鍵點擊IDE中的測試文件>運行 – dimitrisli

回答

0

你可以試試這個:

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

    @Autowired 
    private CompactDisc cd; 
    ... 
} 

請記住,測試類執行測試前whene加載整個應用程序上下文。

+0

使用Spring Framework 4.0.1和junit 4.12庫我不能導入_SpringBootTest_和_SpringRunner_ – Maver

相關問題