Search in sources :

Example 1 with CreationException

use of samples.expectnew.CreationException in project powermock by powermock.

the class WhenNewCases method canDefineSeveralMockResultForNew.

@Test
public void canDefineSeveralMockResultForNew() throws Exception {
    final Target expectedTarget = new Target(UNKNOWN_TARGET_NAME, UNKNOWN_TARGET_ID);
    whenNew(Target.class).withArguments(eq(TARGET_NAME), eq(TARGET_ID)).thenThrow(new CreationException());
    whenNew(Target.class).withArguments(eq("Unknown"), eq(-1)).thenReturn(expectedTarget);
    Target actualTarget = new ExpectNewDemo().createTarget(new ITarget() {

        @Override
        public int getId() {
            return TARGET_ID;
        }

        @Override
        public String getName() {
            return TARGET_NAME;
        }
    });
    assertThat(actualTarget).isEqualToComparingFieldByField(expectedTarget);
}
Also used : ExpectNewDemo(samples.expectnew.ExpectNewDemo) ITarget(samples.expectnew.ITarget) Target(samples.expectnew.Target) ITarget(samples.expectnew.ITarget) CreationException(samples.expectnew.CreationException) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 2 with CreationException

use of samples.expectnew.CreationException in project powermock by powermock.

the class ExpectNewCases method canDefineSeveralMockResultForNew.

@Test
public void canDefineSeveralMockResultForNew() throws Exception {
    final Target expectedTarget = new Target(UNKNOWN_TARGET_NAME, UNKNOWN_TARGET_ID);
    expectNew(Target.class, TARGET_NAME, TARGET_ID).andThrow(new CreationException());
    expectNew(Target.class, "Unknown", -1).andReturn(expectedTarget);
    replay(Target.class);
    Target actualTarget = new ExpectNewDemo().createTarget(new ITarget() {

        @Override
        public int getId() {
            return TARGET_ID;
        }

        @Override
        public String getName() {
            return TARGET_NAME;
        }
    });
    assertThat(actualTarget).isEqualToComparingFieldByField(expectedTarget);
}
Also used : ExpectNewDemo(samples.expectnew.ExpectNewDemo) ITarget(samples.expectnew.ITarget) Target(samples.expectnew.Target) ITarget(samples.expectnew.ITarget) CreationException(samples.expectnew.CreationException) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)2 CreationException (samples.expectnew.CreationException)2 ExpectNewDemo (samples.expectnew.ExpectNewDemo)2 ITarget (samples.expectnew.ITarget)2 Target (samples.expectnew.Target)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1