mvn test does not run unit tests in springboot project - java

I have unit tests in my springboot project in addition to the default application test that comes with the project bundle when I create the project from start.spring.io. When I run mvn test from command line, I see that only the default application tests are run but not the unit tests that I have written. However, I can run these tests from IntelliJ. I am using maven version 3.6.2 and maven surefire plugin version 2.22.2. Can someone let me know what I am missing here? Thanks.
Here's my test class:
#RunWith(SpringRunner.class)
#SpringBootTest
public class BranchServiceUnitTest {
#Autowired
private BranchService branchService;
#MockBean
private BranchRepository branchRepository;
#Test
public void testAddNewBranch() {
Branch testBranch = new Branch();
testBranch.setBranchName("TestBranch");
testBranch.setCity("TestCity");
testBranch.setContactNumber("TestContactNumber");
testBranch.setEmailId("TestEmailId");
Mockito.when(branchRepository.save(testBranch)).thenReturn(testBranch);
Branch addedBranch = branchService.addBranch(testBranch);
assertThat(addedBranch.getCity()).isEqualTo("TestCity");
}
#Test
public void findBranchById() {
Branch testBranch = new Branch();
testBranch.setId(1);
testBranch.setBranchName("TestBranch");
testBranch.setCity("TestCity");
testBranch.setContactNumber("TestContactNumber");
testBranch.setEmailId("TestEmailId");
Mockito.when(branchRepository.findById(testBranch.getId())).thenReturn(java.util.Optional.of(testBranch));
Branch foundBranch = branchService.getBranchById(1);
assertThat(foundBranch.getId()).isEqualTo(1);
}
#Test
public void testGetAllBranches() {
Branch testBranch1 = new Branch();
testBranch1.setId(1);
testBranch1.setBranchName("TestBranch");
testBranch1.setCity("TestCity");
testBranch1.setContactNumber("TestContactNumber");
testBranch1.setEmailId("TestEmailId");
Branch testBranch2 = new Branch();
testBranch2.setId(2);
testBranch2.setBranchName("TestBranch");
testBranch2.setCity("TestCity");
testBranch2.setContactNumber("TestContactNumber");
testBranch2.setEmailId("TestEmailId");
List<Branch> branches = Arrays.asList(testBranch1,testBranch2);
Mockito.when(branchRepository.findAll()).thenReturn(branches);
assertThat(branches.size()).isEqualTo(2);
assertThat(branches.get(0).getId()).isEqualTo(1);
}
}
Following is my pom file:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.rkasibha</groupId>
<artifactId>rentabook</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>rentabook</name>
<description>Rent a book service</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.modelmapper</groupId>
<artifactId>modelmapper</artifactId>
<version>2.3.6</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

