2

I want to write mockito test for code given below. Is there any solution?

 @Override
public String getPortDirection()
{
    return NameTokens.INPUT_PORT_DIRECTION_VALUE;
}
Avinash Jadhav
  • 1,243
  • 2
  • 12
  • 20

2 Answers2

1

I'm not sure, wether the test will be helpfull, but it is at least easy to implement:

@Test
public void checkPortDirection() {
    assertEquals(NameTokens.INPUT_PORT_DIRECTION_VALUE, getPortDirection());
}

Mockito is not used in this Test, just JUnit.

slartidan
  • 20,403
  • 15
  • 83
  • 131
  • actually both function or you can say class are written in different package.so basically how to call that method getPortDirection in this test class.And also basically Nametokens is class in which that INPUT_PORT_DIRECTION_VALUE variable defined which also belongs to another pakage.So how to take this string value? – Avinash Jadhav Aug 21 '15 at 05:56
  • Usaually refering to constants and methods from other packages is no problem. Check, that your classes include the correct `import` statements. But if your constant and method are *package private* or *protected, you will have to make them `public`. – slartidan Aug 21 '15 at 06:26
  • here you have written test for any non null value.What is this testing when selected object is null? – Avinash Jadhav Aug 25 '15 at 06:16
0

If you want to mock the value of the static field (with null), then you'll have to use PowerMockito. Take a look at this answer: https://stackoverflow.com/a/8911517/529256 Basic Mockito cannot mock static methods, fields, or constructors.

Community
  • 1
  • 1
Craig
  • 2,286
  • 3
  • 24
  • 37