### Question:
JavaClassBuilderImpl extends BaseBuilder implements JavaClassBuilder { @Override public void addImport(String packageName, String object) { Name name = new Name(); name.setQualifier(new Name(packageName)); name.setIdentifier(object); if (!getPackageName().equalsIgnoreCase(packageName) && !CompilationUnitUtils.isImported(getCompilationUnit(), name)) { getCompilationUnit().addImport(new ImportDeclaration(name, false, false)); } } JavaClassBuilderImpl(String packageName, String name, String annotationsPackage); @Override String getName(); @Override void setJavaDoc(String comment); @Override void addPrivateField(String name, String type, String description); @Override void addFieldAnnotation(String name, String annotationName); @Override void addAnnotation(String annotationName); @Override void generateGettersAndSetters(); @Override void addImport(String packageName, String object); static final Logger LOGGER; }### Answer:
@Test public void testAddingImports() throws IOException { Capture<CompilationUnit> compilationUnitCapture = Capture.newInstance(); sourceProject.addCompilationUnit( eq(PACKAGE_NAME), eq(CLASS_NAME), capture(compilationUnitCapture)); replayAll(); javaClassBuilder.addImport("java.util", "List"); javaClassBuilder.addImport("java.util", "List"); javaClassBuilder.addImport("java.util", "List"); javaClassBuilder.build(sourceProject); assertEquals( "package com.github.kklisura;\n\n" + "import java.util.List;\n" + "\n" + "public class ClassName {\n" + "}\n" + "", compilationUnitCapture.getValue().toString()); verifyAll(); }
@Test public void testAddingImportsOnSamePackage() throws IOException { Capture<CompilationUnit> compilationUnitCapture = Capture.newInstance(); sourceProject.addCompilationUnit( eq(PACKAGE_NAME), eq(CLASS_NAME), capture(compilationUnitCapture)); replayAll(); javaClassBuilder.addImport(PACKAGE_NAME, "Test"); javaClassBuilder.addImport("java.util", "List"); javaClassBuilder.build(sourceProject); assertEquals( "package com.github.kklisura;\n\n" + "import java.util.List;\n" + "\n" + "public class ClassName {\n" + "}\n" + "", compilationUnitCapture.getValue().toString()); verifyAll(); }