VariantSelector { @Nullable public Variant findVariantToSelect(@NotNull AndroidProject androidProject) { Collection<Variant> variants = androidProject.getVariants(); if (variants.size() == 1) { Variant variant = getFirstItem(variants); assert variant != null; return variant; } for (Variant variant : variants) { if ("debug".equals(variant.getName())) { return variant; } } List<Variant> sortedVariants = Lists.newArrayList(variants); sortedVariants.sort(Comparator.comparing(Variant::getName)); return sortedVariants.isEmpty() ? null : sortedVariants.get(0); }  @Nullable Variant findVariantToSelect(@NotNull AndroidProject androidProject);  }
@Test public void findVariantToSelectWithDebugVariant() { Variant debugVariant = mock(Variant.class); Variant releaseVariant = mock(Variant.class); when(myAndroidProject.getVariants()).thenReturn(Lists.newArrayList(debugVariant, releaseVariant)); when(debugVariant.getName()).thenReturn("debug"); when(releaseVariant.getName()).thenReturn("release"); Variant variant = myVariantSelector.findVariantToSelect(myAndroidProject); assertSame(debugVariant, variant); }
@Test public void findVariantToSelectWithoutDebugVariant() { Variant aVariant = mock(Variant.class); Variant bVariant = mock(Variant.class); when(myAndroidProject.getVariants()).thenReturn(Lists.newArrayList(bVariant, aVariant)); when(aVariant.getName()).thenReturn("a"); when(bVariant.getName()).thenReturn("b"); Variant variant = myVariantSelector.findVariantToSelect(myAndroidProject); assertSame(aVariant, variant); }
@Test public void getVariantToSelectWithoutVariants() { when(myAndroidProject.getVariants()).thenReturn(Collections.emptyList()); Variant variant = myVariantSelector.findVariantToSelect(myAndroidProject); assertNull(variant); }