2016-11-22 49 views
2
改写模拟值
@RunWith(MockitoJUnitRunner.class) 
public class Test { 

    @Mock 
    private SomeDependency<T> obj; 

    @InjectMocks 
    private SomeClass mainObj; 

    @Test 
    public void dependencyShouldBeNotNull() { 
     //here I need one value of SomeDependency obj 
     assertEquals(2, mainObj.method()) 
    } 

    @Test 
    public void dependencyShouldBeNull() { 
     //here I need SomeDependency obj to be null 
     assertEquals(1, mainObj.method()) 
    } 

主要类:的Mockito:在不同的方法

class SomeClass { 
     private SomeDependency<T> field; 

     public int method() { 
     if(field==null) 
      return 1; 
     else 
      return 2; 
     } 
} 

我的问题:根据不同的方法,需要如何重写的模拟值?

编辑 在主SomeClass的我有这样的代码:

if (obj != null) { 
     //perform some actions 
    } 
+0

'obj = null;'? – Seelenvirtuose

+0

@Seelenvirtuose我试过了,它不起作用 –

+0

设置'obj = null'不起作用,如果你想要'obj == null' ???你应该解释更多你想要的东西。事实上,这个问题很不明确。 – Seelenvirtuose

回答

1

最简单的方法是使用2个测试类而不是1个测试类,因为它在执行测试方法时已经太迟了,因为模拟已经被注入(除非使用应该避免的反射)。

第一个测试

@RunWith(MockitoJUnitRunner.class) 
public class Test1 { 
    @Mock 
    private SomeDependency<T> obj; 

    @InjectMocks 
    private SomeClass mainObj; 

    @Test 
    public void dependencyShouldBeNotNull() { 
     //here I need one value of SomeDependency obj 
     assertEquals(2, mainObj.method()); 
    } 
} 

第二个测试

@RunWith(MockitoJUnitRunner.class) 
public class Test2 { 
    @InjectMocks 
    private SomeClass mainObj; 

    @Test 
    public void dependencyShouldBeNull() { 
     //here I need SomeDependency obj to be null 
     assertEquals(1, mainObj.method()); 
    } 
} 

如果你想只有一个测试类做到这一点,它仍然是可能的,但它更像是一个黑客因为你需要一个不是传统方法的条件注入,所以你需要用MockitoAnnotations.initMocks(obj)以编程方式注入模拟。

而不是直接注入嘲笑成测试类的,我们需要依靠将包含或不现场obj,如果不存在任何将被注入所以这将是null否则你将有一个模拟注入的包装类。

public class TestInjectMocks { 

    /** 
    * Small interface that will be implemented by the wrapper classes 
    * only used to get the main class 
    */ 
    public interface TestConfig { 
     SomeClass getSomeClass(); 
    } 

    @Test 
    public void dependencyShouldBeNotNull() { 
     // This class will allow to get an instance of SomeClass 
     // with the field injected 
     TestConfig obj = new TestConfig() { 
      @Mock 
      private SomeDependency<T> obj; 
      @InjectMocks 
      private SomeClass mainObj; 

      @Override 
      public SomeClass getSomeClass() { 
       return mainObj; 
      } 
     }; 
     MockitoAnnotations.initMocks(obj); 
     SomeClass mainObj = obj.getSomeClass(); 
     //here I need one value of SomeDependency obj 
     assertEquals(2, mainObj.method()); 
    } 

    @Test 
    public void dependencyShouldBeNull() { 
     // This class will allow to get an instance of SomeClass 
     // without the field injected 
     TestConfig obj = new TestConfig(){ 
      @InjectMocks 
      private SomeClass mainObj; 
      @Override 
      public SomeClass getSomeClass() { 
       return mainObj; 
      } 
     }; 
     MockitoAnnotations.initMocks(obj); 
     SomeClass mainObj = obj.getSomeClass(); 
     //here I need SomeDependency obj to be null 
     assertEquals(1, mainObj.method()); 
    } 
} 

注:我们称之为MockitoAnnotations.initMocks(obj)明确不需要注释@RunWith(MockitoJUnitRunner.class)了。

+2

或者不要使用'@InjectMocks'并用手进行适当的注入:) –

0

根据您所发表的东西,我建议使用“Mockito.when()”为第一种方法的方法,然后,如@Seelenvirtuose所示,设置obj = null;。如果这不起作用,您可能需要传入一个不同的Mocked out对象,该对象已初始化为null。请参阅this示例。