Testing Junit Eclipse in the same project

This is a relatively open question. If I created an application in a project in Eclipse and then I want to test this project, should I create JUnit code in the same project or create a separate project. For example...

ShopSystem possibly the name of my main project - should I create a project with the name say, ShopSystemTest ?

In general - how far away should the test code be saved from the main project folder? If I save the test code in the main project, and then export the main project as an executable jar, it will take a test code with it, which is not perfect ...

Suggestions?

+48
java eclipse junit
Aug 26 2018-10-10T00:
source share
4 answers

Although there isn’t the only right way, the usual approach is to keep unit tests in one project.

You can create a second source folder (for example, test ), where you put the test classes in the same packages as the tested classes. It also allows you to test private class classes without populating your source packages with test classes.

Your source folder / package structure will look like this:

 -sources -main -my.package -MyClass.java -test -my.package -MyClassTest.java 

You can then customize your build to not include the original test folder when packing the JAR.

+59
Aug 26 '10 at 9:41
source share

I like the maven convention: there is a separate source tree for the main and the test in one project, the main code is deployed, the test code is not. Package structures can be (but not necessarily) identical.

 project src main java // source files resources // xml, properties etc test java // source files resources // xml, properties etc 

And in eclipse, when you select new -> JUnit test case , you just change the source folder to src / test / java and leave the proposed package as it is.

(One of the benefits of the rest in the same package is access to protected and restricted element packages, although this is not the β€œcorrect” unit test behavior)




Update: Here is the code illustrating 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; } 

Testing 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"); } } 
+19
Aug 26 2018-10-10T00:
source share

Usually you have -

 /src/main/java (for codes) /src/test/java (for tests) 
+4
Aug 26 2018-10-10T00:
source share

Consider the maven path: in the maven project, crises are thus organized.

 src |--main | |--java |--test |--java 

The source code is sent to src / main / java, the junit test code goes to src / test / java, both of them are the source folder (and, as a result, you can put your jUnit code in the same package as your Java code, but in another source folder).

An interesting fact is that for normal coding, your jUnit classes are in code packages, but when creating a jar, you can use classes that come only from src / main / java and not let out tests.

+3
Aug 26 2018-10-10T00:
source share



All Articles