org.fest.assertions.Assertions Java Examples

The following examples show how to use org.fest.assertions.Assertions. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: VerboseLoggingOfInvocationsOnMockTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void shouldPrintThrowingInvocationOnMockToStdOut() {
	// given
	Foo foo = mock(Foo.class, withSettings().verboseLogging());
	doThrow(new ThirdPartyException()).when(foo).doSomething("Klipsch");

	try {
		// when
		foo.doSomething("Klipsch");
		fail("Exception excepted.");
	} catch (ThirdPartyException e) {
		// then
           Assertions.assertThat(printed())
                   .contains(getClass().getName())
                   .contains(mockName(foo))
				.contains("doSomething")
				.contains("Klipsch")
                   .contains(ThirdPartyException.class.getName());
	}
}
 
Example #2
Source File: AdapterTemplateCodeGeneratorTest.java    From android-codegenerator-library with Apache License 2.0 6 votes vote down vote up
@Test
public void produceFormActivityCodeTest() throws Exception {
    // given
    List<Resource> resources = Lists.newArrayList(
            new Resource(new ResourceId("button"), new ResourceType("ImageButton")),
            new Resource(new ResourceId("edit_text_name"), new ResourceType("EditText")),
            new Resource(new ResourceId("edit_text_city"), new ResourceType("EditText")),
            new Resource(new ResourceId("header_text"), new ResourceType("TextView")),
            new Resource(new ResourceId("list", "android"), new ResourceType("List"))
    );

    // when
    String generatedCode = templateCodeGenerator.generateCode(resources, "form");

    // then
    String expectedCode = templatesProvider.provideTemplateForName("results/adapters/FormAdapter.java");
    Assertions.assertThat(generatedCode).isNotNull().isEqualTo(expectedCode);
}
 
Example #3
Source File: ActivityTemplateCodeGeneratorTest.java    From android-codegenerator-library with Apache License 2.0 6 votes vote down vote up
@Test
public void produceFormActivityCodeTest() throws Exception {
    // given
    List<Resource> resources = Lists.newArrayList(
            new Resource(new ResourceId("imageButton"), new ResourceType("ImageButton")),
            new Resource(new ResourceId("button"), new ResourceType("Button")),
            new Resource(new ResourceId("edit_text_name"), new ResourceType("EditText")),
            new Resource(new ResourceId("edit_text_city"), new ResourceType("EditText")),
            new Resource(new ResourceId("header_text"), new ResourceType("TextView")),
            new Resource(new ResourceId("list", "android"), new ResourceType("List"))
    );

    // when
    String generatedCode = templateCodeGenerator.generateCode(resources, "form");

    // then
    String expectedCode = templatesProvider.provideTemplateForName("results/activities/FormActivity.java");
    Assertions.assertThat(generatedCode).isNotNull().isEqualTo(expectedCode);
}
 
Example #4
Source File: MethodsBuilderTest.java    From android-codegenerator-library with Apache License 2.0 6 votes vote down vote up
@Test
public void builtOnClickListenerMethodString() throws Exception {
    // given
    methodsBuilder = provideMethodsBuilder(Lists.newArrayList(getMockResourceProvider("done_button", "OnClickListener")));

    // when
    String value = methodsBuilder.builtString();

    // then
    Assertions.assertThat(value).isNotNull().isEqualTo(
            "@Override\n" +
                    "    public void onClick(View view) {\n" +
                    "        switch (view.getId()) {\n" +
                    "            case R.id.done_button:\n" +
                    "                //TODO implement\n" +
                    "                break;\n" +
                    "        }\n" +
                    "    }"
    );
}
 
