JUnit4 in Eclipse - java

I'm trying to run some JUnit test units in Eclipse 3.5, but with no luck at all.
JUnit3 works fine.
When I create the JUnit4 Test unit, Eclipse offers to add the JUnit library to the class-path. I accept, but when I check to see if it was added in the project's properties panel, I can see JUnit4 was added, but no JARS where included.
If I choose Edit, the combo says "JUnit 4", and just below "Current location: Not Found".
When I launch a JUnit4 test, I get an error saying and internal error occurred, NullPointerException.
I've read for about two days now, and all references say eclipse INCLUDES JUnit4, but somehow, there seems to be something I'm missing.
I've tried re-creating my projects and creating a test in brand new ones with no luck.
package test;
import static org.junit.Assert.*;
import org.junit.Test;
public class AuthServiceTest {
#Test
public final void testValidateCredentials() {
fail("Not yet implemented"); // TODO
}
}
[Edit]
I've added junit-4.8.1.jar to the project's classpath, and eclipse's classpath, but still the same problem.
[Edit2]
I also added junit-dep-4.8.1.jar, since I'm not sure if these dependencies are necessary, but no change.

Right click on the project name .
Built Path--> Add Libraries
Select the appropriate library from the list (JUnit 4 in this instance) .

In my Eclipse installation JUnit 4 is provided, it's in plugins\org.junit4_4.3.1\junit.jar
If you can't find it, then I guess that you may need to download it.
You can associate your JUnit with the Eclipse JUnit settings in
Windows->Preferences->Java->Build Path->User Libraries
Select JUnit there, and you can add and edit JARs.

It appears that the Eclipse 3.5 from Fedora's repository doesn't include JUnit, and installing the appropriate package didn't include it either.
Another Debian PC presented the same issue. A clean download from eclipse.org solved the problem.
I will report this bug at some point :)

Related

How do I resolve "the type is not accessible" error in Eclipse when using an external JAR? JAR in question is algs4 from Princeton [duplicate]

I'm new to the whole programming stuff but here's my problem:
I used to add my JUnit test cases in Eclipse by right clicking on the project, and just add New > JUnit Test Case.
Currently, I am not able to implement any test methods because Eclipse tells me on the line
import static org.junit.jupiter.api.Assertions.*;
the error message
The type org.junit.jupiter.api.Assertions is not accessible.
Error I get in the IDE:
I tried the following:
Reinstalling Eclipse, using a fresh workplace.
Adding the JUnit to Build path
Nothing helped.
It worked and works in older projects just fine.
Here is how the Package Explorer looks:
What am I missing?
You use the Java Platform Module System (JPMS) by having a module-info.java file in the default package probably without the required requires <module>; statement. JPMS was introduced in Java 9.
Do one of the following:
Delete the module-info.java file (if needed, you can recreate it via right-clicking the project folder and choosing Configure > Create module-info.java)
In module-info.java add the corresponding requires statement, e.g. by going to the line with the import statement and using the corresponding Quick Fix (Ctrl+1)

Cannot find test class in project - "The input type of the launch configuration does not exist"

I am working with TestContainers for my project but when I am running the test in eclipse, I am having the following error with me
On top of this window, it also gives a specific error which says Cannot find test class "DBInitTest" in project "myProject".
package org.ft.cdcp;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.*;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.utility.DockerImageName;
import org.testcontainers.junit.jupiter.Testcontainers;
#Testcontainers
public class DBInitTest {
private final PostgreSQLContainer sqlContainer = new PostgreSQLContainer(DockerImageName.parse("postgres:alpine"));
#BeforeEach
public void setUp() {
sqlContainer.start();
}
#Test
#DisplayName("Initializing database container")
public void initTest() {
assertTrue(sqlContainer.isRunning());
}
}
Do note that I have checked and rechecked the dependencies, everything is intact and there's no problem with them, something's buggy only about JUnit5.
I looked it up online but only found something which said that I should Right click on source folder --> Build Path --> Use as source path, but this fix doesn't seem to be working for me.
I am not expert and cannot explain why this fixed any but:-
Today I started having the same problem as you reported.
After fiddling about for a while (reinstalling STS, lombok ...) I eventually clicked on the error entry that popped up. It took me to "Edit lauch configuration properties" for the Test that would not run.
Once there I clicked on the "Search" on the line of the "Test Class:" field.
I selected the correct class from the "Test Selection" window that popped up.
(nb what I thought was the same value that was already there)
UPDATE
Now I feel silly.
I had a test class called "AdminTaskControllerMockMvcTest" in a file called "AdminTaskControllerMockMvcTests.java", ie class name with an "s" on the end.
Renaming my test class to "AdminTaskControllerMockMvcTests" fixed all my issue.
Regards, Andrew
So I figured out what the error was.
My project structure was like this:
└─main_project/
├─ myProject/
└─ other_sub_project/
It might sound stupid but it's definitely something beginners tend to often (as I did). I have opened the main_project in my IDE and there were other sub-projects inside that projects too. The DBInitTest was inside the sub-project named myProject.
I was getting the error because of this structure only. So when I closed the project and opened the sub-project directly from the IDE, the error was gone.
Steps to resolve:
Close the currently opened project i.e. main_project
Click on Open Project in your IDE
Navigate to the folder of the sub-project i.e. myProject and open it inside your IDE
Voila, the error is gone now. ;)

Java not processing JUnit jar

