Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Seam组件.getInstance()和单元测试_Java_Unit Testing_Inversion Of Control_Seam_Unitils - Fatal编程技术网

Java Seam组件.getInstance()和单元测试

Java Seam组件.getInstance()和单元测试,java,unit-testing,inversion-of-control,seam,unitils,Java,Unit Testing,Inversion Of Control,Seam,Unitils,我在我的一个类的构造函数中调用Component.getInstance(Needed.class),它不是seam组件。 这很好,但我试图用单元测试来覆盖它,我得到的是上面一行的IllegalStateException。 有没有办法用测试覆盖Component.getInstance 顺便说一下,我正在使用unitils库。。。 提前感谢您在类中使用了,这不适合进行单元测试。试着搬到另一个地方去。这使得单元测试更加容易 服务定位器示例: public class MyService : Se

我在我的一个类的构造函数中调用
Component.getInstance(Needed.class)
,它不是seam组件。 这很好,但我试图用单元测试来覆盖它,我得到的是上面一行的
IllegalStateException
。 有没有办法用测试覆盖Component.getInstance

顺便说一下,我正在使用unitils库。。。 提前感谢

您在类中使用了,这不适合进行单元测试。试着搬到另一个地方去。这使得单元测试更加容易

服务定位器示例:

public class MyService : Service
{
    private Needed dependency;

    public MyService()
    {
        this.dependency = 
            Component.getInstance(Needed.class);
    }
}
依赖项注入示例:

public class MyService : Service
{
    private Needed dependency;

    public MyService(Needed dependency)
    {
        this.dependency = dependency;
    }
}
在执行依赖项注入时,您的类将不会有任何对容器的引用(在您的情况下,
组件
),这使得对类进行单元测试更加容易