Cannot import SpringApplicationContextLoader - java

I use IntelliJ IDEA and write Spring project.
I added dependency
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
and more related to Spring Boot.
I cannot import SpringApplicationContextLoader using:
import org.springframework.boot.test.SpringApplicationContextLoader
But I see many projects on GitHub which use this class.
How can I check current package of this class?

According to javadoc of SpringApplicatioNContextLoader a.4.x it was deprecated in favor of #SpringBootTest or SpringBootContextLoader if absolutely necessary.

Solution provided by Yoav Gur doesn't work for the latest spring-boot (1.5.2.RELEASE).
In my case I was looking for a solution to activate a spring profile for my cucumber tests. And I too found a lot of solutions which import this SpringApplicationContextLoader.class.
There is a class SpringBootContextLoader which might substitute for this, but I found another solution for my issues so didn't use it.
In case you're interested, you can find it here:
How to activate spring boot profile with cucumber

Try adding the following dependency to your pom:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
<version>1.1.1.RELEASE</version>
</dependency>
See:
https://1maven.com/idartifact/org.springframework.boot:spring-boot:1.1.1.RELEASE

Related

ngdbc.jar equivalent Maven repository

I am using ngdbc.jar to connect Java and HANA. Now I want to build a Web application. So I searched for the equivalent Maven repository but I could not find that one.
I found mostly for the cloud ones and finally found this one:
<dependency>
<groupId>com.sap.cloud</groupId>
<artifactId>neo-java-web-api</artifactId>
<version>1.53.18.2</version>
<scope>provided</scope>
</dependency>
Can any one tell which repository I can use instead of ngdbc.jar? Any help is appreciated
SAP doesn't allowed redistribution of ngdbc.jar in their TC as mentioned here. So you won't find any legal one online.
Your only option is to include the jar from your local. Something like:
<dependency>
<groupId>sample</groupId>
<artifactId>com.sample</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/yourJar.jar</systemPath>
</dependency>
described here here how to deal with the missing ngdbc.jar:
https://github.com/claboran/priceimporter
I tried to install the jar file as a dependency by
install:install-file -Dfile=C:\ngdbc.jar -DgroupId=com.han.driver -DartifactId=hana -Dversion=1.0 -Dpackaging=jar
in Maven which is pretty handy and good way.hope this helps someone dealing with custom jars.

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.

Maven dependency for Google Spreadsheet

Added the following dependency in pom.xml.
<dependency>
<groupId>com.google.api.client</groupId>
<artifactId>google-api-data-spreadsheet-v3</artifactId>
<version>1.0.10-alpha</version>
</dependency>
But it doesn't seem to work. It doesn't contain any of the required classes. For example, SpreadsheetService, SpreadsheetFeed, SpreadsheetEntryetc.
Is there any other maven repository for Google Spreadsheet?
Also, in this documentation, they haven't mentioned any maven repository. So, do I have to manually download the required JARs and add them to my project?
After some searching and experimentation, I finally found the dependency which contains the required classes to work with Google Spreadsheets. Here it is -
<dependency>
<groupId>com.google.gdata</groupId>
<artifactId>core</artifactId>
<version>1.47.1</version>
</dependency>
To test this, I used the code given here.
As far as getting authorization is concerned, look at this answer.

Override SLF4J implementation in unit tests for Android

How to override SLF4J implementation for unit tests?
For Android application im using:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-android</artifactId>
<version>1.6.1-RC1</version>
</dependency>
But unfortunately, running any unit test gives me:
java.lang.RuntimeException: Stub!
Due to stubbed Android JAR's in Maven Central.
I have a lot of utility and tiny service classes which don't use Android API. But some of them use slf4j logger's which causes this error.
How can i test them without splitting classes into two separate artifacts?
The way which i currently use is to remove slf4j-android and add slf4j-simple in test scope.
I found that i can configure Maven surefire plugin:
<configuration>
<classpathDependencyExcludes>
<dependencyExclude>org.slf4j:slf4j-android</dependencyExclude>
</classpathDependencyExcludes>
</configuration>
But IDE... No... Looks like IntelliJ IDEA silently ignores this setting and run tests with slf4j-android.
Similar question:
Alternate SLF4J Binding or Config For Unit Test?
But not suitable for slf4j-android.
Maybe somebody knows more convenient and working solution for this problem?
Maybe use another logging framework. But i don't know any other which works well with Android.
BTW: Robolectric didn't help.
Not perfect solution - workaround
In my project I have separated application logic (my-app-library) and unit tests (my-app-test) into two different modules. With this configuration I can test logic from my-app-library with standard logger.
Configuration from my-app-test:
<dependency>
<groupId>my.android.app</groupId>
<artifactId>my-app-library</artifactId>
<version>1.2.5-SNAPSHOT</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-android</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>

Difference between ehcache and ehcache-core

I am beginner for ehcache v/s ehcache-core in Spring framework, my pom.xml used ehcache version 1.5.0
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>1.5.0</version>
</dependency>
Now, it will need to update ehcache version because it will use in another jar:-
Updated ehcache version 2.7.0
But it returns error
net.sf.ehcache.Cache.getStatistics() method not found.
Now, I am replacing ehcache via ehcache-core 2.5.7 as:-
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<version>2.5.7</version>
</dependency>
Is it break another functionalities or will work same as ehcache?
Just as many other large frameworks (like Spring), ehcache is split into several modules. One of those modules is core, the others are web, server, jcache, debugger and many more (see https://mvnrepository.com/artifact/org.ehcache.modules).
Sometimes, for various reasons, you may not want to include the entire large framework, with all its sublibraries, into your project. Then you can decide which module you want to use.
In other words, using ehcache pom will include a full library in your project. Using ehcache-core will only include functionalities defined in ehcache-core.
You can either find out which module contains the functionality you need and include it, or go with full ehcache but use the appropriate version.
There still is an ehcache module in version 2.5.7 but as it only pulls dependencies it's of type pom. One of those dependencies is ehcache-core. My guess is that your functionality won't be satsified with just that. Try
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>2.5.7</version>
<type>pom</type>
</dependency>

Categories