2015-01-21 2 views
0

누군가가이 웹 서비스를 테스트하는 가장 좋은 방법을 제안 할 수 있습니까? 그것은 db로부터 데이터를 얻지 않고 대신 자신의 데이터를 생성합니다.정적 Java 웹 서비스를 테스트하는 방법

내 문제는 내 테스트에서 데이터를 유지하는 방법입니다.

@Path("/api/grid/accounts") 
public class GridAccountService { 

    private List<GridAccount> accountList = new ArrayList<GridAccount>(); // contains the statically created data 

    public GridAccountService() { 
     generateAccounts(500); // creates a collection of objects  
    } 

    public List<GridAccount> getAccountList() { // this does not work! Causes a CANNOT FIND SYMBOL error in the test 
     return accountList; 
    } 

    private void generateAccounts(int max) { 
     int count = 0; 
     for (int i = 0; i < max; i++) {     
      accountList.add(new GridAccount("Account" + count)); // other props removed for simplicity! 
     } 
    } 

    @GET 
    @Produces("application/json; charset=utf-8") 
    public PaginationObject getAccounts(@QueryParam("crit") Criteria crit) { 
     List<GridAccount> filteredAccountList = filter(accountListWithDetails, crit); // filter the list of generated accounts according to the criteria entered by the user 
     PaginationObject obj = new PaginationObject(); // return this object 
     obj.setData(filteredAccountList); 
     obj.setTotal(filteredAccountList.size());    
     return obj; 
    } 
} 

private List<GridAccount> filter(List<GridAccount> accountList, Criteria criteria) { 

    // First calls a method to translate the incoming criteria into predicates that can be understood by org.apache.commons.collections.CollectionUtils;  
    Predicate predicate = translate(criteria); 
    // next calls apache's CollectionUtils to filter the list according to the predicate 
    return CollectionUtils.filter(accountList, predicate); 
} 

내 제안 JUnit 테스트 :

public class GridAccountServiceTest { 
    @Test 
    public void testGetAccounts() { 
     /* 
     This successfully calls the service's constructor that creates the data but how do I get hold of it? 
     */ 
     GridAccountService service = new GridAccountService(); 
     /* 
     I can define a getter in the service to get the collection 
     but when I do this, it throws a CANNOT FIND SYMBOL error 
     */ 
     List<GridAcccount> data = service.getAccountList(); 
    } 

} 

이 컴파일러에서 찾을 수없는 SYMBOL 오류는 다음과 같습니다

[ERROR] -> [Help 1] 
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.5.1:testCompile (default- testCom 
pile) on project demo-services: Compilation failure 
C:\ws\html5-framework-build\demo\demo-services\src\test\java\com\sungard\ui\demo \resource\service\grid\GridAccountServiceTest.java:[59,12] error: cannot find symbol 

at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor 
.java:213) 
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor 
.java:153) 
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor 
.java:145) 
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProje 
ct(LifecycleModuleBuilder.java:84) 
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProje 
ct(LifecycleModuleBuilder.java:59) 
    at org.apache.maven.lifecycle.internal.LifecycleStarter.singleThreadedBu 
ild(LifecycleStarter.java:183) 
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(Lifecycl 
eStarter.java:161) 
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:320) 
at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:156) 
at org.apache.maven.cli.MavenCli.execute(MavenCli.java:537) 
at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:196) 
at org.apache.maven.cli.MavenCli.main(MavenCli.java:141) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl. 
java:62) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces 
sorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:483) 
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Laun 
cher.java:290) 
at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.jav 
a:230) 
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(La 
    uncher.java:409) 
    at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java: 
    352) 
    Caused by: org.apache.maven.plugin.CompilationFailureException: Compilation failure 
    C:\ws\html5-framework-build\demo\demo- services\src\test\java\com\sungard\ui\demo  \resource\service\grid\GridAccountServiceTest.java:[59,12] error: cannot  find symbol 

    at org.apache.maven.plugin.AbstractCompilerMojo.execute(AbstractCompiler 
    Mojo.java:729) 
    at org.apache.maven.plugin.TestCompilerMojo.execute(TestCompilerMojo.jav 
    a:161) 
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(Default 
    BuildPluginManager.java:101) 
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor 
    .java:209) 
    ... 19 more 
+0

컴파일에 대한 정보를 묻는 메시지가 나타나면 완전하고 정확한 오류 메시지를 붙여넣고 코드를 표시하십시오. 알 수없는 코드로 인해 알 수없는 오류가 발생하는 이유를 알 수 없습니다. –

+0

stacktrace로 질문을 업데이트했습니다. – Tone

+0

그래서 GridAccountServiceTest.java의 59 행은 무엇입니까? 그것이'List 라인이라면 data = service.getAccountList(); ', 당신이 호출하는 메소드'getAccountList()'의 정의는 어디에 있습니까? –

답변

1

문제는 정말 당신이 확인하고 싶어 무슨 행동입니까?

웹 서비스가 작동하는지 확인하려면 웹 서버를 시작하고 HttpUnit 또는 이와 유사한 요청을 만들어야합니다. 그런 다음 응답 내용을 확인할 수 있습니다.

정적 데이터가 올바르게 생성되었는지 확인하려면 서비스 외부에서 생성 한 다음 생성자 또는 다른 수단을 통해 전달하는 것이 좋습니다 (예 : 설정자 또는 필요한 경우 싱글 톤 사용) . 그런 다음 서비스를 전혀 참조하지 않고 생성 된 데이터를 확인할 수 있습니다.

또는 직접적인 문제를 해결할 수있는 훨씬 더 해킹 된 해결책은 (현재 개인용) accountList 필드를 public으로 만드는 것입니다. 그런 다음 서비스를 구축 한 후 테스트에서 해당 필드에 직접 액세스 할 수 있습니다 (service.accountList).

컴파일러 오류 '기호를 찾을 수 없음'은 일반적으로 IDE에 의해 선택되며 문제가 정확히 강조 표시됩니다. IDE를 사용하지 않는 경우 Eclipse 또는 IntelliJ 설치를 고려할 수 있습니다.

+0

안녕하세요 aetheria, 매우 유용한 의견에 감사드립니다. 당신이 말하는 것은 의미가 있지만 컬렉션을 public으로 변경하더라도 심볼을 찾을 수 없음 오류가 발생합니다. 어딘가에 다른 뭔가가 있어야합니다, 나는 당신의 제안을 따르고 자바 IDE (현재 자바 스크립트에 대한 Webstorm을 사용하여 !!!) – Tone

관련 문제