Example #5
Source File: VerboseLoggingOfInvocationsOnMockTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void shouldPrintRealInvocationOnSpyToStdOut() {
	// given
	FooImpl fooSpy = mock(FooImpl.class,
			withSettings().spiedInstance(new FooImpl()).verboseLogging());
	doCallRealMethod().when(fooSpy).doSomething("Klipsch");
	
	// when
	fooSpy.doSomething("Klipsch");
	
	// then
       Assertions.assertThat(printed())
               .contains(getClass().getName())
               .contains(mockName(fooSpy))
			.contains("doSomething")
			.contains("Klipsch");
}
 
Example #6
Source File: BFragmentTemplateCodeGeneratorTest.java    From android-codegenerator-library with Apache License 2.0 6 votes vote down vote up
@Test
public void produceFormActivityCodeTest() throws Exception {
    // given
    List<Resource> resources = Lists.newArrayList(
            new Resource(new ResourceId("imageButton"), new ResourceType("ImageButton")),
            new Resource(new ResourceId("button"), new ResourceType("Button")),
            new Resource(new ResourceId("edit_text_name"), new ResourceType("EditText")),
            new Resource(new ResourceId("edit_text_city"), new ResourceType("EditText")),
            new Resource(new ResourceId("header_text"), new ResourceType("TextView")),
            new Resource(new ResourceId("list", "android"), new ResourceType("List"))
    );

    // when
    String generatedCode = templateCodeGenerator.generateCode(resources, "form");

    // then
    String expectedCode = templatesProvider.provideTemplateForName("results/activities/BFormFragment.java");
    Assertions.assertThat(generatedCode).isNotNull().isEqualTo(expectedCode);
}
 
Example #7
Source File: MocksSerializationTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void private_constructor_currently_not_supported_at_the_moment_at_deserialization_time() throws Exception {
    // given
    AClassWithPrivateNoArgConstructor mockWithPrivateConstructor = Mockito.mock(
            AClassWithPrivateNoArgConstructor.class,
            Mockito.withSettings().serializable()
    );

    try {
        // when
        SimpleSerializationUtil.serializeAndBack(mockWithPrivateConstructor);
        fail("should have thrown an ObjectStreamException or a subclass of it");
    } catch (ObjectStreamException e) {
        // then
        Assertions.assertThat(e.toString()).contains("no valid constructor");
    }
}
 
Example #8
Source File: CapturingArgumentsTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void should_capture_all_vararg() throws Exception {
    // given
    IMethods mock = mock(IMethods.class);
    ArgumentCaptor<String> argumentCaptor = ArgumentCaptor.forClass(String.class);

    // when
    mock.mixedVarargs(42, "a", "b", "c");
    mock.mixedVarargs(42, "again ?!");

    // then
    verify(mock, times(2)).mixedVarargs(any(), argumentCaptor.capture());

    List<String> allVarargsValues = argumentCaptor.getAllValues();
    Assertions.assertThat(allVarargsValues).containsExactly("a", "b", "c", "again ?!");
}
 
Example #9
Source File: MethodsBuilderTest.java    From android-codegenerator-library with Apache License 2.0 6 votes vote down vote up
@Test
public void builtOnClickListenerMethodWithTwoButtonsString() throws Exception {
    // given
    methodsBuilder = provideMethodsBuilder(Lists.newArrayList(
            getMockResourceProvider("cancel_button", "OnClickListener"),
            getMockResourceProvider("exit_button", "OnClickListener")
    ));

    // when
    String value = methodsBuilder.builtString();

    // then
    Assertions.assertThat(value).isNotNull().isEqualTo(
            "@Override\n" +
                    "    public void onClick(View view) {\n" +
                    "        switch (view.getId()) {\n" +
                    "            case R.id.cancel_button:\n" +
                    "                //TODO implement\n" +
                    "                break;\n" +
                    "            case R.id.exit_button:\n" +
                    "                //TODO implement\n" +
                    "                break;\n" +
                    "        }\n" +
                    "    }"
    );
}
 
