Maven/junit project organization

I have three packages:

  • Foo provides a certain API
  • FooMySQL (depends on Foo) implements the Foo API using MySQL
  • FooPostgreSQL (depends on Foo) implements the Foo API using PostgreSQL

I have a bunch of junit tests, which should be run twice: once again FooMySQL, and once again FooPostgreSQL. They are the same tests, because the API should behave identically for both implementations, with only the setup and teardown slightly different.

Question: where should I define those tests? The most logical place appears to be in Foo. However, in Foo, they are not executable because Foo doesn't know of any implementation. I can put them into FooMySQL, but it appears I would have to have a full copy on FooPostgreSQL, as any "test" code defined in Foo cannot be referred to via the maven dependency mechanism from FooMySQL etc. (Is that correct, or perhaps I'm missing a trick?)

Alternatively I could create a separate FooTests project, which the FooMySQL and FooPostgreSQL tests could depend on. But that somehow sounds wrong, too. Is there a better idea?

-------------Problems Reply------------

The first thing you should know is that what you should unit-test are your implementations. Always. I think you're leaning to that but I wasn't sure since you mentioned about testing Foo, I just want to make sure it's established.

So let's just do that without doing all the tests twice.

public abstract class FooTestBase {
protected abstract Foo getFoo();
@Test public void testBarMethod() {
// Do some tests calling getFoo().
}
}

public class FooMysqlTest extends FooTestBase {
@Before public void setUp() {
// Do stuff
}
@After public void tearDown() {
// Do stuff
}
@Override protected Foo getFoo() {
return new FooMysql();
}
}

public class FooPostgresqlTest extends FooTestBase {
@Before public void setUp() {
// Do stuff
}
@After public void tearDown() {
// Do stuff
}
@Override protected Foo getFoo() {
return new FooPostgresql();
}
}

Since you use Maven, by default FooTestBase will not be considered as a test class because it doesn't start with Test or ends with Test or TestCase. Therefore your tests will be centralized in FooTestBase and they will be executed in each child class.

Your tests will be written only once, and your setup/teardown will be specific to each implementation.

Just came across this: https://maven.apache.org/guides/mini/guide-attached-tests.html

<dependencies>
<dependency>
...
<type>test-jar</type>
<scope>test</scope>
</dependency>
</dependencies>

This may solve my problem more elegantly; investigating.

Update: it works is it should!

Category:java Views:5 Time:2018-03-13
Tags: java junit maven

