I am trying to make tests for ActivityStarter library, which is using annotation processing, but I get strange error:
An expected source declared one or more top-level types that were not present.
Expected top-level types: <[MainActivityStarter]>
Declared by expected file:
<com/example/activitystarter/MainActivityStarter.java>
The top-level types that were present are as follows:
- [ainActivityStarter] in </SOURCE_OUTPUT/ainActivityStarter.java>
When I started:
import com.google.testing.compile.JavaFileObjects
import org.junit.Test
import javax.tools.JavaFileObject
import activitystarter.compiler.ActivityStarterProcessor
import com.google.common.truth.Truth.assertAbout
import com.google.testing.compile.JavaSourceSubjectFactory
import org.junit.Assert.*
import com.google.testing.compile.JavaSourceSubjectFactory.javaSource
import com.google.testing.compile.JavaSourcesSubject
import com.google.testing.compile.JavaSourcesSubject.SingleSourceAdapter
import com.google.testing.compile.JavaSourcesSubjectFactory.javaSources
class ActivityStarterTest() {
@Test
fun simpleGenerationTest() {
val beforeProcess = "com.example.activitystarter.MainActivity" to """
import android.app.Activity;
import activitystarter.MakeActivityStarter;
@MakeActivityStarter
public class MainActivity extends Activity {}
"""
val afterProcess = "com.example.activitystarter.MainActivityStarter" to """
import android.content.Context;
import android.content.Intent;
import android.support.annotation.UiThread;
public class MainActivityStarter {
@UiThread
public static void fill(MainActivity activity) {
}
@UiThread
public static void start(Context context) {
Intent intent = new Intent(context, MainActivity.class);
context.startActivity(intent);
}
@UiThread
public static void startWithFlags(Context context, int flags) {
Intent intent = new Intent(context, MainActivity.class);
intent.addFlags(flags);
context.startActivity(intent);
}
@UiThread
public static Intent getIntent(Context context) {
Intent intent = new Intent(context, MainActivity.class);
return intent;
}
}
"""
processingComparator(beforeProcess, afterProcess)
}
fun processingComparator(beforeProcess: Pair<String, String>, afterProcess: Pair<String, String>) {
val source = JavaFileObjects.forSourceString(beforeProcess.first, beforeProcess.second)
val bindingSource = JavaFileObjects.forSourceString(afterProcess.first, afterProcess.second)
assertAbout<SingleSourceAdapter, JavaFileObject, JavaSourceSubjectFactory>(javaSource())
.that(source)
.processedWith(ActivityStarterProcessor())
.compilesWithoutWarnings()
.and()
.generatesSources(bindingSource)
}
}
Both before and after process code was taken from real code before and after process, so it should be ok. Also, in lib there is no way of cutting this first letter:
JavaFile brewJava() {
return JavaFile.builder(bindingClassName.packageName(), getActivityStarterSpec())
.addFileComment("Generated code from ActivityStarter. Do not modify!")
.build();
}
private TypeSpec getActivityStarterSpec() {
TypeSpec.Builder result = TypeSpec
.classBuilder(bindingClassName.simpleName())
.addModifiers(PUBLIC);
and:
bindingClassName = ClassName.get(packageName, className + "Starter");
Any idea how do deal with it?