开发者

How to test ( junit, mockito )

I have a class with 3 field in it.

public class Data implements TestCon{

private UserDTO userDto; 
private UserTestDto userTestDto; 
private boolean status; 


public String getUserId(){
String userId = status ? usertestdto.getUserId() : userDto.getUserId();

if(string==null){
  return userId;
}          
return test_id; --> this string variable is contained in the superclass (TestCon) 
                    that is implemented. 
}

How can I write a test of this code?

  • DTO Since DTOs are pojo, do I need to mock them?

  • or I don't nee开发者_开发问答d to mock the boolean value, right?

my test class

   @ExtendWith(MockitoExtension.class)
   class DataTest{

    void test_getUserId(){
       //How should I write for a correct test? 
    }

   }


DTO Since DTOs are pojo, do I need to mock them?

My personal opinion is, that it's perfectly fine to mock objects, especially if they are difficult to construct. Such objects clutter the test and provide rarely additional value.
On the other hand, if you DTO's internal structure changes and you're now returning a value, that was maybe null before (assuming type has not changed), the mock won't notice this, thus the tests in this class will still run correctly, though they should fail.

Afaik you can mock only objects, thus you can't mock a boolean value.

A third point is, that you need more than one test to catch all use cases (1. status = true, 2. status = false, 3. string==null, 4. string!=null)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