Example #10
Source File: DeepStubbingTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void verificationMode_only_work_on_the_last_returned_mock() throws Exception {
    // 1st invocation on Address mock (stubbing)
    when(person.getAddress("the docks").getStreet().getName()).thenReturn("deep");

    // 2nd invocation on Address mock (real)
    person.getAddress("the docks").getStreet().getName();
    // 3rd invocation on Address mock (verification)
    // (Address mock is not in verification mode)
    verify(person.getAddress("the docks").getStreet()).getName();

    try {
        verify(person.getAddress("the docks"), times(1)).getStreet();
        fail();
    } catch (TooManyActualInvocations e) {
        Assertions.assertThat(e.getMessage())
                .contains("Wanted 1 time")
                .contains("But was 3 times");
    }
}
 
Example #11
Source File: MethodsBuilderTest.java    From android-codegenerator-library with Apache License 2.0 5 votes vote down vote up
@Test
public void builtNoMethodsString() throws Exception {
    // given
    methodsBuilder = provideMethodsBuilder(Lists.<ResourceProvider>newArrayList());

    // when
    String value = methodsBuilder.builtString();

    // then
    Assertions.assertThat(value).isNotNull().isEqualTo("");
}
 
Example #12
Source File: InvocationMatcherTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void should_capture_varargs_as_vararg() throws Exception {
    //given
    mock.mixedVarargs(1, "a", "b");
    Invocation invocation = getLastInvocation();
    CapturingMatcher m = new CapturingMatcher();
    InvocationMatcher invocationMatcher = new InvocationMatcher(invocation, (List) asList(new Equals(1), new LocalizedMatcher(m)));

    //when
    invocationMatcher.captureArgumentsFrom(invocation);

    //then
    Assertions.assertThat(m.getAllValues()).containsExactly("a", "b");
}
 
Example #13
Source File: GenericResourceProviderTest.java    From android-codegenerator-library with Apache License 2.0 5 votes vote down vote up
@Test
public void provideFieldAdvanced2Test() throws Exception {
    // given
    resourceProvider = new ButtonProvider(new Resource(new ResourceId("done_job_button"), new ResourceType("Button")));

    // when
    Map<String, String> values = resourceProvider.provideValues();

    // then
    Assertions.assertThat(values.get("RESOURCE_ID")).isNotNull().isEqualTo("R.id.done_job_button");
    Assertions.assertThat(values.get("RESOURCE_TYPE")).isNotNull().isEqualTo("Button");
    Assertions.assertThat(values.get("RESOURCE_NAME")).isNotNull().isEqualTo("doneJobButton");
}
 
Example #14
Source File: ActivityCodeGeneratorTest.java    From android-codegenerator-library with Apache License 2.0 5 votes vote down vote up
@Test
public void createGameProduceCodeTest() throws Exception {
    // given
    // when
    String producedCode = produceCodeFromFilePath("codegeneration/layouts/create_game.xml");

    // then
    Assertions.assertThat(producedCode).isNotNull().isEqualTo(templatesProvider.provideTemplateForName("results/activities/CreateGameActivity.java"));
}
 
Example #15
Source File: XMLResourceExtractorTest.java    From android-codegenerator-library with Apache License 2.0 5 votes vote down vote up
@Test
public void specificTest() throws Exception {
    // given
    InputStream inputStream = inputStreamProvider.getStreamFromResource("extractor/layouts/specific.xml");

    // when
    List<Resource> resources = resourceExtractor.extractResourceObjectsFromStream(inputStream);

    // then
    Assertions.assertThat(resources).isNotNull().hasSize(1);
    assertResource(resources.get(0), "ViewPager", "android.support.v4.view", "pager", null);
}
 
