Netbeans and creating JUnit tests - netbeans

I am using Netbeans 7.0.1 with a web project that I have imported from existing sources. I have added the JUnit library to my project. In Netbeans tutorials online for version < 7 it says to create a JUnit test for a given existing class by righ-clicking on the source file in the project, select the menu "Tools" and then there should be an option to create a JUnit test. However this option does not appear for me. There seem to bean old bug report/request for functionality, describing this but it does not seem to have been resolved as it is mentioned as late as 2011-05-09 on the Netbeans bugzilla list (link Related on bugzilla). On the bugzilla it is suggested to explicitly create the folder /test/unit/src in the project, but still after this when I try to create JUnit test for an existing class from rightclick project -> new -> other -> JUnit, I get "no tests root folder was found in the selected project" (I tried changing the foldername to tests as well). Can I explicitly set this location in some configuration file and is there any way of getting the expected functionality allowing me to simply righclick a source file and select "JUnit test" to create a JUnit stub for the selected class ?

If you right click on the project and pick "Properties" you should see "Sources" on the left. Select that and then click "Add Folder..." under the "Test Package Folders" section. This will add a new test directory. If you have an existing one, delete it first (just make sure the test sources are saved somewhere else) and then add the new one.

If you right click on your project and add new Unit Tests-> JUnit Test it will automatically create the path for you.

if src/main/java is missing, netbeans 8 does not let you create test case in src/test/java using the wizard
looks like a bug, still valid in 2015

What kind of project you've made has a large effect on whether there's a test root already or no. In Netbeans 7.1.1, a "POM Project" doesn't create a test root directory as a part of the project build process. However, making a Java Application did make a test root by default.
I tried the directions offered by mamboking and I wasn't able to change the directories in the Sources section of the project properties. Making a new project of type "Java Application" make the correct test root.

Related

IntelliJ Idea Scala files not available in 'New' context menu

I am new to both IntelliJ and Scala. I am attending the course "Functional Programming Principles in Scala" on Coursera. I downloaded the zip file for the sample assignment, which contained a sample Scala project.
I imported the project successfully (I guess) in IntelliJ. However, when I right-click on a package in the project explorer, there are no Scala-related templates. I can only select "New Java class", XML files and some forms.
Does anyone know why this happens and if there is another way to create a new Scala class or object from a template?
Thanks.
Import the project by selecting its build.sbt file - not the project folder, not the eclipse project file. Then everything is imported correctly.
For me it works as following:
Right clicked on src and selected "mark directory as" -> source root.
This may also be a caching issue.
If so, try File then Invalidate Caches / Restart
Yes, you need to make the scala directory a source directory (in Menu / Project structure / Modules ) but I also had to do the following:
Still in Project Structure goto Global libraries.
Select Scala SDK.
Right click the Scala SDK and Add to module...
Now my context menu gives me New / Scala class and so on.
Once you've created a project and sbt has finished initializing, try creating another project. It should show up correctly in the new project, as it worked for me. It seems to be some kind of caching issue though Invalidate Caches/Restart didn't help me. Also, ensure that source directories are marked properly (source folders appear in blue color, test sources are in green).
I had this problem everytime I created a new project with spaces in the name. eg "Hell Wev". Using "HellWev" as a project name seems to work fine
Same issue (and solution) as #cwadeevans.
A project named "Week 1" did not show Scala files in the context menu "New". The solution was to delete the project and create a new one named "Week1".

How do you configure eclipse to work with a project that wasn't created by it (java)

