小编Zar*_*tra的帖子

Play 2.4中的Guice,DI和单元测试

所以我一直试图通过文档来解决这个问题,但我没有得到任何结果.

我在一个创建存储库对象的服务类中设置了一些简单的DI绑定.简单.但是,当我在测试模式下运行它时,@ Inject什么都不做,并且从不实例化存储库对象.

@Inject
TagRepository tagRepository;
Run Code Online (Sandbox Code Playgroud)

所以在它使用的行上,在测试模式下,我们当然会得到一个NullPointerException

tagRepository.tagExistsByName(tag);
Run Code Online (Sandbox Code Playgroud)

这样就冒充我的测试了:

[error] Test services.TagsServiceTest.testAddNewTag failed: java.lang.NullPointerException: null, took 0.097 sec
[error]     at services.TagService.tagExists(TagService.java:27)
[error]     at services.TagService.addNewTag(TagService.java:18)
[error]     at services.TagsServiceTest.testAddNewTag(TagsServiceTest.java:29)
Run Code Online (Sandbox Code Playgroud)

我的问题是,如何配置我的应用程序以在测试模式下使用Guice注入器?我的控制器没有这个问题,因为实际上正在向他们发出请求,设置了完整的应用程序.

我应该提到的一件事是我正在使用提供程序来为测试提供我的应用程序.我应该使用Guice应用程序构建器吗?如果是这样,那会去哪儿?在这方面,游戏文档并不是很有用.这是提供者

@Override
protected FakeApplication provideFakeApplication() {
    return new FakeApplication(new java.io.File("."), Helpers.class.getClassLoader(), ImmutableMap.of("play.http.router", "router.Routes"), new ArrayList<String>(), null);
}
Run Code Online (Sandbox Code Playgroud)

更新:

以下是基于以下建议的更新

在我的BaseTest类中

    @Override
    protected Application provideApplication() {
        return new GuiceApplicationBuilder().in(Mode.TEST).build();
    }
Run Code Online (Sandbox Code Playgroud)

然后在服务测试类中

    @Before
    public void beforeTest() {
        Injector injector = new GuiceInjectorBuilder().bindings(bind(TagService.class).toInstance(new TagService())).injector();
        tagService = injector.instanceOf(TagService.class);
    }
Run Code Online (Sandbox Code Playgroud)

但是,我仍然得到空指针异常,因为没有注入TagRepository.

回答:

我在想这个有点不对劲.如果您使用需要注入的对象设置注入器,然后从中创建一个实例,您将不再获得NullPointerExceptions

@Before
public void beforeTest() …
Run Code Online (Sandbox Code Playgroud)

java unit-testing guice playframework-2.4

12
推荐指数
1
解决办法
2757
查看次数

Laravel 5.1到5.2作曲家更新错误

每当我尝试运行composer update时,我现在都会收到一个错误,其根本原因如下

调用未定义的方法Illuminate\Bus\Dispatcher :: mapUsing()

我可以确认Laravel 5.2已正确安装,所有其他依赖项也是如此.这只发生在运行php artisan clear-compiled时.

我还根据几小时前在Laracasts论坛上提出的建议更新了我的基本控制器

use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;

abstract class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
}
Run Code Online (Sandbox Code Playgroud)

但我仍然收到错误

更新:只要应用程序完全自举,就会发生这种情况.我的应用程序现在甚至不会运行.

更新2,完整堆栈跟踪:

PHP Fatal error:  Call to undefined method Illuminate\Bus\Dispatcher::mapUsing() in /Users/Zara/Web/cafe/app/Providers/BusServiceProvider.php on line 16
PHP Stack trace:
PHP   1. {main}() /Users/Zara/Web/cafe/artisan:0
PHP   2. Illuminate\Foundation\Console\Kernel->handle() /Users/Zara/Web/cafe/artisan:36
PHP   3. Illuminate\Foundation\Console\Kernel->bootstrap() /Users/Zara/Web/cafe/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php:105
PHP   4. Illuminate\Foundation\Application->bootstrapWith() /Users/Zara/Web/cafe/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php:208
PHP   5. Illuminate\Foundation\Bootstrap\BootProviders->bootstrap() /Users/Zara/Web/cafe/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:203
PHP   6. Illuminate\Foundation\Application->boot() /Users/Zara/Web/cafe/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/BootProviders.php:17
PHP   7. array_walk() /Users/Zara/Web/cafe/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:718
PHP   8. …
Run Code Online (Sandbox Code Playgroud)

