vendredi 24 novembre 2017

Why my mock test always pass for method which returns null but must return String

I'm wriring test to check if method returns correct string. But even if method returns null the test is passed! Why it happens? How correctly to write the test? Thank you!

// TestMyObj.java: My test class
public class TestMyObj {
    @InjectMocks
    private MyClass myClass;

    @Mock
    TestObj testObj;

    private final String test = "test";

    @Before
    public void setup() {
        MockitoAnnotations.initMocks(this);  // or use mockito runner
    }

    @Test
    public void test() {
        when(testObj.get()).thenReturn(test);
        Assert.assertTrue(myClass.get().equals(test));
        verify(testObj).get();
    }
}

// MyClass.java: Class which uses my object
public class MyClass {
    private TestObj testObj;

    public MyClass() {}
    public MyClass(TestObj testObj) { this.testObj = testObj; }
    String get() { return testObj.get(); }
}

// TestObj.java: My testing object 
public class TestObj {
    String get() { return null; }
}

Aucun commentaire:

Enregistrer un commentaire