I have been using the text editor gedit and the terminal for my latest project but I find that I have so many classes it would be more useful to be working with a separate directory for my .class and .java files so I can see what is going on more easily.
Unfortunately I have not been able to figure out how to get eclipse to work with a project that has already been made (or even one that was already made on eclipse with another computer!). This was not such a problem with two or three classes but I am getting to have so many that it is really tedious- It seems to want me to build a completely new project or nothing.
EDIT (additional info):
I don't think it will be possible to show the code of the project as I current have 12 classes for it but by the end I may have one or two more...
I know how to make a project in eclipse. What I am not sure how to do it open a directory full of .java files in eclipse that were created using a text editor rather than in an eclipse project and get eclipse to accept it as a project.
Does anybody know how to achieve this task?
You can import your project into eclipse doing the following steps:
Create a new Java Project in Eclipse (By going to File -> New -> Java Project) select a name for the project and you can select finish
You have to import your existing code to eclipse, go to:
File -> Import -> General -> FileSystem
Then browse to the directory where your code is currently, and say you want to import into the project you created in step 1
Afterwards remember to go to the project properties (right click in the project folder while in the navigator view, for example) and make sure the java build path has the right source folders configured.
Fortunately, you can use eclipse for any project previously created. Do you possibly have the code to show us how you are applying to the project in eclipse?
For example, you can create a project and class, then copy over starting from public static void main(String[] args) {
down to the bottom, then import the important stuff above the main class name.

How do I open the GWT samples in eclipse Helios and GWT 2.1.1?

What is the straight-forward way of importing one of the GWT samples in eclipse Helios + GWT 2.1.1. It seems like the content of the zip file doesn't contain any project files for eclipse.
I tried the following:
create a GWT project 'bla'
Delete the src ad war folder and
replace it with the src and war
folder from the sample
refresh the project in eclipse
hit 'run'
[ERROR] Unable to find
'bla/bla.gwt.xml' on
your classpath; could be a typo, or
maybe you forgot to include a
classpath entry for source?
So I go to the run configuration and remove the path for the example projects from the arguments list. I also changed the name of hosted html page to the one in the sample. Hit 'run' and:
[ERROR] Invalid version number "2.0"
passed to external.gwtOnLoad(),
expected "2.1"; your hosted mode
bootstrap file may be out of date;
Now I need an out of date version of GWT to run these samples? What is going on, I just want to look at a sample to learn a new concept.
Reading the readme.txt in the GWT samples directory I came across a solution that works on Eclipse 3.7. Assuming you have ant installed on your machine, execute the following from the sample's directory:
ant eclipse.generate
This will generate both the .classpath and .project files needed to import the sample into Eclipse and run it.
Here the relevant excerpt from README.txt:
-- Option A: Import your project into Eclipse (recommended) --
If you use Eclipse, you can simply import the generated project into
Eclipse. We've tested against Eclipse 3.4 and 3.5. Later versions
will likely also work, earlier versions may not.
If the directory containing this file does not have a .classpath or
.project file, generate them by running 'ant eclipse.generate'
In Eclipse, go to the File menu and choose:
File -> Import... -> Existing Projects into Workspace
Browse to the directory containing this file, select "Mail".
Be sure to uncheck "Copy projects into workspace" if it is checked.
Click Finish. You can now browse the project in Eclipse.
To launch your web app in GWT development mode, go to the Run menu and
choose:
Run -> Open Debug Dialog...
Under Java Application, you should find a launch configuration
named "Mail". Select and click "Debug".
You can now use the built-in debugger to debug your web app in
development mode.
In eclipse 3.7, instead of Run -> Open Debug Dialog..., its Run -> Debug Configurations. I got both the showcase and mail samples running following those commands. Should work for all of the other samples as well.
The examples that come with GWT SDK were built in different ways. Some are Maven builds, others use Ant. Eclipse can handle these common builds directly, but you have to choose the right option.
So, if you want to open the Expenses project in eclipse, you would look in the projects root directory (i.e. ../gwt2.4/samples/Expenses) where you'll find a file called pom.xml. That is a Maven build file. To import it into your Work space select:
File->Import...
Open Maven->Existing Maven Projects, and click on Next > button
Browse your file system til you reach the Expenses folder. Click on it and Select OK.
At this point it will show you the Projects in that directory in the Projects: area of the window...only one in this case: /pom.xml com.google.gwt.sample.expenses:...etc.
click on the checkbox for that project, Next>
Finally, it ask you to map plugins. You'll need to select in the drop down menu under Actions the required plugins (such as m2e).
In other project folders you may find a build.xml file instead. That's an Ant build file. To import that you do something similar, but different (of course!):
File->New->Project...
Open the Java folder and select "Java Project from Existing Ant Buildfile", Next >
Click the Browse button, dig through your directory to the project folder and select the build.xml file, and Open it.
Project name should fill in automatically, just select the declaration to use in the middle window and click Finish.
Since those tutorial only include the src and war directory, the idea remains to:
create a new project following this tutorial
remove any created class part of the example files which are automatically added (see Issue 1547)
Unfortunately the Google Plugin does not allow to created a new Web Application Project without creating template files. The template files are nice for the first try but annoying if you want to start from scratch with a new GWT application.
copy the src and war directory in place within the now empty project
Following your ticket 5847 (No easy and straight-forward way to make examples work in eclipse),
as xo4yhamope comments, you need the right GWT option.
and did you consider the Issue 5038 about the error message:
Unable to find 'xxxx.gwt.xml' on your classpath;
could be a typo, or maybe you forgot to include a classpath entry for source?
This message usually means you attempted to refresh the browser before dev mode has had a chance to finish loading the module.
The uppercase/lowercase confusion happens because the module has been renamed to 'stockwatcher', but dev mode cannot map 'stockwatcher' to "StockWatcher' until it has finished loading the entire module.
So, it's just a matter of waiting a few seconds longer after the Development Mode pane says I should go to http://127.0.0.1:8888/StockWatcher.html?gwt.codesvr=127.0.0.1:9997 ?
That seems a little messed up. It should wait until it's actually ready before telling me it's ok, because users (like me) are going to go there as soon as the UI says it's ready.
The other confusing part is that the server begins serving the host page as soon as that message in the UI comes up. Because this was happening, I assumed that the server was completely up, even though (as it turns out) it wasn't.
Anyway, waiting a few more seconds seems to resolve the "Unable to find 'stockwatcher.gwt.xml' on your classpath" problem. Thanks!
I'm beginning to suspect this is a bug in the samples. I am adding this answer so it can be marked as an answer when I get confirmation about that.
I was trying out some other samples and came across this page. At the top is a little explanation for how to download and import the sample.
Before you begin
The StockWatcher project
This tutorial builds on the GWT
concepts and the StockWatcher
application created in the Build a
Sample GWT Application tutorial.
If you have not completed the Build a
Sample GWT Application tutorial and
are familiar with basic GWT concepts,
you can import the StockWatcher
project as coded to this point.
Download the StockWatcher project.
Unzip the file.
Import the project into Eclipse
From the File menu, select the Import... menu option.
Select the import source General > Existing Projects into
Workspace. Click the Next button.
For the root directory, browse to and select the StockWatcher
directory (from the unzipped file).
Click the Finish button.
If you are using ant, edit the
gwt.sdk property in
StockWatcher/build.xml to point to
where you unzipped GWT.
Now this is what I call straight-forward and easy.
At that point I had already built the StockWatcher in a previous tutorial but I got intrigued by the fact that it was explained exactly as I tried it the first time. So I downloaded the project and it had the correct eclipse project structure. I tried to import it 'et voila' I have the project in eclipse. I runs out of the box (with warnings). That is how expected the samples to be.
This experience leads me into thinking that the sample projects are malformed (as eclipse projects) and need to be updated. Let's hope my issue report leads to results.
-- Option A: Import your project into Eclipse (recommended) --
If you use Eclipse, you can simply import the generated project into Eclipse.
We've tested against Eclipse 3.4 and 3.5. Later versions will likely also
work, earlier versions may not.
If the directory containing this file does not have a .classpath or .project
file, generate them by running 'ant eclipse.generate'
In Eclipse, go to the File menu and choose:
File -> Import... -> Existing Projects into Workspace
Browse to the directory containing this file,
select "Mail".
Be sure to uncheck "Copy projects into workspace" if it is checked.
Click Finish.
You can now browse the project in Eclipse.
Copy from readme.txt supplied by samples. I tried this method, it's OK.
To import GWT showcase(2.5.1) I did following steps:
Create a new Google Web Application Project
New –> Other –> Google –> Web Application Project
Provide the project name “Showcase” and the Package name is com.google.gwt.sample.showcase
Click Finish
Go to eclipse plugin folder and traverse to the gwt
Under that folder go to gwt-2.3.\samples\Showcase
Copy (Ctrl C) all the contents. The contents are
a. src
b. test
c. war
d. build.xml
e. README.txt
In the eclipse, right click on the Showcase project and paste. Overwrite all the files
The files should start copying without any problem
Right click on the Showcase and Run As “Web Application”
You should have your Showcase working like Gem!
For me it worked like a charm ;)
Ref : http://simplestepswebdev.wordpress.com/2011/05/16/import-gwt-samples-into-eclipse/