Related post

  • Eclipse/Maven: JUnit tests not compiled when running them 2011-05-22

    I am working on a project using Maven and Eclipse (m2eclipse plugin). I've got problems with the JUnit tests: Sometimes, when running them within Eclipse, they wont be compiled, but the old class files are used instead. When I delete the class files,

  • Visual Studio/ASP.NET MVC Project Organization & Structure Links, Examples and Personal Preferences 2009-08-19

    I'm looking for multiple inputs on great project organization/structure, preferably especially for complex ASP.NET MVC projects (which will definitely go beyond the couple of folders initially created by the project type). Project organization also i

  • How to run a Maven JME project in netbeans? 2009-12-02

    I have created a maven project with j2me-archetype using netbeans. It creates a project with a default MIDLet (SampleMIDlet.java). But when I try to run the project it says that No Main Class Found. Can anyone tell that why it is going for Main class

  • Android and JUnit: Create a JUnit Project or a test source folder 2010-03-07

    There is an option to create an Android JUnit Project. As far as I am aware the other way is to create a folder in the same project called test which contains the same package name as the existing project. What are the differences between these two m

  • Problems by import of a multiple modules maven 2 project into eclipse workspace 2010-03-11

    Hey, I was wondering if someone has experienced the same problem as me and can help me. I have a maven project which contains 6 modules. Some of modules are depending on each other. The project is written in Java and builds to jars, wars and aar. I'v

  • TFS Project Organization 2010-04-09

    I'm relatively new to TFS and am wondering other people deal with organising TFS where there are many projects? For example, does anyone know if it's possible to put TFS projects inside folders or do people use prefixes / suffixes? --------------Solu

  • Inject Groovy into existing Maven Java Project 2010-07-17

    I have been maintaining a maven java project for year. Recently, I learned Ruby and asked why haven't these nice features (of Ruby) existed in Java, and I am so happy to find Groovy the answer. It's already out there for more than 6 years and what a

  • Start a Maven J2Me project in Eclipse and run it 2010-07-17

    I have a problem setting up my maven j2me project that I created in Eclipse. I have the WTK 2.5.2 & SDK 3.0 installed and associated with Eclipse. I created a project using j2me-simple archetype. Copied the files from my previous J2Me project whi

  • Maven PHP project compile = "Execution of php failed. Maybe the php.exe is not in path" 2010-08-02

    Trying to create a Maven PHP project (just test) for Sonar Generated empty project with mvn archetype:generate -DarchetypeGroupId=org.phpmaven -DarchetypeArtifactId=php5-web-archetype -DarchetypeVersion=1.0 -DgroupId=org.sample -Dartifact=my-app Then

  • Exporting eclipse maven project as a non-maven eclipse project with dependencies 2010-08-18

    Is it possible to export my maven Java project on Eclipse to users that do not have maven or the eclipse maven plugin? The first question that comes to my mind is how to force all the dependencies to be included with the export. I want to make it eas

  • Where do I put weblogic-application.xml in my Maven 2 project? 2010-08-31

    Where do I put weblogic-application.xml in my Maven 2 project so that Maven places it in META_INF in the target EAR artifact? --------------Solutions------------- The weblogic-application.xml should be packaged in the META-INF directory of your final

  • Pull dependencies of modules by referencing parent in maven java project? 2010-11-10

    I have a maven-java project (say Project A) with a parent defining modules in its pom. I also have an external project (say Project B) that requires dependencies of two of the modules from Project A. For now, i have defined the dependency to pull eac

  • How to compile maven/flex project without any *.mxml file? 2010-12-13

    Is there any way how to compile maven/flex project which does not contain any *.mxml? The flex project contains ActionScript classes only (i.e. "src/flex" directory contains *.as files only). My pom.xml is here: <groupId>com.test</groupId

  • Converting a Netbeans project to a Maven enabled project 2011-03-31

    How can I transition a Netbeans generated project into accepting a Maven configuration? There are options to create Maven based projects, but there is nothing (that I've found so far) to add Maven dependencies to existing projects. --------------Solu

  • What are the conventional locations for for JSPs, JavaScript, CSS, Images in Maven web projects? 2011-04-04

    I need to convert a traditional J2EE web application into a new Maven web project. In traditional project JSPs are under WebApp/jsps folder, JavaScript & CSS files under WebApp/scripts folder, image under WebApp/images folder, .properties files u

  • Maven/AJDT project in Eclipse 2011-06-29

    I need to use aspectj in a maven project. I installed the maven plugin for eclipse (m2e), the maven aspectj plugin, and also AJDT for Eclipse. So now, when i open a new project i have "Maven Project" and "AspectJ Project". how can i make a new projec

  • How to use Jetty in a Netbeans Maven Web Project? 2011-08-09

    I'm trying to use Jetty in a Netbeans Maven Web Project (instead of Glassfish or Tomcat). I've managed to get running and debugging to work as follows: Add the plugin to pom.xml: <plugin> <groupId>org.mortbay.jetty</groupId> <art

  • Maven 2 project error icon in Eclipse 2011-08-23

    I have create multi-module maven project in Eclipse IDE (already installed M2Eclipse plugin). There I can build my project successfully. But after build also it shows errors icon in my every module project. What kind of issue it can be? Thank You. --

  • Maven Update Project Configuration issue 2011-09-09

    Whenever I perform a Maven Update Project Configuration, I loose ay of the projects I have listed in eclipse under the Java Build Path/Projects. Is there a plugin or something that I need to add to the pom.xml to make it reconfigure that whenever I u

  • maven ejb project - package with dependencies 2011-09-21

    Is there a way to pack the dependencies of a maven ejb project in with the final jar? I typically use a separate ear project and include the ejb as a dependency - it will fill out the lib folder automatically this way. However, this seems a bit waste

  • Error when importing Maven-GWT project ("No marketplace entries found to handle gwt-maven-plugin") 2011-10-27

    I'm using Eclipse Indigo on Win XP, Maven 3.0.3 and GWT 2.4. I created a Maven-GWT project using the Mavne gwt archetype. Then I opened Eclipse, went to File -> Import, selected Existing Maven Projects, chose the pom for my GWT-Maven project, and

  • Maven Archetypes & Project Structure 2011-11-04

    Maven newmbie here. Been looking over the docs at the official Maven site and am trying to understand the difference between a Maven archetype and the actual directory structure of a Maven-based project. Does an archetype dictate the project director

  • displaytag.properties location in a maven web project 2012-01-16

    In a maven web project (developed with Eclipse on JBoss), I have put the file displaytag.properties paging.banner.placement=both in the following three locations: /myapp/src/main/java/displaytag.properties /myapp/src/main/resources/displaytag.propert

  • Removing java source folders for a Maven reactor project in Eclipse 2012-01-27

    When checking out a reactor project with multiple projects m2eclipse insists in creating the /src/{main,test}/{java,resources} folders even though the projects' packaging type is pom. How can I instruct m2eclipse to not create source folders for pom

  • maven sub projects - should they have their own depedencies 2012-04-11

    If a project has many sub-projects, and all the sub projects have a common parent pom.xml, shouldn't all the the dependencies be listed in parent pom.xml ? What's the point of allowing sub project to have their own dependencies.. ? It only opens up t

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.093 (s). 11 q(s)