I am unable to compile tests with JUnit. When I attempt to do so, I get this error:
package org.junit.jupiter.api does not exist
I get this error compiling the tests even if I put the .jar in the same directory and compile as follows:
javac -cp junit4-4.12.jar Tests.java
The contents of Test.java are:
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
public class Tests {
... several tests ...
It's not clear to me what the issue is, and as far as I can tell, it should work with the .jar -- it's the one from /usr/share/java, where it was installed when I installed junit.
As #DwB has already mentioned you have wrong junit version.
Here is what is jupiter in JUnit: http://junit.org/junit5/docs/current/user-guide/#overview-what-is-junit-5
In simple words JUnit Jupiter API is a set of new classes which were written and introduced in junit 5 version only. And ur trying to use 4 version.
And also i want to clarify some points.
even if I put the .jar in the same directory and compile as follows
It does not matter actually is your file in the same directory or not. Its all about it's path. If you are setting jar only by name of jar file (as you did) then your path becomes relative to your current directory from where u execute javac command. You can just use absolute path and run this command from every directory you want.
https://docs.oracle.com/javase/8/docs/technotes/tools/windows/classpath.html (this one is for windows but for other os there are only minor changes in path writing)
If you get errors like package does not exist, classnotfound or anything similar then such kinds of errors almost always mean you have something wrong with your classpath or dependencies. In your case you simply had wrong version.
Now about finding necessary deps. In java world one of the main places for dependencies is maven central. Almost every opensource library can be found there and maven by default uses this repository to find and load dependencies (in your case these are jars) from there. Also you can use it to get necessary jars manually by simply using it's UI (https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api/5.0.0). There is download jar button.
Now if you know package or class but do not know in what dependency (jar for simplicity) it is located. In this case you can use http://grepcode.com or other resources which allow to search within available source code withit different repositories. In most cases this work. With juniper i did not manage to find smth there but in other cases this may help) Or the most simple case is just google package and in most cases it also will help to define entry point.
Now about solving ur issue. It seems that you will need as api as implentation. You will definitely need this one https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api/5.0.0 but it seems that you will need juniper-engine too. First try adding only API and then just go on adding necessary libraries according to errors. You can add multiple jars to cp (read provided class path guide from oracle).

Unable to run JUnit test in IntelliJ

I wanted to start project using TDD. Created the test directory and later changed it to package that is integrated with src direcotry. In both cases I get the same error:
Class not found: "tests.objectsTest"
I tried different techniques of importing JUnit jar and none solved problem. Also I tried to rename my test class but it gives no solutons whatsoever.
It seems that IntelliJ or JUnit changes name of the test class. Shouldn't it be objectsTest.tests?
I am using JUnit version 4.12 and latest IntelliJ EAP.
This is my project structure:
Project:
-.idea
-src
-logic
-objects
-tests
-test
-test.java
src and tests are directories marked as Source and Test. Every package except test is empty. On my other PC with IntelliJ Community Edition everything works fine but on EAP there is this bug. Unfortunatelly I have to use EAP.
test.java code:
package test;
import org.junit.Test;
public class test {
#Test
public void canCreateInhabitant(){
}
}
Have you checked if you have the JUnit plugin enabled? I (foolishly, shame!) disabled it at some point and was unable to get IDEA to run my tests until I remembered to turn the plugin back on...
Check the root directory of your classes. It must be marked as source (for java classes) or test (for java test classes).
It seems that your directory is not well marked in IntelliJ.
I did a simple test and put it on github.
It's the absolute simplest of tests but it works great, standing inside the test class pressing shift+ctrl+t will run the test.
Go ahead and clone it and try it out.
Easiest way is:
Open Class in Intellij and press Ctrl+Shift+T
Select "Create New Test"
Now, a new pop up will be opened where you can select Unit Test Library (For your case its Junit4)
Select the methods which you want to include in test
And there you go !
Sometimes I find that this happens when I try and launch "All tests" from the project folder in the structure view. Launching all tests by right clicking on the test root folder and selecting "all tests" from there seems to solve it.
Just had this happen to me. When I built via Maven it had a problem. When I fixed the problem, it would run the junits again. Goofy.
I had the same problem, I solved it by clicking File-> Invalidate chaces and Invalidate an Restart

java.lang.NoClassDefFoundError while running JUnit test in Netbeans

I am building an Android hello world application in Netbeans. It's building properly and I am able to run in the emulator also.
But when creating and running the Junit test I get a java.lang.NoClassDefFoundError.
How can I fix this problem?
Check the manifest of your test project, for example HelloWorldTest/AndroidManifest.xml. The android:targetPackage attribute of the instrumentation markup should target the package in your application, for example, com.example.helloworld. Not the test package of your test project (eg. com.example.helloworld.test).
Simply AndroidManifest.xml -> manifest -> package of main application should match AndroidManifest.xml -> manifest -> instrumentation -> android:targetPackage of the test application.
The full error message contains the name of the class, that wasn't found on the classpath. Double check if the classpath that is used for running the test includes all required classes and libraries (your class files, junit.jar, android specific libaries).
I find that a rebuild usually finds the classes (as it is a classpath issue). Netbeans seems to aggressively compile existing tests, but the main source code (from a maven setting at least) is not always available.
I've started running full builds to try address this. There might be a plugin that addresses this but I haven't found it yet.
EDIT: This might help.
I don't know about netbeans specifics, but the problem is probably the same.
FragmentActivity can not be tested via ActivityInstrumentationTestCase2
Your test project can be using a different android compatibility library than your main project, and that causes this weird errors.

Categories