Skip to content

Commit 1b69b8d

Browse files
author
Tobias Stamann
committed
Added unit tests and updated README.md
1 parent 336729c commit 1b69b8d

File tree

4 files changed

+74
-1
lines changed

4 files changed

+74
-1
lines changed

README.md

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,37 @@ public void yourUnitTestWithPassedInElementAndProcessor() {
224224

225225
}
226226
```
227-
227+
228+
### Testing generated and compiled classes
229+
Testing of the generated code can either be done by providing an integration test project or by using Cute to achieve this as part of the compilation test.
230+
Unfortunately, testing generated code with Cute heavily relies on the Javas reflection api, since generated classes aren't available in your unit test code.
231+
But it's working flawlessly if your generated classes are implementing precompiled interfaces:
232+
233+
````java
234+
@Test
235+
public void blackBoxTest_justCompileCodeAndDoClassTestWithImplementedInterface() {
236+
Cute.blackBoxTest().given().noProcessors()
237+
.andSourceFiles("/TestClassWithImplementedInterface.java")
238+
.whenCompiled()
239+
.thenExpectThat()
240+
.compilationSucceeds()
241+
.andThat()
242+
.generatedClass("io.toolisticon.cute.TestClassWithImplementedInterface")
243+
.testedSuccessfullyBy(new GeneratedClassesTestForSpecificClass() {
244+
@Override
245+
public void doTests(Class<?> clazz, CuteClassLoader cuteClassLoader) throws Exception{
246+
247+
SimpleTestInterface unit = (SimpleTestInterface) clazz.getConstructor().newInstance();
248+
MatcherAssert.assertThat(unit.saySomething(), Matchers.is("WHATS UP?"));
249+
250+
}
251+
})
252+
.executeTest();
253+
}
254+
````
255+
256+
Consider to prefer integration tests over Cute if no precompiled interface is implemented by the generated classes.
257+
228258
# Projects using this toolkit library
229259

230260
- [Annotation processor toolkit](https://github.com/toolisticon/annotation-processor-toolkit) : Toolkit that allows you to build annotation processors in a more comfortable way

cute/src/test/java/io/toolisticon/cute/CuteTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1086,4 +1086,23 @@ public void doTests(Class<?> clazz, CuteClassLoader cuteClassLoader) throws Exce
10861086
.executeTest();
10871087
}
10881088

1089+
1090+
@Test()
1091+
public void blackBoxTest_justCompileCodeAndDoClassTestWithImplementedInterfaceAndRelationsBetweenClasses() {
1092+
Cute.blackBoxTest().given().noProcessors()
1093+
.andSourceFiles("/compiletests/withmultiplerelatedsourcefiles/JustOutput.java", "/compiletests/withmultiplerelatedsourcefiles/TestClassWithImplementedInterface.java")
1094+
.whenCompiled()
1095+
.thenExpectThat()
1096+
.compilationSucceeds()
1097+
.andThat().generatedClass("io.toolisticon.cute.TestClassWithImplementedInterface").testedSuccessfullyBy(new GeneratedClassesTestForSpecificClass() {
1098+
@Override
1099+
public void doTests(Class<?> clazz, CuteClassLoader cuteClassLoader) throws Exception{
1100+
1101+
SimpleTestInterface unit = (SimpleTestInterface) clazz.getConstructor().newInstance();
1102+
MatcherAssert.assertThat(unit.saySomething(), Matchers.is("WHATS UP???"));
1103+
1104+
}
1105+
})
1106+
.executeTest();
1107+
}
10891108
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package io.toolisticon.cute;
2+
3+
public class JustOutput {
4+
5+
public static String getString() {
6+
return "WHATS UP???";
7+
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package io.toolisticon.cute;
2+
3+
import io.toolisticon.cute.TestAnnotation;
4+
import io.toolisticon.cute.testcases.SimpleTestInterface;
5+
6+
/**
7+
* Test class for annotation processor tools.
8+
*/
9+
@TestAnnotation
10+
public class TestClassWithImplementedInterface implements SimpleTestInterface {
11+
12+
@Override
13+
public String saySomething() {
14+
return JustOutput.getString();
15+
}
16+
}

0 commit comments

Comments
 (0)