I just shifted my project form Netbeans to intelliJ IDEA, its a junit based test project. In netbeans I was using statments
assertTrue("Message", conditionCustom());
and it was working without any extra import. Now when using the same above command in intelliJ I have to import file
import static org.junit.Assert.assertTrue;
is there any way so I dont need to write the above line in my code file? otherwise I have to edit all my files to get working assertTrue statement.
You either have to add the static import OR make clear what class that static call is associated with:
Assert.assertTrue("Message", conditionCustom());
I usually use the latter because I think it's clearer.
Java won't compile unless it can figure out which class to associate that static method with.
I'd guess that perhaps you use inheritance to associate that static method with your test.
Related
what I mean by making a package default is including the java package into the class mainstream just like java.lang
suppose I Have a package utility.* I want this package to be imported by default in any class I create.
Hence, whenever I'd create a class, I wouldn't have to write the import statement at all, as we don't have to in the case of java.lang. Is there a way to achieve this?
Only java.lang is auto-imported this way and there's no way to add other packages to this list (outside of building your own version of Java, which is definitely a bad idea).
However, most IDEs allow you to define "favorite" packages that will be searched first for suggestions when writing a class name that hasn't been imported there and adding your package to that list has a very similar effect, since the IDE will just add the import statement for you.
I'm Using Netbean for Java Assignment (for School) and doing Unit Test (JUnit) yesterday.
When i added New > JUnit Class, it used to open Java Class starts with ...
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;
public class UtilsClass {
...
}
But Today When i add New > JUnit Class. it opens Java Class starts with
import junit.framework.TestCase;
public class NewEmptyJUnitTest1 extends TestCase {
.....
}
i know both are Junit Class but i really want back the 1st one as i'm used to it. i have done complete uninstallation of Netbean and reinstall it.
Ok it's an old question but since I had the same problem right now, I think it is worth an answer. Maybe it is also useful for others.
I observed that if I create a new plain project in NetBeans, create a class and create a test class from it by right clicking the class -> Tools -> Create/Update Test, a JUnit4 test class is created. This leads me to the following solution:
Ensure that you have the correct library included in your project's TestLibraries folder, if not right click the TestLibraries folder, remove the JUnit3 library and replace it with the JUnit4 library, which you can download here: https://junit.org/junit4/.
(note that JUnit4.12 has a dependency on the hamcrest 1.3 library, anyway both libraries are already present in the NetBeans 8.2 installation)
Asuming that you already had the correct libraries included and the problem still exists, as it was for me. You can check the NetBeans project properties file <NetBeans-Project-Location>/nbproject/project.properties. Within this file you may find the property junit.selected.version=3 you can eigther change this to 4 or delete the whole line.
Save the file. If you now create a test class the JUnit4 tamplate will be picked.
Enjoy writing your JUnit4 tests. ;)
Is there a way to customize the default imports in Eclipse?
For example if I open a new JUnit Test Class by default I get these imports:
import static org.junit.Assert.*;
import org.junit.Test;
What I'd like to get:
import static org.junit.Assert.*;
import org.junit.Test;
import static org.hamcrest.Matchers.*;
Unfortunately, Eclipse is quite lacking in the customizability of code generation when refactoring and creating new entities.
You might want to check out Eclipse Optimize Imports to Include Static Imports for information how to make content assist find static methods in predefined classes. That might be what you actually want. In the accepted answer Joey Gibson writes that you can add org.hamcrest.Matchers to Window » Preferences » Java » Editor » Content Assist » Favorites.
Another solution to the specific problem of statically importing Hamcrest methods, is to configure a Code Template named hamcrest instead. That way you can simply type ham and follow up with ctrl + space to get the import at the top.
The template should look like
${staticImport:importStatic('org.hamcrest.Matchers.*')}${cursor}
An even more convenient hack is to add this template to the already existing test code template which generates a new test method. If you change this template to:
#${testType:newType(org.junit.Test)}
public void ${testName}() throws Exception {
${staticImport1:importStatic('org.hamcrest.Matchers.*')}
${staticImport2:importStatic('org.junit.Assert.*')}${cursor}
}
and use this each time you make a new test method you will never have to care about adding the hamcrest import manually again.
Image to show where you configure it:
The closest preference I can find is the one under Window --> Preferences --> Java --> Code Templates. Expand the Code section and select the New Java files option to view the pattern for newly created Java files. You can then click Edit to add the import, for instance:
${filecomment}
${package_declaration}
import org.hamcrest.*;
${typecomment}
${type_declaration}
In all cases you still need to write the code that uses the org.hamcrest package. Alternatively, just organize the imports by pressing Ctrl+Shift+O after adding the code that uses the package.
I recommend you to add org.hamcrest.Matchers.* into “Favorites” (Window -> Preferences -> Java -> Editor -> Content Assist -> Favorites).
This way content assist will propose static members even if the import is missing and add corresponding import when you use the member.
It means you can type the method/field you’d like to use and let content assist add the import automatically.
Organize Imports
Modern IDEs offer a feature called Organize Imports. Using this feature you can no longer be worried about those import statements, the IDE itself manage those imports.
How should you do
As you write your codes, whenever you want to make the IDE to organize your imports, you should just press its shortcut keys.
Keyboard: Ctrl+Shift+O
Menu: Source → Organize Imports
How it works
IDE search through your codes and lookup each class and add their corresponding import statements. Also unused imported classes will be removed.
In Netbeans you can navigate through this , Tools-->templates-->java Folder--> you can give what you need to want while opening a page Example :there will be Java class,interface,enum,exception etc
I have two .class files that I'm supposed to black box test. These are in a package one.two.three. My tests are also in the same package. There is a third .class file in the same package whose purpose is to hold an enum variable for the Orders class I'm supposed to test. In eclipse, I'm able to get the junit tests for Orders to work by importing the enum directly e.g.
import one.two.three.Orders.ShippingMethod;
If I try to do this using Ant or via the command line, I get the error "package one.two.three.Orders does not exist". If I change the import statement to
import one.two.three.*;
Ant, Eclipse, and the terminal cannot find any of the classes I have. I need to compile and run the test cases with Ant. The classes are in bin/one/two/three Any help would be greatly appreciated, thanks.
Import Orders, as it is the class, and assuming that ShippingMethod is an enum within that class, the correct way to reference its type is Orders.ShippingMethod.
Attempting to import a class's internal types sometimes works oddly in Eclipse. This is likely due to Eclipse not using the javac compiler packaged in your jdk, while Ant does (it has to, because Ant doesn't ship an embedded compiler).
import one.two.three.Orders;
public class Whatever {
private Orders.ShippingMethod shipMethod;
}
This should work in everything, as it's the right way to do it.
import one.two.three.Orders.ShippingMethod;
could easily confuse most compilers as there is not a
one/two/three/Orders/ShippingMethod.class
file, which means the class loader won't find it at runtime.
I'll bet it's a bug in the Eclipse embedded compiler, as I've seen quite a few. On the bright side, the Eclipse embedded compiler exists to provide faster, tighter integration between code editing and Eclipse. On the dark side, that means that sometimes the Eclipse compiler and the javac compiler differ. When in doubt, the javac compiler is probably correct.
You'll need to set the classpath.
I don't know exactly on Eclipse (I use NetBeans), but I click on Libraries -> add JAR/Folder.
For command line, you need to specify class path
java -cp path/to/my/files (...)
Trying to import a class I made in Dr. Java. I made a simple class called QuestionObject which has String questionString and an array for answers and then an int which corresponds to the correct answer and corresponding getters, setters, and constructors. Compiles fine. In my other class file, the one with my main method and such called Game, when I try import QuestionObject; I get a 'cannot resolve' error in the compiler. I saved the class in the same folder as Game.java. Doing everything in OS X not using command line.
Help please! There's probably a simple answer, I just can't find it!
Don't import the class if both classes are in the same directory and in the default package!
Try removing the import statement.
Try setting the package of both classes. And make sure that both classes are in the class path. I am not familiar with Dr Java but with the command line compiler javac *.java in the directory with the classes should pickup both classes.