2016-09-13 5 views
1

이것이 스프링 부트 또는 gradle 문제인지 알 수 없음통합 테스트 중에 스프링 부트 값이 gradle로 대체되지 않음

Restful Services를 빌드하는 데 스프링 부트 1.4를 사용하고 있습니다. 서비스 상태를 제공하는 StatusController 클래스가 있습니다. 여기에는 서비스 이름, 빌드 타임, 환경 및 버전이 포함됩니다. 여기

내 컨트롤러입니다

@RestController 
public class StatusController 
{ 

    @Value("${service.version: not configured}") 
    private String version; 

    @Value("${service.name: not configured}") 
    private String appName; 

    @Value("${service.env: not configured}") 
    private String env; 

    @Value("${service.timestamp: not configured}") 
    private String buildDate; 

    @RequestMapping(value = "/status", method = RequestMethod.GET) 
    @ResponseStatus(HttpStatus.OK) 
    @ResponseBody 
    public String getStatus() throws JsonProcessingException { 
     Map<String,String> map = new HashMap<>(); 
     map.put("version", version); 
     map.put("appName", appName); 
     map.put("env", env); 
     map.put("buildDate", buildDate); 
     return new ObjectMapper().writeValueAsString(map); 

    } 
} 

SRC/메인/자원/application.properties

service.name=${name} 
service.version=${version} 
service.timestamp=${timestamp} 

build.gradle (만 관련 섹션)

processResources { 
    expand project.properties 
} 

def buildTime() { 
    def today = new Date() 
    def formattedDate = today.format('MM-dd-yyyy HH:mm:ss z') 
    return formattedDate 
} 

ext.timestamp=buildTime() 

나는 버전 de 내 서비스 (bootRun을 gradlew) 시작하고 내가 주입 모든 값 (안 "구성되지 않은"의 기본 값을 참조/상태를 쳤을 때 gradle.properties

version=1.0.0-SNAPSHOT. 

에 벌금을 부과. 그러나, 내 테스트에서 대체되지 않는 값을 참조하지만 주입 된 기본값.

여기에 나는 값이 테스트 클래스에서 대체되지 않습니다 확인, 내 테스트 클래스 디버거를 사용하여

@RunWith(SpringRunner.class) 
@SpringBootTest(webEnvironment= SpringBootTest.WebEnvironment.RANDOM_PORT) 
public class ApplicationTests { 
    @Test 
    public void contextLoads() { 
     System.out.println("test passed"); 
    } 

    @Value("${service.version: not configured}") 
    private String version; 

    @Value("${service.name: not configured}") 
    private String appName; 

    @Value("${service.env: not configured}") 
    private String env; 

    @Value("${service.timestamp: not configured}") 
    private String buildDate; 

    @Autowired 
    private TestRestTemplate restTemplate; 

    @Test 
    public void exampleTest() throws Exception { 
     String lifeCheck = this.restTemplate.getForObject("/status", String.class); 
     System.out.print(lifeCheck); 
    } 
} 

입니다. 뿐만 아니라 여기

컨트롤러

에 대입하지 테스트 위의 실행의 출력입니다

{"appName":" not configured","buildDate":" not configured","env":" not configured","version":" not configured"} 
+0

어디에서 어떻게 테스트를하고 있습니까? 명령 줄에서 Gradle을 통해? IDE의 테스트 지원을 사용하지 않고 IDE 내에서? –

+0

IDE에서 테스트를 실행하는 경우 다음의 Gradle 포럼 토론이 유용 할 수 있습니다. https://discuss.gradle.org/t/gradle-eclipse-seems-to-ignore-any-user-defined- logic-in-processresources-and-processtestresources-tasks/7358/9 –

답변

0

Gradle을에서 별도의 작업이 processTestResources

+0

그 이유는 무엇입니까? src/main/resources/application.properties에서'ProcessResources'에서 일어나는 자리 표시자를 대체 할 것입니다. –

+0

네,하지만 테스트는'src/test/resources/application.properties'를 사용하고 적절한 작업에서 자리 표시자를 확장하지 않습니다. , 예. 'processTestResources' – rorschach

0

장소에서 application.properties 전화가 있다는 것을 잊지 마십시오 src/test/resources/

테스트 클래스는 주요 리소스를 볼 수 없습니다. 테스트 자원 폴더에 값을 넣으면 값이 대체됩니다. :)

+0

값을 주입 중입니다. 주입 순서는 src/main/resources/application.properties 다음에 src/test/resources/application.properties가옵니다. 어쨌든, 테스트 자원 폴더에 넣기 위해 작동하지 않았다. –

+1

스프링 부트 테스트가'src/main/resources'에서'application.properties' 파일을 선택할 수 있다는 것을 지적하고 싶습니다. 이 대답은 정확하지 않습니다. –