php laravel composer-php laravel-5.2

11
推荐指数
1
解决办法
2612
查看次数

神秘玩2.4注射异常

最近升级到Play 2.4,我还在学习所有的小怪癖等等.我正试图让我的索引页面正常工作而且我很难接受它,而且我知道它很小,我很想念.这是错误

CreationException: Unable to create injector, see the following errors:

1) Error in custom provider, Configuration error: Configuration error[Cannot connect to database [default]]
  while locating play.api.db.DBApiProvider
  while locating play.api.db.DBApi
    for parameter 0 at play.db.DefaultDBApi.<init>(DefaultDBApi.java:28)
  at play.db.DefaultDBApi.class(DefaultDBApi.java:28)
  while locating play.db.DefaultDBApi
  while locating play.db.DBApi
    for field at play.db.DBModule$NamedDatabaseProvider.dbApi(DBModule.java:61)
  while locating play.db.DBModule$NamedDatabaseProvider
  at com.google.inject.util.Providers$GuicifiedProviderWithDependencies.initialize(Providers.java:149)
  at play.db.DBModule.bindings(DBModule.java:40):
Run Code Online (Sandbox Code Playgroud)

和我的配置信息(是的,mysql的端口号是正确的)

application.conf

db.default.driver=com.mysql.jdbc.Driver
db.default.url="jdbc:mysql:localhost:33060/coffee_bean"
db.default.username=
db.default.password=""
ebean.default = ["models.*"]
Run Code Online (Sandbox Code Playgroud)

主控制器

public Result index() {
        ObjectNode response = Json.newObject();
        Configuration config = Play.application().configuration();
        response.put(config.getString("coffee.bean.message.key"),config.getString("coffee.bean.success.message"));
        response.put(config.getString("version"), config.getString("coffee.bean.version"));

        return …
Run Code Online (Sandbox Code Playgroud)

java guice ebean playframework-2.4

7
推荐指数
1
解决办法
7354
查看次数

Laravel 5视图未找到

我在Laravel 5中遇到了最奇怪的错误,我在其他任何项目中都没有这样做过.

问题

当我尝试点击我的应用时收到以下错误

没有这样的文件或目录(查看:/home/vagrant/Code/resources/views/layout/master.blade.php)(查看:/home/vagrant/Code/resources/views/layout/master.blade.php)

但是,在我的目录结构中

它肯定存在!

在我的刀片模板中

@extends('layout.master')

@section('content')
jfoewifjewo[ij
@endsection
Run Code Online (Sandbox Code Playgroud)

在我的控制器中

public function index()
    {
        return view('home');
    }
Run Code Online (Sandbox Code Playgroud)

其他人遇到过这个问题?我以前从未见过它.是存储在本地和宅基地都有读/写.

更新:我在Laravel 5.0.13上

php laravel laravel-5 homestead

6
推荐指数
1
解决办法
4480
查看次数

玩2.4和IntellJ不解析类

我正在启动我的第一个Play 2.4项目,我遇到了IntelliJ识别和查找JUnit和Play测试类的问题.这是我看到的截图

问题

基本上,代码智能并没有获得JUnit依赖.当我运行激活测试时,测试似乎运行正常.

问题:如何获取代码智能来解决这些问题我需要将特定目录标记为源吗?

我重新启动了intellij,并使用sbt重建.

java intellij-idea playframework-2.4

6
推荐指数
1
解决办法
614
查看次数