Example #16
Source File: MenuTemplateCodeGeneratorTest.java    From android-codegenerator-library with Apache License 2.0 5 votes vote down vote up
@Test
public void produceCreateGameMenuCodeTest() throws Exception {
    // given
    List<Resource> resources = Lists.newArrayList(new Resource(new ResourceId("action_done"), new ResourceType("item")));

    // when
    String generatedCode = templateCodeGenerator.generateCode(resources, "create_game");

    // then
    String expectedCode = templatesProvider.provideTemplateForName("results/menus/CreateGame.java");
    Assertions.assertThat(generatedCode).isNotNull().isEqualTo(expectedCode);
}
 
Example #17
Source File: ActivityCodeGeneratorTest.java    From android-codegenerator-library with Apache License 2.0 5 votes vote down vote up
@Test
public void viewPagerProduceCodeTest() throws Exception {
    // given
    // when
    String producedCode = produceCodeFromFilePath("codegeneration/layouts/view_pager.xml");

    // then
    Assertions.assertThat(producedCode).isNotNull().isEqualTo(templatesProvider.provideTemplateForName("results/activities/ViewPagerActivity.java"));
}
 
Example #18
Source File: MenuTemplateCodeGeneratorTest.java    From android-codegenerator-library with Apache License 2.0 5 votes vote down vote up
@Test
public void produceGamesMenuCodeTest() throws Exception {
    // given
    List<Resource> resources = Lists.newArrayList(
            new Resource(new ResourceId("action_refresh"), new ResourceType("item")),
            new Resource(new ResourceId("action_settings"), new ResourceType("item"))
    );

    // when
    String generatedCode = templateCodeGenerator.generateCode(resources, "games");

    // then
    String expectedCode = templatesProvider.provideTemplateForName("results/menus/Games.java");
    Assertions.assertThat(generatedCode).isNotNull().isEqualTo(expectedCode);
}
 
Example #19
Source File: MockSettingsImplTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void shouldReportErrorWhenAddingNoInvocationListeners() throws Exception {
    try {
        mockSettingsImpl.invocationListeners();
        fail();
    } catch (Exception e) {
        Assertions.assertThat(e.getMessage()).contains("at least one listener");
    }
}
 
Example #20
Source File: TemplateManagerTest.java    From android-codegenerator-library with Apache License 2.0 5 votes vote down vote up
@Test
public void getEmptyTemplateTest() throws Exception {
    // given
    String template = "${RESOURCE_NAME}";
    TemplateManager templateManager = new TemplateManager(template);

    // when
    // then
    String templateManagerTemplate = templateManager.getResult();
    Assertions.assertThat(templateManagerTemplate).isNotNull().isEmpty();
}
 
Example #21
Source File: MockSettingsImplTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void canAddDuplicateInvocationListeners_ItsNotOurBusinessThere() {
	//given
	assertFalse(mockSettingsImpl.hasInvocationListeners());
	
	//when
	mockSettingsImpl.invocationListeners(invocationListener, invocationListener).invocationListeners(invocationListener);
	
	//then
	Assertions.assertThat(mockSettingsImpl.getInvocationListeners()).containsSequence(invocationListener, invocationListener, invocationListener);
}
 
Example #22
Source File: MockSettingsImplTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void shouldAddInvocationListener() {
	//given
	assertFalse(mockSettingsImpl.hasInvocationListeners());
	
	//when
	mockSettingsImpl.invocationListeners(invocationListener);
	
	//then
    Assertions.assertThat(mockSettingsImpl.getInvocationListeners()).contains(invocationListener);
}
 
Example #23
Source File: MockSettingsImplTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void shouldAddVerboseLoggingListenerOnlyOnce() {
	//given
	assertFalse(mockSettingsImpl.hasInvocationListeners());
	
	//when
	mockSettingsImpl.verboseLogging().verboseLogging();
	
	//then
	Assertions.assertThat(mockSettingsImpl.getInvocationListeners()).hasSize(1);
}
 
