Cannot use JUnitCore - java

I'm pretty new to java and unit testing and I'm experimenting with junit and so I watch tutorials and read blogs about it. From time to time there are things like PrimerClasses and TestSuites and everyone seems to have no problem using these kind of functionalities.
So when I try to use these too, my project cannot find JUnitCore or even the runner, where all that pretty stuff comes from and up to now I was not able to work out what the reason might be.

Have you added dependency of JUnit in your application?
If you are unable to find the classes then you should add proper dependencies.
If you have created the maven project, then add below lines in your pom file.
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
Or download the jar and add it to you project classpath.

Related

There's Lombok annotation #Slf4j which won't work

I need your help. I have a problem with Lombock annotation #Slf4j. When I try to use it I don't have ability to use its functionality:
There's my build.gradle file:
I've already installed Lombock into my IntelliJ Idea and enabled Annotating processing, but it haven't got me resul yet.
Can you make some advice? I would be very grateful!
Taking a wild stab here, but lombok doesn't include slf4j. It really can't do that1. All features in the extern packages work like this, and it is why the lombok.extern package layer exists: To show you that it's a lombok feature that makes some non-core ('external') dependency work nicer, not that the lombok feature includes this dependency or replaces it.
Thus, all you would need to fix this is to add slf4j to your dependencies:
compile 'org.slf4j:slf4j:1.7.31'
omr something along those lines. Note that SLF4j also needs runtime configuration (slf4j itself is just a 'frontend' that lets you write log statements that go to whereever your configuration says they go at runtime. This 'configuration' includes the code to actually do stuff with these logs. Slf4j tutorials will cover all this.
[1] Adding the deps automatically is not really possible; lombok ships with a number of features that are specifically to make some library / framework easier to use, we (DISCLAIMER: I do quite a bit of work on lombok) can't ship them all of those as deps of lombok, and we haven't (and probably would never) try to hack into your build system to make some sort of semi-dependent system where we detect you use e.g. #Slf4j and somehow make your build system include it only then.
There is some IDE build issue you have.
Why don't you adopt Maven (or Gradle) build tool, that is used in maybe 80% (gradle is maybe other 19.99%) team projects.
And then there is nothing specific about using Lombock
https://projectlombok.org/setup/maven
just
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
<scope>provided</scope>
</dependency>
</dependencies>
And it works in any IDEA or most IDE
P.S. Specifically no configuration/plugin in IDEA since v2020.3
https://projectlombok.org/setup/intellij
P.P.S. If you use maven/gradle and have any issues in any IDE,
try first to make sure, that there is no issues with build itself by running in command line
mvn package
or
gradle build
And then if build is OK, try to resolve specific IDE issue.
Think like "what most people do or won't do to make it work"
For example you can install one more the newest IDEA instance, e.g. IDEA CE (that it undervalued, as it is free) and open your project there: it should just work without any dancing.

How to include javax.jms.* in Intellij?

I can't seem to get this to work in a regular Java project. Does this only work with Maven?
I'm trying to implement the Message interface for a class like so...
public class Test implements Message {
}
I include the following import statement at the top of my code:
javax.jms.*
My project structure
Would I include something like the following in one of the XML files?
<dependency>
<groupId>javax.jms</groupId>
<artifactId>jms</artifactId>
<version>1.1</version>
</dependency>
If you are not in a Maven project, you have to tell IntelliJ that your code needs the jms library.
To do so, open your project/module settings and under libraries add it.
Depending on what you want to do with this project, consider using Maven. Unless this is a "toy project", chances are high that you'll benefit from a proper dependency manager as Maven.

Maven - "Package from transitive module dependency referenced, declare a direct dependency to fix."

I never worked with Maven before, but after having a lot of problems with mockito dependencies in my Netbeans module, I decided to create a new one using Maven and move everything to this new project. After some time doing so, I managed to get the module running just ok.
The problem is that when I use some functionalities that require the Netbeans API I get an exception.
My guess is that the problem is in the dependencies declared in pom.xml. In some of the imports that I use in my application, I get the following message:
Package from transitive module dependency referenced, declare a
direct dependency to fix.
When I clean and build or run mvn clean install in the root directory I get this error:
Project uses classes from transitive module
org.netbeans.api:org-netbeans-modules-projectapi:jar:RELEASE73 which
will not be accessible at runtime.
To fix the problem, add this module as direct dependency. For OSGi bundles that are supposed to be wrapped in NetBeans modules, use the
useOSGiDependencies=false parameter
In the org.codehaus.mojo plugin declaration (which was the only one that had the useOSGiDependencies tag) I tried to change useOSGiDependencies=true to useOSGiDependencies=false but that didn't work. I also tried to change RELEASE73 to RELEASE82, since I am using Netbeans 8.2 but that didn't work as well. I also tried a bunch of other possible solutions that I found on google, but none seemed to work for me.
These are the Netbeans api dependencies that I have in my pom.xml file:
<dependencies>
<dependency>
<groupId>org.netbeans.api</groupId>
<artifactId>org-netbeans-modules-project-libraries</artifactId>
<version>RELEASE73</version>
</dependency>
<dependency>
<groupId>org.netbeans.api</groupId>
<artifactId>org-netbeans-api-annotations-common</artifactId>
<version>RELEASE82</version>
</dependency>
<dependency>
<groupId>org.netbeans.api</groupId>
<artifactId>org-openide-util</artifactId>
<version>RELEASE82</version>
</dependency>
<dependency>
<groupId>org.netbeans.api</groupId>
<artifactId>org-openide-awt</artifactId>
<version>RELEASE82</version>
</dependency>
<dependency>
<groupId>org.netbeans.modules</groupId>
<artifactId>org-netbeans-core</artifactId>
<version>RELEASE73</version>
</dependency>
<!-- ... -->
</dependencies>
I looked everywhere but couldn't find a straightforward answer on how to declare a direct dependency.
Can anyone explain to me what am I doing wrong here?
After digging a bit more, I found that I had to add all the dependencies manually. This answer helped me a lot.
I just lack experience with maven. Basically I googled org-netbeans-modules-projectapi:jar:RELEASE73 maven entered the first link and added the needed dependency to my project.

JUnit5: Trouble Importing Assertions

I'm trying to use JUnit5 to create some basic unit tests. I go to my Analyzer.java class and get the popup for creating a test. I hit Create New Test, setting Testing Library to JUnit5. I check off a bunch of methods to generate test methods for and hit OK.
So now I have an AnalyzerTest.java file and at the top I have:
import static org.junit.jupiter.api.Assertions.*;
Unfortunately, Assertions is red (this is in IntelliJ IDEA). When I hover, it says "Cannot find symbol Assertions". In a similar vein, I have:
#org.junit.jupiter.api.Test
before each test method and when I hover, I get "Cannot resolve symbol Test"
I simply want to create and then run some unit tests but obviously am doing something wrong.
Any ideas?
Thanks!
Gradle
Add the following dependency to your Gradle:
testImplementation("org.junit.jupiter:junit-jupiter-api:5.0.1")
Under your dependencies.
dependencies {
testImplementation("org.junit.jupiter:junit-jupiter-api:5.0.1")
I don't know you are using or maven or gradle. But if you are using maven just add below dependecies in between your tags. Let me know if you need more help regarding this. I have used older version below, you can check the latest version from https://mvnrepository.com and update the pom script.
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-engine -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.4.0</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.4.0</version>
<scope>test</scope>
</dependency>
If you use IntelliJ-IDEA, make sure your test file is in the Test Sources roots.
Because my project do not have the path src/test/java, when I use Ctrl+Shift+T to add a test file, it added in src/main/java...
See intellij support post
Maven
If using Maven, be sure to specify a dependency element inside the dependencies element.
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.4.0-RC1</version>
<scope>test</scope>
</dependency>
If you want to use those Assertions outside of your test-related classes, in your regular app classes, drop the <scope>test</scope> element.
Note that as of 5.4.0 of JUnit, we can specify the new single Maven artifact of junitjupiter which in turn will supply 8 libraries to your project. Very convenient if you are writing only JUnit 5 tests (Jupiter test engine), and not “vintage” JUnit 4 tests or other test engines.
If using Gradle rather than Maven, see the Answer by RileyManda.

Getting Spring (and other libraries) in a Maven world

I am following an online tutorial, and am stuck trying to get Spring libraries to run things with. The instructions are either a little out of date or assume I know how to do things in a Maven world that I do not.
They give web addresses on the Spring site from which they say I can "download the jars". But you can't download jars from any of them; evidently what you can do is find pom.xml fragments and use them to download jars. If you know how.
For example: the tutorial says:
Finally, following is the list of Spring and other libraries to be
included in your web application. You simply drag these files and drop
them in WebContent/WEB-INF/lib folder.
commons-logging-x.y.z.jar
org.springframework.asm-x.y.z.jar
org.springframework.beans-x.y.z.jar
org.springframework.context-x.y.z.jar
org.springframework.core-x.y.z.jar
org.springframework.expression-x.y.z.jar
org.springframework.web.servlet-x.y.z.jar
org.springframework.web-x.y.z.jar
spring-web.jar
I love the "simply" here.
I have many but not all of these (version 3.2.4). I have googled the library names and used jarfinder for the ones I don't have, but haven't found them all. It seems to me I'm flailing around a bit, just trying this and that until something works. I hate that.
I'm hoping someone can give me a set of steps I should follow when faced with this sort of thing, since we're faced with it all the time. Do I need to stop my current study of programming and learn Maven inside and out so that I can configure the bloody system so I can program again?
Is it a maven project you have?
If so in the pom.xml you just need to place the “pom fragments” in the xml.
i.e. between the dependencies tags. For example,
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
</dependency>
</dependencies>
Once you have done that run maven install. Under Eclipse it is: run as > maven install.
That will download all the dependences i.e. jars you have listed into the you local maven repository.
I think the default is C:\Users\yourName\.m2\repository
Found most of the needed jar files here:
http://www.java2s.com/Code/Jar/CatalogJar.htm
while I downloaded commons-logging here:
http://mvnrepository.com/artifact/commons-logging/commons-logging/1.2
My advice is, stop study you program language, Java I guess and learn how to integrate maven dependencies of Spring in your Maven project.
http://spring.io/blog/2009/12/02/obtaining-spring-3-artifacts-with-maven/
You cannot start the house from the roof

Categories