score:71
While there is no only right way, the usual approach is to keep unit tests in the same project.
You can create a second source folder (like test
), where you put your test classes into the same packages as the classes under test. This also allows you to test package-private classes while not flooding your main source packages with test classes.
Your source folder/package structure would then look like this:
-sources
-main
-my.package
-MyClass.java
-test
-my.package
-MyClassTest.java
You can then configure your build to not include the test
source folder when packing the JAR.
score:4
Consider the maven way : In a maven project, soruces are organized this way
src
|--main
| |--java
|--test
|--java
Your source code goes in src/main/java, your junit test code goes in src/test/java, they both are source folder (and as a consequence you can put your jUnit code in the same package as your Java code, but in a different source folder).
The interest is that for usual coding, your jUnit classes are in code packages, but on jar creation, you can take classes coming only from src/main/java and not release your tests.
score:5
Typically you have -
/src/main/java (for codes)
/src/test/java (for tests)
score:22
I like the maven convention a lot: There is a separate source tree for main and test in the same project, main code gets deployed, test code doesn't. Package structures can be (but don't have to be) identical.
project
src
main
java // source files
resources // xml, properties etc
test
java // source files
resources // xml, properties etc
And in eclipse, when you choose new -> JUnit test case
, you just change the source folder to src/test/java and leave the suggested package as is.
(One of the benefits of remaining in the same package is having access to protected and package scoped members, although this is not 'proper' unit test behavior)
Update: Here's some code to illustrate my last point:
Main class (in src/main/java):
package com.test;
public class Foo{
static class Phleem{
public Phleem(final String stupidParameter){
}
}
String bar;
protected String baz;
protected Object thingy;
}
Test class (in src/test/java):
package com.test;
import org.junit.Test;
public class FooTest{
@Test
public void testFoo(){
final Foo foo = new Foo();
foo.bar = "I can access default-scoped members";
foo.baz = "And protected members, too";
foo.thingy = new Foo.Phleem("And I can access default-scoped classes");
}
}
Source: stackoverflow.com
Related Query
- Eclipse junit testing in the same project
- Using both Eclipse and NetBeans on the same project
- Kotlin And Java In The Same Project Using Eclipse IDE
- "The project: swing which is referenced by the classpath, does not exist" in Eclipse when project name is the same as workspace name
- IntelliJ's IDEA and Eclipse on the same project
- How to use the same source from PhoneGap Build and PhoneGap Android project in Eclipse
- Multiple versions of the same multi-modules maven project under eclipse
- Creating a new C++ Project in Eclipse CDT with the same settings as another project
- How do I get my Maven Junit test to print out the same stack trace as what Eclipse JUnit generates?
- Is it possible to have multiple people work on the same Eclipse project at once? (Like Google Docs)
- Combine Eclipse PDT and Aptanta plugin w/ JQuery in the SAME project
- Eclipse how to reference file in a different src under the same project
- Specific JRE name (which might not be the same on other computers) stored in meta-data for Eclipse Ganymede WTP project
- Run JUnit test for all the sub directories having different versions of the same project
- Is there a plugin for Eclipse that allows two people to edit the same project at the same time?
- How to use different versions of Eclipse in the same project and the same source base
- Eclipse opens .class instead of .java when opening declaration in the same project
- How do I get my Eclipse JUnit test run to behave the same as running the test on the mvn command line?
- How to open a eclipse java and a c++ project point to the same root directory
- Most rational and painless way to work on the same Eclipse Android project on two different PCs
- AVD + Eclipse : always runs the same project
- Eclipse shortcut to always run the same project
- Getting error while importing a class defined in the same project in eclipse
- Eclipse - Running the same project twice
- How can I have 2 files of the same name in an eclipse project but in 2 different sub-folders?
- How do I make running a JUnit test in eclipse behave the same as running the Maven test on the command line?
- maven and eclipse builds differently the same project
- Eclipse Java Remote Debug: How do I ensure that the code I've got in my project is the same as the code running in the JVM?
- Deleting Java project and then creating the same one in the same directory - Eclipse
- Is there a way to make Eclipse open a hyperlinked java file from another project from the same workspace
More Query from same tag
- Eclipse Kepler making multiple SSH connections
- Connecting blobs with opencv 2.4 in java
- Trying to make a Fibonacci Android App
- 0 NATIVE_LIBRARY_NAME cannot be resolved or is not a field with Linux + Eclipse + Java
- Linking one xml to another xml using ImageButton in Eclipse ADT?
- R not installing correctly in Eclipse ... Java issues?
- Mercurial Eclipse Error
- Github EGit and Netbeans git: push not working
- How can I install my Eclipse plug-in from the Eclipse Marketplace?
- Changing application name and packages
- App Engine Connected Android Project - Could not find ...RequestFactorySource
- Error 401 Authorization required when pushing to github repository
- How to quickly replace a Maven Dependency with an Eclipse project?
- Transparent SWT Canvas
- Maven dependencies for hadoop and yarn
- Start another Activity
- What is different in different user accounts when doing a maven build
- Using eclipse PHP manual feature using drupal .inc
- Where does the java.util.logging log file or output go?
- List view opens the same activity
- bootclasspath in tomcat
- Deploy WAR file in Tomcat, Issue after Deployment
- Using Eclipse ant plugin from command line?
- Eclipse, bundle projects and run them separately
- Encoding problems running jsp web pages on Apache Tomcat
- Is there scala plugin compatible with Spring Source Tools Suit?
- How to configure Eclipse C project to accept C++ includes
- Eclipse IDE search definition error (Progress View)
- How to write plugins for Aptana Studio 3
- Oracle CEP Tools Eclipse plugin - missing requirements