Eclipse doesn't see my new junit test

I'm using eclipse to run the tests in a single junit(4) test class. The tests in the class all run just fine. Then I add an additional test and run the class through the test running in ecplise again. Only the old tests are run. The new test isn't seen by eclipse. There's no error or anything, it's just as if eclipse is looking at an old version of the test.
If I run the tests using maven, everything works fine. Additionally, after I run the tests in maven, ecplipse can see and run the new test correctly.
Any ideas what's going on? Any ideas how to get ecplipse's test runner to see my new test cases?
I had the same issue. I solved it by doing the following:
Going to Project -> Properties -> Java Build Path
For the source folder src/test/java, the output folder was set to
"Default output folder"
Setting this to the typical Maven target/test-classes directory in your Maven structure
After this, Maven and Eclipse were in sync (as opposed to Eclipse happily running an older version of the tests, from whenever the last Maven compile was).
Maybe you "just" need to create a new Run configuration. Eclipse "remembers" the latest used Run configuration and just repeats it if not told otherwise. To make sure you have a new Run Configuration you can rightclick the test case in the package explorer and choose Run As | Junit Test. Next time you hit play this will be the "remembered" Run configuration etc.
Possibly src/test is not in the Java Build Path.
Solution on Kepler:
Project -> Build Path -> Configure Build Path -> Source -> Add Folder
Then check the box corresponding to test under src
You might find this is likely caused by using Maven to build (Maven usually builds into the 'target' folder), but Eclipse is using a different build folder for its own build process. Simplest way is to go into the target folder under your Eclipse Project (or Bundle if using OSGi) and delete the conflicting subfolders/class-files from under that directory; for me this is my "target" folder. Then get Eclipse to rebuild, and everything should be fine.
Technically, and alternatively, you could just blow away the entire build/target folder if you wanted to, and let Eclipse rebuild everything.
In response to the answer provided by Ryan Dawe, I have found out that Default output folder can be set to only one folder, for all the source folders on build path. So if i changed the output folder to target/test-classes, my src/main/java was also outputting classes there.
You might have written this response for a different older version of eclipse, but as of Mars.2 release, we can only have one default output folder for all source folders.
The best solution i have found so far for this problem is to just include the target/test-classes as a class folder, by going to Project -> Properties -> Java Build Path -> Libraries -> Add Class folder.
It seems that your project wasn't recompiled. Either check Menu:Project/Build Automatically or do it manually as Boris Pavlocic commented.
This seems to be the same issue as junit not using the newest file
The problem seems to be that Eclipse puts the compiled tests in the wrong folder which can be solved by manually specifying where they should end up.
Add "test" in front of your test classes if not there already the #Test annotation isn't always picked up from Eclipse's Junit Test framework.
Here is how i fixed my problem...
right click project and go to Run As -> Run Configurations...
select JUnit -> [project-name] on the the popup that came up
(this configuration of [project-name] was created for me by eclipse
but if not there you can right click JUnit -> New and create it)
goto Classpath tab
highlight User Entries and click Advanced... button
on the Advanced Options popup that came up select Add Folders and click OK
on the Folder Selection popup that came up scroll to your project open up target and select test-classes and click OK
repeat steps 4-6 for the [project-name]/target/classes directory and any other directory needed in your classpath (like properties files used in your tests etc.)
Note: this assumes your project's default output folder for tests is target/test-classes, if it is not then adjust accordingly. Also, make sure you have the right JUnit version selected under the JUnit Run Configuration as well and your src/test/java directory is a source folder to your project, etc. as mentioned by others.
It means that you have created a Test class that you haven´t build yet.
After a build, for example with "gradle build" the Test class will be found by Eclipse too.
In my case I had to make a cleanup before as well.

