Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to expect the creation of an array with PowerMock
    primarykey
    data
    text
    <p>I'm currently trying to test a method that creates an array of objects, and then executes a method on a object with the array of objects as the parameter. Everytime I try to do it, I keep getting a <code>constructorNotFound</code> error. I apologize if its unclear, but hopefully you will be able to understand what I'm talking about with my example below.</p> <pre><code>public class Bar { public final String name; public final Bar[] barList; public Bar(String name) { this.name = name; barList = null; } public Bar update(Bar[] bar) { Bar newBar = new Bar(name); newBar.barList = bar; return newBar; } } public class Foo { public static Bar internalMethod( Bar oldBar) { // in reality this method does a lot more // I just kept it simple for demonstration purposes Bar[] bar = new Bar[2]; oldBar = oldBar.update(bar); // about 15-20 method calls with oldBar as a parameter // and require mocking return oldBar; } } </code></pre> <p>Here is the test class:</p> <pre><code>import org.junit.Test; import org.junit.runner.RunWith; import org.powermock.api.easymock.PowerMock; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; @RunWith(PowerMockRunner.class) @PrepareForTest(Bar[].class) public class testFoo { @Test public void test() throws Exception { Bar[] barArr = new Bar[2]; PowerMock.expectNew(Bar[].class).andReturn(barArr); PowerMock.replay(Bar[].class); Bar bar = new Bar("name"); Bar newBar = Foo.internalMethod(bar); // assume there are many more checks to make sure that the appropriate // methods were called and the changes were made assertEquals("name", newBar.name); assertNull(newBar.barList[0]); assertNull(newBar.barList[1]); } } </code></pre> <p><strong>Does anyone know how to deal with a situation like this?</strong></p> <p>I realize that the class design isn't ideal, but, unfortunately, I do not get to change it. I'm using PowerMock 1.4.10 and I do not have access to mockito framework.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. COI honestly don't understand what you are doing here. If you really intend to test a method, it doesn't make sense to manipulate what the method itself does. I would understand if you tried to mock what the method does in order to test some other code that actually uses your `methodToTest` - but this makes no sense to me. Also, `methodToTest` as it is listed here doesn't do anything (except that it adds some work for the garbage collector). So... sorry, but I don't understand what you are talking about.
      singulars
    2. COI've updated the question and method `methodToTest` so that the situation is a little more realistic since my original example was oversimplified as @skirsch pointed out. I've also added in some comments to clarify where I have taken some liberties to simplify the code.
      singulars
    3. COOkay. I guess you cannot do that; look [here](https://code.google.com/p/powermock/wiki/MockConstructor) and search for "array". I'm not sure what you want to achieve (seems like you want to mock exactly what is done anyway? Is it about the expectations?), but still I think if you want to test that method, you should not mess with its internals. The funny thing is that the asserts look like your actually testing the `Bar.update` method; but I assume that's because of the simplifications...
      singulars
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload