@Test public void should_generate_assertion_for_classes_in_package_using_provided_class_loader() throws Exception { ClassLoader customClassLoader = new MyClassLoader(Thread.currentThread().getContextClassLoader()); Set<Class<?>> classes = collectClasses(customClassLoader, "org.assertj.assertions.generator.data"); for (Class<?> clazz : classes) { assertThat(clazz.isAnonymousClass()) .as("check that " + clazz.getSimpleName() + " is not anonymous") .isFalse(); assertThat(clazz.isLocalClass()) .as("check that " + clazz.getSimpleName() + " is not local") .isFalse(); assertThat(isPublic(clazz.getModifiers())) .as("check that " + clazz.getSimpleName() + " is public") .isTrue(); logger.info("Generating assertions for {}", clazz.getName()); final ClassDescription classDescription = converter.convertToClassDescription(clazz); File customAssertionFile = assertionGenerator.generateCustomAssertionFor(classDescription); logger.info( "Generated {} assertions file -> {}", clazz.getSimpleName(), customAssertionFile.getAbsolutePath()); } }
@Test public void should_generate_assertion_for_class_with_properties_that_clash_with_fields() throws Exception { assertionGenerator.generateCustomAssertionFor( converter.convertToClassDescription(FieldPropertyClash.class)); assertGeneratedAssertClass(FieldPropertyClash.class, "FieldPropertyClash.expected.txt"); }
@Theory public void should_generate_assertion_for_property_with_exception(Class<?> beanClass) throws Exception { assertionGenerator.generateCustomAssertionFor(converter.convertToClassDescription(beanClass)); String expectedContent = readFileToString(new File("src/test/resources/BeanWithOneException.expected.txt")); if (!BEAN_WITH_ONE_EXCEPTION.equals(beanClass)) { expectedContent = expectedContent.replace( BEAN_WITH_ONE_EXCEPTION.getSimpleName(), beanClass.getSimpleName()); expectedContent = expectedContent.replace( " throws java.io.IOException ", " throws java.io.IOException, java.sql.SQLException "); GetterWithException[] getters = { STRING_1_EXCEPTION, BOOLEAN_1_EXCEPTION, ARRAY_1_EXCEPTION, ITERABLE_1_EXCEPTION }; for (GetterWithException getter : getters) { String throwsClause = generateThrowsClause( IOException.class, getter.getPropertyName(), getter.isBooleanType()); String replacement = throwsClause + generateThrowsClause( SQLException.class, getter.getPropertyName(), getter.isBooleanType()); expectedContent = expectedContent.replace(throwsClause, replacement); } } assertThat(fileGeneratedFor(beanClass)).hasContent(expectedContent); }
@Theory public void should_generate_assertion_for_nested_class(NestedClass nestedClass) throws Exception { Class<?> clazz = nestedClass.getNestedClass(); assertionGenerator.generateCustomAssertionFor(converter.convertToClassDescription(clazz)); assertThat(fileGeneratedFor(clazz)) .hasContent(expectedContentFromTemplate(clazz, "NestedClassAssert.template.expected.txt")); }
@Test public void should_generate_hierarchical_assertion_for_artwork_class() throws Exception { assertionGenerator.generateHierarchicalCustomAssertionFor( converter.convertToClassDescription(ArtWork.class), allClasses); assertGeneratedAssertClass(ArtWork.class, "ArtWorkAssert.expected.txt"); assertAbstractGeneratedAssertClass(ArtWork.class, "AbstractArtWorkAssert.expected.txt"); }
@Test public void should_generate_flat_assertion_for_movie_class() throws Exception { abstractFileGeneratedFor(Movie.class).delete(); assertionGenerator.generateCustomAssertionFor(converter.convertToClassDescription(Movie.class)); assertGeneratedAssertClass(Movie.class, "MovieAssert.flat.expected.txt"); assertThat(abstractFileGeneratedFor(Movie.class)).doesNotExist(); }
@Test public void should_generate_assertion_for_classes_using_type_with_same_name() throws IOException { Class<?> clazz = ClassUsingDifferentClassesWithSameName.class; assertionGenerator.generateCustomAssertionFor(converter.convertToClassDescription(clazz)); assertGeneratedAssertClass( ClassUsingDifferentClassesWithSameName.class, "ClassUsingDifferentClassesWithSameName.expected.txt"); }
@Test public void should_generate_assertion_for_class_with_public_fields() throws Exception { assertionGenerator.generateCustomAssertionFor(converter.convertToClassDescription(Team.class)); assertGeneratedAssertClass(Team.class, "TeamAssert.expected.txt"); }
@Test public void should_generate_assertion_for_interface() throws Exception { assertionGenerator.generateCustomAssertionFor( converter.convertToClassDescription(PlayerAgent.class)); assertGeneratedAssertClass(PlayerAgent.class, "PlayerAgentAssert.expected.txt"); }
@Test public void should_generate_assertion_for_class_with_primitives() throws Exception { assertionGenerator.generateCustomAssertionFor( converter.convertToClassDescription(Primitives.class)); assertGeneratedAssertClass(Primitives.class, "PrimitivesAssert.expected.txt"); }
@Test public void should_generate_assertion_for_class_with_predicates() throws Exception { assertionGenerator.generateCustomAssertionFor( converter.convertToClassDescription(BooleanPredicates.class)); assertGeneratedAssertClass(BooleanPredicates.class, "BooleanPredicates.expected.txt"); }