gwt> importing a sample project

I'm just barely after 2 hours of trying to force it to work and looking for answers online.
How in the world do you import a sample gwt application into your eclipse and make it run?
by the way, I cannot find "projectCreator.cmd" anywhere in my files, where is it suppose to be assumming i've used eclipse plugin updater to d/l gwt 1.7.1?
Well, this is what I ended up doing though it ugly and probably not how it was meant to be:
I create a new application called it "bla" or whatever
then right on project > import... > general\file system... (as in import files into projects)
then I selected the top folder of one of the sample applications, for example ..gwt..samples/Mail
selected all folders and files.
selected option "override without warning\asking"
de-selected option "copy entire folder structure"
that's about it. I went to build.XML to rename the project name to "bla" and then I clicked run as a web application.
good luck.
For GWT 2.4: In every project's root you will find a README.txt. If you follow its directions (involving creation of Eclipse project configuration via Ant) you can import the project easily. To run it I additionally had to configure the project's GWT settings (project's context menu -> Google -> WebToolkit settings).
First, Create an Eclipse project for your source, if you haven't already done so, by selecting File > New > Java Project. Then choose Create project from existing source and set up your project. At this point, your source will be loaded in Eclipse, but the project's build path may not be set up properly, and you may see build errors.
Alternatively, if your application's source tree already contains a .project file, either because you had previously worked on it in Eclipse or because it was generated by a tool like GWT's webAppCreator, you can import the project by going to File > Import > General and selecting Existing Projects into Workspace
To enable Google Web Toolkit, right-click your project and select Google > Web Toolkit Settings. Check the Use Google Web Toolkit box and click OK to apply the change.
Enabling App Engine for your project is similar: right-click your project and select Google > App Engine Settings. Check the Use Google App Engine box and click OK.
Source : https://developers.google.com/eclipse/docs/existingprojects
What I did to start it:
My workspace is at: /Users/ievgennaloiko/Documents/helpdesk/
I have downloaded the gwt to Downloads folder. Extracted it. Navigated from terminal to samples and run
ant eclipse.generate
for each sample project I need to import to Eclipse. Even you can run this command on whole sample project.
Next I've copied the samples folder to /Users/ievgennaloiko/Documents/helpdesk
Started Eclipse.
File import -> Existing proj into ws -> /Users/ievgennaloiko/Documents/helpdesk/samples/DynaTable
Copy project into ws - Unchecked.
Next I had to go to properties of the project Google-WebToolkit->Use web toolkit.
Here are the arguments I run with, check for correspondence:
-remoteUI "${gwt_remote_ui_server_port}:${unique_id}" -logLevel INFO -port
auto - codeServerPort 9997 -war
/Users/ievgennaloiko/Documents/helpdesk/samples/DynaTable/war
com.google.gwt.sample.dynatable.DynaTable