Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Test generated code that includes Maven dependencies
Test generated code that includes Maven dependencies [message #1822518] Mon, 09 March 2020 09:32 Go to next message
Niels Heltner is currently offline Niels HeltnerFriend
Messages: 6
Registered: March 2019
Junior Member
Hello,

From my DSL I generate Java code which depends on libraries that are resolved through Maven (e.g. a web framework). I would like to write automated tests that test the behavior of the generated classes.

I have looked at the CompilationTestHelper class, which allows me to access the generated classes, but it does not seem like it's able to resolve Maven dependencies.

I have also looked at workbench tests, by extending the AbstractWorkbenchTest class, which seems to be able to resolve Maven dependencies by invoking 'waitForBuild()', but I can't figure out a way to get the generated classes from here.

[Updated on: Mon, 09 March 2020 09:33]

Report message to a moderator

Re: Test generated code that includes Maven dependencies [message #1822521 is a reply to message #1822518] Mon, 09 March 2020 09:36 Go to previous messageGo to next message
Karsten Thoms is currently offline Karsten ThomsFriend
Messages: 739
Registered: July 2009
Location: Dortmund, Germany
Senior Member

Why don't you just simply generate the code to your Maven project and test your generated code with Unit Tests in Maven?
Re: Test generated code that includes Maven dependencies [message #1822597 is a reply to message #1822521] Tue, 10 March 2020 11:45 Go to previous messageGo to next message
Mohan Radhakrishnan is currently offline Mohan RadhakrishnanFriend
Messages: 19
Registered: July 2009
Junior Member
I was looking for a straightforward Maven setup too. Mine is an XTend project create from eclipse. It doesn't have 'src/main/generated-sources/xtend/'. So the following pom.xml isn't useful. My goal is mvn generate-sources. I plan to generate and also write my own tests. As the OP asked I will need dependencies which I plan to include in this pom.xml. The example XTend in git uses an older version of gradle and isn't immediately helpful.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>validator</groupId>
<artifactId>validator-pipeline</artifactId>
<version>0.0.1-SNAPSHOT</version>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<xtext-version>2.21.0</xtext-version>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.eclipse.xtend</groupId>
<artifactId>org.eclipse.xtend.lib</artifactId>
<version>${xtext-version}</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>src/main/generated-sources/xtend/</source>
<source>src/test/generated-sources/xtend/</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-clean-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<phase>clean</phase>
<goals>
<goal>clean</goal>
</goals>
<configuration>
<filesets>
<fileset>
<directory>src/main/generated-sources/xtend/</directory>
</fileset>
<fileset>
<directory>src/test/generated-sources/xtend/</directory>
</fileset>
</filesets>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.eclipse.xtend</groupId>
<artifactId>xtend-maven-plugin</artifactId>
<version>${xtext-version}</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>xtend-install-debug-info</goal>
<goal>testCompile</goal>
<goal>xtend-test-install-debug-info</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Re: Test generated code that includes Maven dependencies [message #1822639 is a reply to message #1822521] Tue, 10 March 2020 16:17 Go to previous messageGo to next message
Niels Heltner is currently offline Niels HeltnerFriend
Messages: 6
Registered: March 2019
Junior Member
Karsten Thoms wrote on Mon, 09 March 2020 09:36
Why don't you just simply generate the code to your Maven project and test your generated code with Unit Tests in Maven?


I'm 100% sure what you mean. My problem is that I based on a .mydsl file generate a pom.xml and Java code which depends on libraries that have to be resolved through Maven. In my tests, if I use the CompilationTestHelper class I can access and invoke the generated Java code through reflection but I cannot get Maven to resolve the dependencies.

[Updated on: Tue, 10 March 2020 16:22]

Report message to a moderator

Re: Test generated code that includes Maven dependencies [message #1822641 is a reply to message #1822639] Tue, 10 March 2020 16:58 Go to previous message
Karsten Thoms is currently offline Karsten ThomsFriend
Messages: 739
Registered: July 2009
Location: Dortmund, Germany
Senior Member

I think you try to do in a unit test what is actually an integration test. In a unit test you should not integrate with Maven, just check that the code is generated as you would like to have it.

For your Maven integration scenario create a reference project that contains DSL files just for testing, invoke the code generator within Maven and write unit tests against the generated code.

A reference project should contain a minimal consistent project with DSL files, generated and manual code. It is a stripped down version of your real project. By doing so, your generator is tested by executing it, and let the build system build what you generated.

I'd not recommend trying to resolve Maven dependencies and do other fancy stuff in your generator's unit tests. You can continue, but then without help.
Previous Topic:Unassigned Rule Calls - 'current' already created
Next Topic:Use Language Server Xtext
Goto Forum:
  


Current Time: Tue Oct 20 11:47:39 GMT 2020

Powered by FUDForum. Page generated in 0.02423 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top