Running tests from eclipse failed

2,263 views
Skip to first unread message

Umesh

unread,
Jul 9, 2010, 3:30:45 AM7/9/10
to PowerMock
I'm trying to run a simple JUnit test with PowerMockRunner.

I get the following error when I run each of the test methods
individually.

ShoppingCartManagerTest.addToCartWithNoInventory
Unrooted Tests
initializationError0(org.junit.runner.manipulation.Filter)
java.lang.Exception: No tests found matching Method
addToCartWithNoInventory(com.demo.ShoppingCartManagerTest) from
org.junit.internal.requests.ClassRequest@1bb9696
at
org.junit.internal.requests.FilterRequest.getRunner(FilterRequest.java:
37)
at
org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.<init>(JUnit4TestReference.java:
28)
at
org.eclipse.jdt.internal.junit4.runner.JUnit4TestMethodReference.<init>(JUnit4TestMethodReference.java:
24)
at
org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:
41)
at
org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:
30)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:
445)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:
673)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:
386)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:
196)

This error occurs only when i try to run a test method individually
and doesn't occur when the test class is run.

I have a @PrepareForTest for each of my test method.

Please let me know if any of you faced this problem earlier and had a
solution to it.

Johan Haleby

unread,
Jul 11, 2010, 3:25:53 PM7/11/10
to powe...@googlegroups.com
Try using @PrepareForTest at the class-level of your test and see if that helps. You should only use it on method-level in very special cases.

/Johan


--
You received this message because you are subscribed to the Google Groups "PowerMock" group.
To post to this group, send email to powe...@googlegroups.com.
To unsubscribe from this group, send email to powermock+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/powermock?hl=en.


Umesh

unread,
Jul 12, 2010, 3:45:55 AM7/12/10
to PowerMock
It works fine when @PrepareForTest at the class-level. However, my
requirement is to use it at the method level.

Any inputs on this would be extremely helpful.

On Jul 12, 12:25 am, Johan Haleby <johan.hal...@gmail.com> wrote:
> Try using @PrepareForTest at the class-level of your test and see if that
> helps. You should only use it on method-level in very special cases.
>
> /Johan
>
> > powermock+...@googlegroups.com<powermock%2Bunsu...@googlegroups.com>
> > .

Johan Haleby

unread,
Jul 12, 2010, 3:18:11 PM7/12/10
to powe...@googlegroups.com
Why do you need it at the method level?

To unsubscribe from this group, send email to powermock+...@googlegroups.com.

Umesh

unread,
Jul 13, 2010, 2:15:49 AM7/13/10
to powe...@googlegroups.com
I have a set of test cases each having few test methods. Only few of those test methods need @PrepareForTest to be configured.
Below is a code snippet of what I'm trying achieve with Powermock.

@RunWith(PowerMockRunner.class)
public class ShoppingCartManagerTest {

    @Test
    @PrepareForTest(Utils.class)
    public void addToCartWithNoInventory() {
        // this method mocks call to inventory database
       
        // inventory service uses Utils class to perform some validation
    }
   
    @Test
    public void addToCartWithActualInventory() {
        // this method retrieves actual inventory from the database
       
        // Utils class should not be mocked here
    }
}

Can you please suggest if there is any alternative method to achieve this?

Jan Kronquist

unread,
Jul 13, 2010, 5:38:13 AM7/13/10
to powe...@googlegroups.com
Usually it is good practice to separate unit testing and integration
tests. I recommend that you separate your test into two different
classes, one unit test where you use PrepareForTest and one
integration test class. You could still reuse common validation
methods if you need to.

Often I also use a separate transaction for each integration test, for
example using Spring Framework. This is often also configured at the
class level.

/Jan

Umesh

unread,
Jul 13, 2010, 5:51:31 AM7/13/10
to powe...@googlegroups.com
Agree and it makes sense to me. I'll try that option. Thanks for the help.
Reply all
Reply to author
Forward
0 new messages