Grails 3.3.8单元测试服务

时间:2019-02-22 20:59:30

标签: unit-testing grails

我正在尝试测试使用src / main / webapp / folder下的文件的服务方法,但是由于任何原因似乎没有访问权限或某些原因,它无法正常工作,获取文件的代码是以下:

def file = grailsApplication.parentContext.getResource("folder/file.txt").file

,错误是:

ServletContext resource [/folder/file.txt] cannot be resolved to absolute file path - web application archive not expanded?

有什么想法吗? 它不是grailsApplication ininction或get resource方法,我认为服务在测试框架使用该服务时无法访问该路径。

1 个答案:

答案 0 :(得分:0)

查看位于https://github.com/jeffbrown/davidpadillaservice的项目。

https://github.com/jeffbrown/davidpadillaservice/blob/master/src/main/resources/folder/file.txt

#include <gtk/gtk.h>

static gboolean button_press_callback (GtkWidget *event_box, GdkEventButton *event, gpointer data)
{
  g_print ("Event box clicked at coordinates %f,%f\n",
           event->x, event->y);

  /* Returning TRUE means we handled the event, so the signal
   * emission should be stopped (don't call any further
   * callbacks that may be connected). Return FALSE
   * to continue invoking callbacks.
   */
  return TRUE;
}

static GtkWidget*
create_image (void)
{
  GtkWidget *image;
  GtkWidget *event_box;

  image = gtk_image_new_from_file ("image.png");
}

int main(int argc, char const *argv[])
{
  create_image();
  return 0;
}

https://github.com/jeffbrown/davidpadillaservice/blob/master/grails-app/services/davidpadillaservice/SomeResourceService.groovy

line number one
line number two
line number three

https://github.com/jeffbrown/davidpadillaservice/blob/master/src/test/groovy/davidpadillaservice/SomeResourceServiceSpec.groovy

package davidpadillaservice

import org.springframework.beans.factory.annotation.Value
import org.springframework.core.io.Resource

class SomeResourceService {

    @Value("classpath:folder/file.txt")
    Resource fileResource

    String getFirstLine() {
        getLine(0)
    }

    String getSecondLine() {
        getLine(1)
    }

    protected String getLine(int index) {
        fileResource.file.readLines().get(index)
    }
}

我希望有帮助。