tiebin-zhang / powermock

Automatically exported from code.google.com/p/powermock
Apache License 2.0
0 stars 0 forks source link

StringBuffer is not being mocked #284

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Create class with the following method 

  public static void tuftaMethod(){
        StringBuffer buff = new StringBuffer();
        buff.toString();
    }

2.Create test for the method with the following body

    @Test
    public void testTuftaMehtod() throws Exception {
        StringBuffer buff = createMock(StringBuffer.class);
        expectNew(StringBuffer.class).andReturn(buff);
        replayAll();

        JustAskNotifierJob.tuftaMethod();

        verifyAll();

    }
3. Run the test

The mock of StringBuffer is not being created the test passes(if a MOCK had 
been created it would fail) and the debug shows that a REAL StringBuffer object 
has been created instead of EasyMock.class.

We are using powermock-easymock-full.jar v 1.4.5

Thanks in advance.

Regards,
Nune.

Original issue reported on code.google.com by nisabek on 12 Oct 2010 at 1:52

GoogleCodeExporter commented 9 years ago
What do you prepare for test?

Original comment by johan.ha...@gmail.com on 12 Oct 2010 at 5:54

GoogleCodeExporter commented 9 years ago
Only the class that is being tested. I tried to prepare StringBuffer but it 
didn't help.

Original comment by nisabek on 13 Oct 2010 at 4:18

GoogleCodeExporter commented 9 years ago
The same thing is for StringBuilder.

Original comment by aznaur...@gmail.com on 13 Oct 2010 at 6:15

GoogleCodeExporter commented 9 years ago
BTW, it's impossible to mock toString() method of any object, it's EasyMock 
limitation  (see 
http://easymock.org/EasyMock3_0_Documentation.html#Object_Methods)

Original comment by mishail.mishail on 14 Oct 2010 at 5:22

GoogleCodeExporter commented 9 years ago
Sorry the issue is invalid. the problem was the toString() method and not 
mocking of StringBuffer. 
What confuses is that the debug shows that the mocked object is of type 
StringBuffer and not of EasyMock type. but other expectations (except of 
toString) work on it.

Thanks.

Original comment by nisabek on 14 Oct 2010 at 6:57

GoogleCodeExporter commented 9 years ago

Original comment by johan.ha...@gmail.com on 14 Oct 2010 at 12:21

GoogleCodeExporter commented 9 years ago
I am not able to create mock object for StringBuffer, what is the solution for 
this??

I am getting the below error!

java.lang.IllegalArgumentException: Cannot subclass final class class 
java.lang.StringBuffer

Original comment by avinash....@gmail.com on 7 Sep 2011 at 10:49