Example #24
Source File: JUnitFailureHackerTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void shouldReplaceException() throws Exception {
    //given
    RuntimeException actualExc = new RuntimeException("foo");
    Failure failure = new Failure(Description.EMPTY, actualExc);
    
    //when
    hacker.appendWarnings(failure, "unused stubbing");
            
    //then
    assertEquals(ExceptionIncludingMockitoWarnings.class, failure.getException().getClass());
    assertEquals(actualExc, failure.getException().getCause());
    Assertions.assertThat(actualExc.getStackTrace()).isEqualTo(failure.getException().getStackTrace());
}
 
Example #25
Source File: CapturingMatcherTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void should_capture_arguments() throws Exception {
    //given
    CapturingMatcher m = new CapturingMatcher();
    
    //when
    m.captureFrom("foo");
    m.captureFrom("bar");
    
    //then
    Assertions.assertThat(m.getAllValues()).containsSequence("foo", "bar");
}
 
Example #26
Source File: TestBase.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
protected void assertContainsType(final Collection<?> list, final Class<?> clazz) {
    Assertions.assertThat(list).satisfies(new Condition<Collection<?>>() {
        @Override
        public boolean matches(Collection<?> objects) {
            for (Object object : objects) {
                if (clazz.isAssignableFrom(object.getClass())) {
                    return true;
                }
            }
            return false;
        }
    });
}
 
Example #27
Source File: TemplateManagerTest.java    From android-codegenerator-library with Apache License 2.0 5 votes vote down vote up
@Test
public void getTemplateTest() throws Exception {
    // given
    String template = "${RESOURCE_NAME} = (${RESOURCE_TYPE}) findViewById(${RESOURCE_ID});";
    TemplateManager templateManager = new TemplateManager(template);

    // when
    templateManager.addTemplateValue("RESOURCE_NAME", "doneButton");
    templateManager.addTemplateValue("RESOURCE_TYPE", "Button");
    templateManager.addTemplateValue("RESOURCE_ID", "R.id.done_button");

    // then
    String result = templateManager.getResult();
    Assertions.assertThat(result).isNotNull().isEqualTo("doneButton = (Button) findViewById(R.id.done_button);");
}
 
Example #28
Source File: XMLResourceExtractorTest.java    From android-codegenerator-library with Apache License 2.0 5 votes vote down vote up
@Test
public void mainNoHeaderTest() throws Exception {
    // given
    InputStream inputStream = inputStreamProvider.getStreamFromResource("extractor/layouts/main_no_header.xml");

    // when
    List<Resource> resources = resourceExtractor.extractResourceObjectsFromStream(inputStream);

    // then
    Assertions.assertThat(resources).isNotNull().hasSize(0);
}
 
Example #29
Source File: MocksSerializationTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void should_be_serialize_and_have_extra_interfaces() throws Exception {
    //when
    IMethods mock = mock(IMethods.class, withSettings().serializable().extraInterfaces(List.class));
    IMethods mockTwo = mock(IMethods.class, withSettings().extraInterfaces(List.class).serializable());

    //then
    Assertions.assertThat((Object) serializeAndBack((List) mock))
            .isInstanceOf(List.class)
            .isInstanceOf(IMethods.class);
    Assertions.assertThat((Object) serializeAndBack((List) mockTwo))
            .isInstanceOf(List.class)
            .isInstanceOf(IMethods.class);
}
 
Example #30
Source File: MocksSerializationForAnnotationTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void should_fail_when_serializable_used_with_type_that_dont_implements_Serializable_and_dont_declare_a_no_arg_constructor() throws Exception {
    try {
        FailTestClass testClass = new FailTestClass();
        MockitoAnnotations.initMocks(testClass);
        serializeAndBack(testClass.notSerializableAndNoDefaultConstructor);
        fail("should have thrown an exception to say the object is not serializable");
    } catch (MockitoException e) {
        Assertions.assertThat(e.getMessage())
                .contains(NotSerializableAndNoDefaultConstructor.class.getSimpleName())
                .contains("serializable()")
                .contains("implement Serializable")
                .contains("no-arg constructor");
    }
}