I think its a weird mix of JUnit 4 and Junit 5 that causes the issue:
Spring boot 2.2.6 (I've used start.spring.io to generate a sample application) uses junit 5.
On the other hand, your test is written with #RunWith which means that it uses junit 4 under the hood.
The dependency:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
also seems suspicious - the spring-boot-starter-test already contains all the required dependencies on JUnit 5, so you don't need this one.
Now in terms of resolution, check out the default test that comes with this sample application (the one you've described in the question). The chances are that it uses JUnit 5 by itself, so you better migrate your test to JUnit 5 and rerun.

This looks spurious.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<includes>
<include>**/*Test*.java</include>
</includes>
</configuration>
</plugin>
What do you hope to gain from using <include>**/*Test*.java</include>? I'm pretty certain the trailing * does not mean zero-or-more characters. It's 1 or more. Documentation
Are there specific classes in your test directory that you want to exclude? If not, I would remove the whole plugin. Surefire is already declared in Maven's implicit parent POM, with sensible defaults that will cover all of your tests. Declaring it yourself is both needlessly verbose and has actively broken something which works out of the box.

If your tests run successfully when ran alone, But not picked up during the
mvn test
Chances are that you might be using an Older Junit package in your tests. Usually the above weird scenario happens when there's a mix up in the Junit version.
If Junit5 is being used, please ensure that the package in imports is
import org.junit.jupiter.api

Related

The package com.google.inject is accessible from more than one module

I'm trying to upgrade an application to Java 11.0.2, from Java 8. So those are my very first steps with Jigsaw modules!
My application uses Guice, and the Assistedinject, and Throwingproviders extensions.
Here's my current module-info.java:
`
module com.example.mymodule {
requires com.google.guice;
requires com.google.guice.extensions.assistedinject;
requires com.google.guice.extensions.throwingproviders;
//...
}
`
The application is based on Maven and when I run mvn package I get no error. But In Eclipse (2018-12), I have this error "`The package com.google.inject is accessible from more than one module":
I tried commenting each of the required module in module-info.java but I clearly need the three of them.
Is there something I can do to remove this error? Or is this an Eclipse bug?
Here's my pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>example</artifactId>
<version>1.0.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.7</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.5.7</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.8.1</version>
</dependency>
<dependency>
<groupId>commons-validator</groupId>
<artifactId>commons-validator</artifactId>
<version>1.6</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-text</artifactId>
<version>1.6</version>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.11.3</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.26</version>
</dependency>
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>4.2.2</version>
</dependency>
<dependency>
<groupId>com.google.inject.extensions</groupId>
<artifactId>guice-throwingproviders</artifactId>
<version>4.2.2</version>
</dependency>
<dependency>
<groupId>com.google.inject.extensions</groupId>
<artifactId>guice-assistedinject</artifactId>
<version>4.2.2</version>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<plugins>
</plugins>
</build>
</project>
Here is a minimal project to reproduce my error.
And here's a video of the issue (to be watched in 1080P for clarity!).
Eclipse uses its own compiler which is even stricter than javac in following the specs. You are requiring different Modules/Jars in your module-info which are all using the package com.google.inject. This is some kind of a split package situation which is not allowed in the JPMS spec. AFAIK javac only yields an error if there are actual classes in the same packages of different moduls, but the eclipse compiler is even more picky here.
There are some solutions out there for solving split package problems. If you don't find a newer version of the libs where the problem is solved (which unfortunately is not very likely for most dependencies today) you could f.e. merge the modules into one custom modules, but this is not a perfect solution of course.
For more background information on the issue see also
Eclipse can't find XML related classes after switching build path to JDK 10 and https://bugs.openjdk.java.net/browse/JDK-8215739
I can reproduce the error on my machine (same Eclipse version, OpenJDK 11), works fine on Apache NetBeans IDE 10.0.
Seems to be a bug in Eclipse, you should file it here. You already have a minimal project that reproduces the error, that helps a lot.
Your test project already works in RC2 of 4.11 (which will be released on March 20, 2019)
You can download the release candidate at https://download.eclipse.org/eclipse/downloads/drops4/S-4.11RC2-201903070500/
Better way to deal with this kind of problem by just go to dependencies hierarchy in pom.xml, where you can find duplicate jar, sometimes it could be difficult to find duplicate jar as jar itself have pom dependencies and "Dependencies hierarchy" will help you to deal with maven problem
`

Debug Stream trace on functional code throws IncompatibleClassChangeError

When I tried to debug the stream in the code below via Stream Trace in IntelliJ, the debugger can't evaluate the foreach because the error below is thrown. I have no idea what it's about, the code by itself runs fine.
Fully updated IntelliJ community edition, JUnit 5, Spring Boot, Maven, Java 11.
The error that happens during Stream Trace debugging only:
java.lang.IncompatibleClassChangeError: Type
com.progonkpa.file.FileService$GeneratedEvaluationClass$5 is not a
nest member of com.progonkpa.file.FileService: types are in different
packages
The code that contains the stream:
public class FileService {
public void createDirs(File parentDir, String[] fileNames) {
Stream.of(fileNames)
.map(fileName -> new File(parentDir, fileName))
.forEach(file -> {
if (file.mkdirs())
System.out.println("Created file: " + file);
else
System.err.println("Failed to create file: " + file);
});
}
}
The test that invokes the method above:
public class FileServiceTest {
private FileService fileService = new FileService();
#Test
public void generateDirs_createsList() {
File tmpDir = new File("/tmp");
String[] dirNamesList = {"dir1", "dir2"};
File createdDir1 = new File(tmpDir, dirNamesList[0]);
File createdDir2 = new File(tmpDir, dirNamesList[1]);
fileService.createDirs(tmpDir, dirNamesList);
assertTrue(createdDir1.exists());
assertTrue(createdDir2.exists());
assertTrue(createdDir1.delete());
assertTrue(createdDir2.delete());
assertTrue(tmpDir.delete());
}
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.unknown.somefunction</groupId>
<artifactId>joske</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!--<dependency>-->
<!--<groupId>org.springframework.boot</groupId>-->
<!--<artifactId>spring-boot-starter-test</artifactId>-->
<!--<scope>test</scope>-->
<!--</dependency>-->
<!--Data processing-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-csv</artifactId>
<version>1.6</version>
</dependency>
<dependency>
<groupId>org.simplejavamail</groupId>
<artifactId>simple-java-mail</artifactId>
<version>5.1.3</version>
</dependency>
<dependency>
<groupId>org.simplejavamail</groupId>
<artifactId>outlook-message-parser</artifactId>
<version>1.1.17</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>com.github.vatbub</groupId>
<artifactId>mslinks</artifactId>
<version>1.0.5</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.3.2</version>
<scope>test</scope>
</dependency>
<!--Testing-->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
The Stream debugger apparently generates bytecode and defines classes on the fly to evaluate expressions.
Relevant source files are
CompilingEvaluator.java
CompilingEvaluatorImpl.java
And there is a currently opened issue on YouTrack, with the exact same exception
Type some.Type$GeneratedEvaluationClass$1 is not a nest member of some.Type: types are in different packages
IDEA-204665
This manifests itself only on JDK versions greater than 10, and unfortunately you have
<java.version>11</java.version>
As the Issue suggest, it happens because
JDK 11 has "Nest-based Access Control" feature
(https://cr.openjdk.java.net/~dlsmith/nestmates.html)
The JEP 181 says
Impact on Other Tools
Any tool that operates on classfiles, or which
generates or processes bytecodes is potentially impacted by these
changes. At a minimum such tools must tolerate the presence of the new
classfile attributes and allow for the change in bytecode rules. For
example:
The javap classfile inspection tool, The Pack200 implementation, and
The ASM bytecode manipulation framework, which is also used internally
in the JDK.

Maven TinyB failure: package tinyb does not exist

I am trying to do a program where I need using Aws sdk and TinyB library. FOr that reason I have decided to use maven to create the project and resolve the dependencies. However, I have been trying to compile the project with the package TinyB for more than a week without success. I would be very grateful if someone could teach me what I am doing wrong.
The failure message I am receving is the following:
C:/Users/fran/Desktop/RSSI_AWS_PROJECT/BleDistanceMeasurement/src/main/java/org/tfm/app/BleMng.java:[7,1]
package tinyb does not exist
And my pom.xml is:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.tfm.app</groupId>
<artifactId>BleDistanceMeasurement</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>BleDistanceMeasurement</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk</artifactId>
<version>1.11.327</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-iot</artifactId>
<version>1.10.34</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-iot-device-sdk-java</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-iot-device-sdk-java-samples</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>org.eclipse.kura</groupId>
<artifactId>tinyb</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>tinyb</id>
<url>https://repo.eclipse.org/content/groups/releases/</url>
</repository>
</repositories>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
I have opened the project with Eclipse IDE to know what is happening but I have seen that eclipse is recognizing the dependencies correctly.
TinyB dependencie scan
Project dependencies
TinyB library I have downloaded and compiled the source from here:
https://github.com/intel-iot-devkit/tinyb
But there is no maven repositories directly from intel so I have added the one form eclipse kura:
<!-- https://mvnrepository.com/artifact/org.sputnikdev/bluetooth-manager-tinyb -->
<dependency>
<groupId>org.sputnikdev</groupId>
<artifactId>bluetooth-manager-tinyb</artifactId>
<version>1.0</version>
</dependency>
This one gives me problems when compiling, it seems as if the repository hasn't been downloaded. But the foder with the jar exists (it shows the error quoted previously).
I have made some little programs with TinyB and they are working perfectly, so the program is compiled and installed correctly. The problem is I am not using maven in this little programs (I just add the import and point to the .jar when executing). Like this:
sudo java -cp examples/java/HelloTinyB.jar:/usr/lib/lib/java/tinyb.jar HelloTinyB
I have also try this other maven repository:
<!-- https://mvnrepository.com/artifact/org.sputnikdev/bluetooth-manager-tinyb -->
<dependency>
<groupId>org.sputnikdev</groupId>
<artifactId>bluetooth-manager-tinyb</artifactId>
<version>1.3.2</version>
</dependency>
In this case it recognizes the dependencies and compiles. The problem is it gives error when I try to execute the program:
java.lang.RuntimeException: Native library is out of date. Please update the native library.
at tinyb.BluetoothManager.getBluetoothManager (BluetoothManager.java:317)
Thank you very much for your help.
I see you are trying to use eclipse Kura too.
Did you achieve making it work?
I have the same issue.
By the way, I see that maybe your dependencies are not really valid, as sputnikdev has a library over tinyB, but, finally I have the same problem than you. These are my main dependencies:
<!-- https://mvnrepository.com/artifact/org.sputnikdev/org.eclipse.smarthome.binding.bluetooth.transport.tinyb -->
<dependency>
<groupId>org.sputnikdev</groupId>
<artifactId>bluetooth-manager</artifactId>
<version>1.5.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/intel-iot-devkit/tinyb -->
<dependency>
<groupId>intel-iot-devkit</groupId>
<artifactId>tinyb</artifactId>
<version>0.5.1</version>
</dependency>
<dependency>
<groupId>org.sputnikdev</groupId>
<artifactId>bluetooth-manager-tinyb</artifactId>
<version>1.3.3</version>
</dependency>

The import org.springframework.test.context.junit4.SpringJUnit4ClassRunner cannot be resolved

I'm a newbie at Spring and this is also my very first question on StackOverflow so I'm going to try to make this as understandable as possible.
I'm trying to make a web service client using Spring and Maven on this tutorial: and I get this error: The import org.springframework.test.context.junit4 cannot be resolved
Here is my code:
package demo;
import hello.WsClientApplication;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; //this won't import
#RunWith(SpringJUnit4ClassRunner.class)
#SpringApplicationConfiguration(classes = WsClientApplication.class)
public class WsClientApplicationTests {
#Test
public void contextLoads() {
}
}
Here is my pom.xml in case you need it.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework</groupId>
<artifactId>gs-consuming-web-service</artifactId>
<version>0.1.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.3.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ws</groupId>
<artifactId>spring-ws-core</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- tag::wsdl[] -->
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.12.3</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<schemaLanguage>WSDL</schemaLanguage>
<generatePackage>hello.wsdl</generatePackage>
<schemas>
<schema>
<url>http://wsf.cdyne.com/WeatherWS/Weather.asmx?wsdl</url>
</schema>
</schemas>
</configuration>
</plugin>
<!-- end::wsdl[] -->
</plugins>
</build>
</project>
I have tried some other solutions in StackOverflow but I can't get it to work.
Thanks.
You need to add a dependency on spring-boot-starter-test:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
Now, if you are using latest spring-boot, 1.5.2 RELEASE, #SpringApplicationConfiguration is no longer available, instead you must use #SpringBootTest. Reference here(#spring boot starter test in Spring Boot)
Gradle
In gradle this would be done by adding
testCompile("org.springframework.boot:spring-boot-starter-test")
to the dependencies block as in:
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
testCompile("junit:junit")
testCompile("org.springframework.boot:spring-boot-starter-test")
}
After that it should be possible to write an import statement at the top of your class
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
Allowing you to use the annotation:
#RunWith(SpringJUnit4ClassRunner.class)
public class MyAppTest {
}
I know this is answered kind of late, but I had the same problem and I found I was able to fix it 2 ways.
I was able to remove the < scope > test < / scope> tags and that removed the restriction
I had an issue with intellij not properly handling it when doing tests, so I made sure it was marked as test-root, I changed it from green -> gray -> back to the green test-root and that resolved my issue.
Just to improve in the situation.
I managed to fix it like this:
before, spring boot initializr created a dependency to spring-boot-starter-test but with the exclusion, like this:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
Eclipse accused the annotation #Runwith to be Junit 4, therefore, the logic thing to do was to remove the exclusion.
Final POM dependency:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
sometimes maven start downloading dependencies and doesn't finish.
Go to your .m2 folder and type:
find . -name *progre*
this command will show you every file with the tag "in-progess", which are the files missing or not finished.
delete the folder and try to update the dependencies again.
In case, you are not using the spring boot, but only spring framework you should add dependency for spring-test.
<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>2.5</version>
<scope>test</scope>
</dependency>
You can check the latest version for spring-test using mvn, and update accordingly.
If you are using spring-boot project than add spring boot dependency, if you are not using spring-boot the below will work, but not recommended.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<version>2.4.1</version>
</dependency>
NOTE: If you are using spring-boot project, adding <version> will not be necessary.

JUnit Test error with embedded glassfish container

I must admit this JUnit Test has been a nightmare till now, I have invested some days, searching the Internet but everything I tried did not work.
The Error I have is following:
SCHWERWIEGEND: Exception while invoking class org.glassfish.persistence.jpa.JPADeployer prepare method
java.lang.RuntimeException: Invalid resource : jdbc/sample__pm
The jdbc/sample__pm does not exist but the jdbc/sample in the persistance.xml does.
As I said I have already searched for this problem but I cound not find a solution to the problem.
I guess the problem is with the pom.xml, but don't know how to fix it.
Thanks for any help
the pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.maggioni</groupId>
<artifactId>SampleApp</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.5.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.jpa.modelgen.processor</artifactId>
<version>2.5.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.main.extras</groupId>
<artifactId>glassfish-embedded-all</artifactId>
<version>4.0</version>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derbyclient</artifactId>
<version>10.10.2.0</version>
</dependency>
</dependencies>
<build>
<finalName>SampleApp</finalName>
</build>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<failOnMissingWebXml>false</failOnMissingWebXml>
</properties>
the source can also be found here
I had the same problem, and after losing lot's of time I got it right. I now can run my tests with my connection pool.
This problem is resulted by series of small maven code generation bugs in Netbeans.
Bellow are a serie of steps for you to check
1 - Your pom.xml must have the path , plugin and dependencies configured for glassfish-embedded-static-shell, as follow:
Add in the properties at the top of your pom the path to your glassfish instalation folder(here is mine):
<properties>
<endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<glassfish.embedded-static-shell.jar>C:/Program Files/glassfish-4.0/glassfish/lib/embedded/glassfish-embedded-static-shell.jar</glassfish.embedded-static-shell.jar>
</properties>
Add/check the plugin definition bellow(substitute mysql per maven in your case):
`
<build>
...
<plugins>
...
<plugin>
<groupId>org.glassfish.embedded</groupId>
<artifactId>maven-embedded-glassfish-plugin</artifactId>
<version>4.0</version>
<dependencies>
<dependency>
<groupId>org.glassfish.main.common</groupId>
<artifactId>simple-glassfish-api</artifactId>
<version>4.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.main.extras</groupId>
<artifactId>glassfish-embedded-static-shell</artifactId>
<version>4.1</version>
<scope>system</scope>
<systemPath>${glassfish.embedded-static-shell.jar}</systemPath>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.28</version>
</dependency>
</dependencies>
<configuration>
<app>target/${project.artifactId}-${project.version}</app>
<port>8282</port>
<contextRoot>${project.artifactId}</contextRoot>
<foo>bar</foo>
</configuration>
</plugin>
...
</plugins>
...
</build>
`
2 - Your glassfish-resources... it's silly, but the jndi-name of your jdbc resource must have the prefix java:app/ so, add it as sample bellow(just the resource, the connection pool can have any name):
*Just remembering the name of your jdbc-resource in glassfish will not have the prefix java:app, e.g.: my jdbc-resource in glassfish is just ShrewdPCPool
3 - Third and the silliest, when deploying the embedded glassfish, the auto generated script of Netbeans doen't include /src/main/resources/setup in the class path, so, the simple solution is to make a copy of your glassfish-resources.xml para /src/main/resources/META-INF/ or if you are patient enough change the script.
Dont forget to clean, and after build with dependencies before you run your tests!

Categories