Package samples.expectnew

Examples of samples.expectnew.ExpectNewDemo


  @Test(expected = NullPointerException.class)
  public void testNewWithWrongArgument() throws Exception {
    final int numberOfTimes = 2;
    final String expected = "used";

    ExpectNewDemo tested = new ExpectNewDemo();
    ExpectNewServiceUser expectNewServiceImplMock = mock(ExpectNewServiceUser.class);
    Service serviceMock = mock(Service.class);

    whenNew(ExpectNewServiceUser.class).withArguments(serviceMock, numberOfTimes).thenReturn(
        expectNewServiceImplMock);
    when(expectNewServiceImplMock.useService()).thenReturn(expected);

    assertEquals(expected, tested.newWithWrongArguments(serviceMock, numberOfTimes));

    verifyNew(ExpectNewServiceUser.class).withArguments(serviceMock, numberOfTimes);
    /*
     * Should throw NPE because the default behavior of Mockito when a
     * something isn't expected is to return a default value. In this case
View Full Code Here


  @Test
  public void testNewWithArguments() throws Exception {
    final int numberOfTimes = 2;
    final String expected = "used";

    ExpectNewDemo tested = new ExpectNewDemo();
    ExpectNewServiceUser expectNewServiceImplMock = mock(ExpectNewServiceUser.class);
    Service serviceMock = mock(Service.class);

    whenNew(ExpectNewServiceUser.class).withArguments(serviceMock, numberOfTimes).thenReturn(
        expectNewServiceImplMock);
    when(expectNewServiceImplMock.useService()).thenReturn(expected);

    assertEquals(expected, tested.newWithArguments(serviceMock, numberOfTimes));

    verifyNew(ExpectNewServiceUser.class).withArguments(serviceMock, numberOfTimes);
  }
View Full Code Here

    fail("Should throw NPE!");
  }

  @Test
  public void testExpectNewButNoNewCallWasMade() throws Exception {
    ExpectNewDemo tested = new ExpectNewDemo();

    MyClass myClassMock1 = mock(MyClass.class);

    whenNew(MyClass.class).withNoArguments().thenReturn(myClassMock1);

    tested.makeDate();

    try {
      verifyNew(MyClass.class).withNoArguments();
      fail("Should throw AssertionError!");
    } catch (AssertionError e) {
View Full Code Here

  @Test
  public void testNewWithParameterTypesAndArguments() throws Exception {
    final int numberOfTimes = 2;
    final String expected = "used";

    ExpectNewDemo tested = new ExpectNewDemo();
    ExpectNewServiceUser expectNewServiceImplMock = mock(ExpectNewServiceUser.class);
    Service serviceMock = mock(Service.class);

    whenNew(ExpectNewServiceUser.class).withParameterTypes(Service.class, int.class)
        .withArguments(serviceMock, numberOfTimes).thenReturn(expectNewServiceImplMock);
    when(expectNewServiceImplMock.useService()).thenReturn(expected);

    assertEquals(expected, tested.newWithArguments(serviceMock, numberOfTimes));

    verifyNew(ExpectNewServiceUser.class).withArguments(serviceMock, numberOfTimes);
  }
View Full Code Here

  @Test
  public void testNewWithConstructorUsingParameterTypesAndArguments() throws Exception {
    final int numberOfTimes = 2;
    final String expected = "used";

    ExpectNewDemo tested = new ExpectNewDemo();
    ExpectNewServiceUser expectNewServiceImplMock = mock(ExpectNewServiceUser.class);
    Service serviceMock = mock(Service.class);

    whenNew(constructor(ExpectNewServiceUser.class, Service.class, int.class)).withArguments(serviceMock,
        numberOfTimes).thenReturn(expectNewServiceImplMock);
    when(expectNewServiceImplMock.useService()).thenReturn(expected);

    assertEquals(expected, tested.newWithArguments(serviceMock, numberOfTimes));

    verifyNew(ExpectNewServiceUser.class).withArguments(serviceMock, numberOfTimes);
  }
View Full Code Here

  @Test
  public void testNewUsingConstructorWithArguments() throws Exception {
    final int numberOfTimes = 2;
    final String expected = "used";

    ExpectNewDemo tested = new ExpectNewDemo();
    ExpectNewServiceUser expectNewServiceImplMock = mock(ExpectNewServiceUser.class);
    Service serviceMock = mock(Service.class);

    whenNew(constructor(ExpectNewServiceUser.class)).withArguments(serviceMock, numberOfTimes).thenReturn(
        expectNewServiceImplMock);
    when(expectNewServiceImplMock.useService()).thenReturn(expected);

    assertEquals(expected, tested.newWithArguments(serviceMock, numberOfTimes));

    verifyNew(ExpectNewServiceUser.class).withArguments(serviceMock, numberOfTimes);
  }
View Full Code Here

  @Test
  public void testNewWithVarArgs() throws Exception {
    final String firstString = "hello";
    final String secondString = "world";

    ExpectNewDemo tested = new ExpectNewDemo();
    VarArgsConstructorDemo varArgsConstructorDemoMock = mock(VarArgsConstructorDemo.class);

    whenNew(VarArgsConstructorDemo.class).withArguments(firstString, secondString).thenReturn(
        varArgsConstructorDemoMock);
    when(varArgsConstructorDemoMock.getAllMessages()).thenReturn(new String[] { firstString, secondString });

    String[] varArgs = tested.newVarArgs(firstString, secondString);
    assertEquals(2, varArgs.length);
    assertEquals(firstString, varArgs[0]);
    assertEquals(secondString, varArgs[1]);

    verifyNew(VarArgsConstructorDemo.class).withArguments(firstString, secondString);
View Full Code Here

    }
  }

  @Test
  public void testNewWithVarArgsConstructorWhenOneArgumentIsOfASubType() throws Exception {
    ExpectNewDemo tested = new ExpectNewDemo();
    Service serviceMock = mock(Service.class);
    VarArgsConstructorDemo varArgsConstructorDemoMock = mock(VarArgsConstructorDemo.class);

    final Service serviceSubTypeInstance = new Service() {

      public String getServiceMessage() {
        return "message";
      }
    };

    whenNew(VarArgsConstructorDemo.class).withArguments(serviceSubTypeInstance, serviceMock).thenReturn(
        varArgsConstructorDemoMock);
    when(varArgsConstructorDemoMock.getAllServices()).thenReturn(new Service[] { serviceMock });

    Service[] varArgs = tested.newVarArgs(serviceSubTypeInstance, serviceMock);
    assertEquals(1, varArgs.length);
    assertSame(serviceMock, varArgs[0]);

    verifyNew(VarArgsConstructorDemo.class).withArguments(serviceSubTypeInstance, serviceMock);
  }
View Full Code Here

    verifyNew(VarArgsConstructorDemo.class).withArguments(serviceSubTypeInstance, serviceMock);
  }

  @Test
  public void testNewWithArrayVarArgs() throws Exception {
    ExpectNewDemo tested = new ExpectNewDemo();
    VarArgsConstructorDemo varArgsConstructorDemoMock = mock(VarArgsConstructorDemo.class);

    final byte[] byteArrayOne = new byte[] { 42 };
    final byte[] byteArrayTwo = new byte[] { 17 };
    whenNew(VarArgsConstructorDemo.class).withArguments(byteArrayOne, byteArrayTwo).thenReturn(
        varArgsConstructorDemoMock);
    when(varArgsConstructorDemoMock.getByteArrays()).thenReturn(new byte[][] { byteArrayOne });

    byte[][] varArgs = tested.newVarArgs(byteArrayOne, byteArrayTwo);
    assertEquals(1, varArgs.length);
    assertSame(byteArrayOne, varArgs[0]);

    verifyNew(VarArgsConstructorDemo.class).withArguments(byteArrayOne, byteArrayTwo);
  }
View Full Code Here

    verifyNew(VarArgsConstructorDemo.class).withArguments(byteArrayOne, byteArrayTwo);
  }

  @Test
  public void testNewWithArrayVarArgsAndMatchers() throws Exception {
    ExpectNewDemo tested = new ExpectNewDemo();
    VarArgsConstructorDemo varArgsConstructorDemoMock = mock(VarArgsConstructorDemo.class);

    final byte[] byteArrayOne = new byte[] { 42 };
    final byte[] byteArrayTwo = new byte[] { 17 };
    whenNew(VarArgsConstructorDemo.class).withArguments(byteArrayOne, byteArrayTwo).thenReturn(
        varArgsConstructorDemoMock);
    when(varArgsConstructorDemoMock.getByteArrays()).thenReturn(new byte[][] { byteArrayOne });

    byte[][] varArgs = tested.newVarArgsWithMatchers();
    assertEquals(1, varArgs.length);
    assertSame(byteArrayOne, varArgs[0]);

    verifyNew(VarArgsConstructorDemo.class).withArguments(byteArrayOne, byteArrayTwo);
  }
View Full Code Here

TOP

Related Classes of samples.expectnew.ExpectNewDemo

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.