Note that there are some explanatory texts on larger screens.

plurals
  1. POSpring autowiring keeps JMockit mocks around for other tests
    primarykey
    data
    text
    <p>I'm trying to unit test a class (let's say, "ClassUnderTest") and its use of a particular library class (let's call it "Helper"). So I'm injecting a mocked Helper into the ClassUnderTest and using Expectations to check things out. </p> <pre><code> public class TestUseOfHelper { @Autowired ClassUnderTest classUnderTest; @Mocked HelperClass helper; [...] @Before public void setUp() throws Exception { Deencapsulation.setField(classUnderTest, "helper", helper); } [...] @Test public void test() { new Expectations() {{ helper.doSomething(any); }}; classUnderTest.useHelper(); } </code></pre> <p>So far, so good. </p> <p>In other tests (same package, different file), I'm testing other aspects of the ClassUnderTest where I just want Helper to do its thing. </p> <pre><code> public class TestOtherQualities { @Autowired ClassUnderTest classUnderTest; [...] @Test public void test() { result = classUnderTest.processSomething(); assertNonNull(result); } </code></pre> <p>Those tests work fine as well.</p> <p>The problem comes in when I run the suite of tests. Now, the second set fails, apparently because the Mocked Helper is still in place!</p> <p>I assume that the full suite is being executed in the same JVM and that Spring has created one ClassUnderTest, injects the mock like it's told, but then just reuses the same ClassUnderTest object for the next test. How do I keep them separate? How can I get "fresh" objects for each separate test file? What am I not getting/googling?</p> <p>I've tried numerous variations of defining objects within the Expectations blocks and using @Tested/@Injected but with no luck. BTW, I'm avoiding mocking the Helper in other tests because I need it to do its thing. Also ClassUnderTest autowires a bunch of other objects, which in turn autowire still others, so mocking everything that ClassUnderTest references is impractical.</p> <p>Any insights, Oh knowledgeable Stack Overflow Magic 8 Ball? ("Try Later" is not acceptable.) </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.
 

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