如何测试静态Java Web服务

时间:2015-01-21 13:20:35

标签: 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(); 
   }

}

这是编译器的CAN NOT FIND SYMBOL ERROR:

[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

1 个答案:

答案 0 :(得分:1)

问题是你想检查什么行为?

如果要检查Web服务是否有效,则可能需要启动Web服务器并使用HttpUnit或类似命令来发出请求。然后,您可以验证回复的内容。

如果要检查帐户静态数据是否正确生成,我建议您在服务之外生成它们,并通过构造函数或其他方式(例如通过setter或必要时使用单例)传递它们。然后,您可以检查生成的数据,而无需参考该服务。

或者,一个可以解决您的直接问题的更多hacky解决方案是制作(当前为私有)accountList字段public。然后,您可以使用service.accountList直接从测试(构建服务之后)访问该字段。

编译器错误“找不到符号”通常会被IDE选中,它会突出显示问题所在。如果您没有使用IDE,可以考虑安装Eclipse或IntelliJ。