0

I am trying to mock one of my private method , it is a must for me to mock it . Can any one help me on this . Because my code tries to traverses through the code .I dont need to traverse through the method . I just need to mock my private method and returns a String . Below is my method

Please note : I have referred all the duplicate marked questions, I tried all those and I am unable to find a solution. Please help. Also one of my parameter is a byte array .

private String decodeResponse(byte bresp[])
    {

        String spresp = null;

        byte bcontent[] = new byte[bresp.length - 2];

        for(int i = 0; i < bcontent.length; i++)
            bcontent[i] = bresp[i + 1];

        try
        {
            spresp = new String(bcontent, "US-ASCII");
        }
        catch(UnsupportedEncodingException ex)
        {
            return null;
        }

        return spresp;
    }

Below is my test class

@PrepareForTest(SPSocketCommunicator.class)
@RunWith(PowerMockRunner.class)
public class SPSocketCommunicatorTest{

      @test
        public void test() throws Exception {
        byte[] temp = null;
        SPSocketCommunicator socketCommunicator=new SPSocketCommunicator(request, Map);
                SPSocketCommunicator spy = PowerMockito.spy(socketCommunicator);
        PowerMockito.spy(SPSocketCommunicator.class);
                PowerMockito.doReturn("a string").when(spy,"decodeResponse",Matchers.<byte[]>any());
        socketCommunicator.decodeResponse(temp);
        }
}
Miller
  • 744
  • 3
  • 15
  • 39
  • @Kayaman : I tried all the solutions that you have marked . But I am unable to find a solution . Please help – Miller Nov 20 '17 at 12:02
  • It's not a good way to test private method. It's better to test the side effect of the public method which use your private method. Private method must born from refactoring after your production was tested – mickaelw Nov 20 '17 at 12:12

0 Answers0