2017-09-13 73 views
0

我想測試我的LoginCacheServiceImpl但有問題。我遵循本教程http://crunchify.com/how-to-create-a-simple-in-memory-cache-in-java-lightweight-cache/,並用我自己的模型對象LoginEvent將其稱爲InMemoryCache。解析@PostConstruct中的TestPropertySource時的numberformatexception

@Service 
@PropertySource("classpath:app.properties") 
public class LoginCacheServiceImpl { 

    @Value("$app{timeToLive}") 
    private long timeToLive; 

    @Value("$app{timerInterval}") 
    private long timerInterval; 

    private InMemoryCache<String, LoginEvent> cache; 

    @Override 
    public InMemoryCache<String, LoginEvent> getCache() { 
     return cache; 
    } 

    @PostConstruct 
    public void init() { 
     cache = new InMemoryCache<>(timeToLive, timerInterval, 10000); 
    } 
} 

該代碼運行良好,但我試圖爲它編寫單元測試。在測試類:

@TestPropertySource("classpath:app-test.properties") 
@ContextConfiguration(classes = { LoginCacheServiceImplTest.class, LoginCacheServiceImpl.class }) 
@RunWith(SpringJUnit4ClassRunner.class) 
public class LoginCacheServiceImplTest 
    @Autowired 
    private Environment environment; 

    private LoginCacheServiceImpl cacheService; 

    @Before 
    public void setUp() { 
     String test = environment.getProperty("timeToLive"); // this value gets parsed correctly 
     cacheService = new LoginCacheServiceImpl();    
    } 

    @Test 
    public void doNothing() { 
     System.out.println("test"); 
    } 
} 

我得到的錯誤:

Caused by: org.springframework.beans.TypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'long'; nested exception is java.lang.NumberFormatException: For input string: "$app{timeToLive}" 
    at org.springframework.beans.TypeConverterSupport.doConvert(TypeConverterSupport.java:77) 
    at org.springframework.beans.TypeConverterSupport.convertIfNecessary(TypeConverterSupport.java:54) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:968) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:949) 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533) 
    ... 42 more 
Caused by: java.lang.NumberFormatException: For input string: "$app{timeToLive}" 

我不知道我應該怎麼要麼重寫我的LoginCacheServiceImpl使其可測試的,或者最好設置我的測試類所以PostConstruct可以正確解析timeToLive和timerInterval。

app-test.properties:

timeToLive=1000 
timerInterval=1000 

順便說一句,我使用的Mockito 2.X.你可以同時使用MockitoJUnitRunner和SpringJUnit4ClassRunner嗎?如果不是,你如何選擇使用哪一個(對不起,Java新手)。

編輯: 如果我有這條線

@ContextConfiguration(classes = { LoginCacheServiceImplTest.class, LoginCacheServiceImpl.class }) 

單元測試給了我,numbeformatexception錯誤,當我嘗試運行測試,因爲它試圖創建LoginCacheServiceImpl。但是,如果我將其更改爲

@ContextConfiguration(classes = { LoginCacheServiceImplTest.class }) 

,只是做

String prop = environment.getProperty("timeToLive"); 

凡在我app-test.properties文件有

timeToLive=1000 

單元測試可以讀取屬性文件。 timeToLive=1000與我在原始app.properties文件中的數量不同,所以我不認爲這是找到app-test.properties文件的問題。

+1

app-test.properties的內容是什麼 – amitmah

+0

它只有timeToLive和timerInterval在裏面。 timeToLive = 1000我認爲它沒有問題,因爲我可以讀取該值,而無需對logincacheservice進行實例化。 – Crystal

+0

看來測試運行無法根據您提到的錯誤讀取或找到該文件。這已經在這裏討論過了:https://stackoverflow.com/questions/17353327/populating-spring-value-during-unit-test – amitmah

回答

0

請檢查下面的更新類:

@Service 
@PropertySource("classpath:app.properties") 
public class LoginCacheServiceImpl { 

    @Value("${timeToLive}") 
    private long timeToLive; 

    @Value("${timerInterval}") 
    private long timerInterval; 

    private InMemoryCache<String, LoginEvent> cache; 

    @Override 
    public InMemoryCache<String, LoginEvent> getCache() { 
     return cache; 
    } 

    @PostConstruct 
    public void init() { 
     cache = new InMemoryCache<>(timeToLive, timerInterval, 10000); 
    } 
} 

如果您propertySource能夠正確地找到classpath中app.properties,您可以用$ {}鍵名訪問屬性。 當擴展PropertyResolver時,環境對象已經具有由spring容器配置的屬性。檢查相同的文檔。 Environment spring

或者不使用@Value,您可以自動裝入@Environment並在您的服務類中使用它。

相關問題