Spring-Boot OAuth2 Strange Behavior When Authenticating with Twitch - java

I have been trying to set up OAuth2 with Twitch through Spring for a few days now. I have resolved quite a few problems in that process but this one has stumped me. When I attempt to access one of the endpoints that I am trying to require users to be authenticated with Twitch for I am getting redirected to localhost:8080/login and being shown a page that simply reads "Login with OAuth 2.0" and has nothing else on it. My expectation was that Spring would automatically redirect me to Twitch's authentication portal and then Twitch would send me back to the Spring application after going through the OAuth process. Instead I am simply being shown that page and nothing is happening.
As far as what has been done so far to remedy this problem... pretty much nothing. I have not been able to find anyone else running into this problem so I am thinking that there are a few possible issues... Based on this tutorial https://spring.io/guides/tutorials/spring-boot-oauth2/ it seems like Spring has out of the box functionality for a lot of different OAuth providers. I am guessing that Twitch is not one of them. That makes me concerned that something on Twitch's backend might be causing this problem for Spring (if that is the case then I will need to make a custom authenticator). The other possibility I have thought of is that Spring might need to be told where to redirect users to for them to get authenticated. If that is the case then I would appreciate some help with that as I have not been able to find any indication that that is something that needs to be done online (and I have no idea how to do it).
Some important files from my project:
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 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.5.7</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.redacted</groupId>
<artifactId>redacted</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>redacted</name>
<description>redacted</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<version>9.0.44</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>2.4.4</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-oauth2-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
application.properties:
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
spring.security.oauth2.client.registration.twitch.client-id=redacted
spring.security.oauth2.client.registration.twitch.client-secret=redacted
spring.security.oauth2.client.registration.twitch.client-authentication-method=post
spring.security.oauth2.client.registration.twitch.redirect-uri=http://localhost:8080/login/oauth2/code/twitch
spring.security.oauth2.client.registration.twitch.provider=twitch
spring.security.oauth2.client.registration.twitch.scope=user:read:email
spring.security.oauth2.client.registration.twitch.authorization-grant-type=AUTHORIZATION_CODE
spring.security.oauth2.client.provider.twitch.authorization-uri=https://id.twitch.tv/oauth2/authorize
spring.security.oauth2.client.provider.twitch.token-uri=https://id.twitch.tv/oauth2/token
spring.security.oauth2.client.provider.twitch.user-info-uri=https://id.twitch.tv/oauth2/userinfo
spring.security.oauth2.client.provider.twitch.user-name-attribute=redacted
and also my SpringSecurityConfiguration file in the event it matters:
package com.redacted;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
#Configuration
#EnableWebSecurity
public class SpringSecurityConfiguration extends WebSecurityConfigurerAdapter {
#Override
public void configure(HttpSecurity httpSecurity) throws Exception {
//This allows users to access the "/" and "/Info" endpoints without authenticating with Twitch. To go anywhere else they will have to authenticate.
httpSecurity.antMatcher("/**").authorizeRequests().antMatchers("/", "/Info").permitAll().anyRequest().authenticated().and().oauth2Login();
}
}
My project structure:
Thank you for your time and consideration, I appreciate the help.
-Epoch
Edit:
Something I have recently found that I thought might be prudent to add to this discussion - when I am attempting to access a secured page in my current setup I see this screen:
It looks as if the intended way Spring normally displays this screen is like this:
It's almost as if Spring simply is not seeing my Oauth provider. Not sure if this information is helpful but I thought I would include it. Springs intended behavior for Oauth is that when there is only one provider configured the /login page is skipped altogether. I would actually prefer this behavior to be exhibited (but given it sees no providers I presume it shows the page).

I was able to recreate the issue. You have specified:
spring.security.oauth2.client.registration.twitch.authorization-grant-type=AUTHORIZATION_CODE
While it should be:
spring.security.oauth2.client.registration.twitch.authorization-grant-type=authorization_code
Spring Boot is unable to load your configuration properties properly, so it's as if you have 0 client registrations configured.

Try adding the .oauth2Client() in your configure method instead of .oauth2Login().

Related

Spring security refuses to use my config file

im learning Spring security and created a config file to change the login from form to basic as a first step, however the config file doesn't do anything and no changes happen
here is the code in the config file:
package Security;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.web.SecurityFilterChain;
#Configuration
#EnableWebSecurity
public class securityConfiguration {
#Bean
public SecurityFilterChain configure(HttpSecurity Sec ) throws Exception {
Sec
.authorizeRequests()
.anyRequest()
.authenticated()
.and()
.httpBasic();
return Sec.build();}
}
and this is 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 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>3.0.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>test</name>
<description>test</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity6</artifactId>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</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 suspected the problem might be spring not picking up the config file but the annotations did nothing to fix this.
I believe the issue might have to do with how you are organizing the packages. By default, Spring will look under the package of the main application or any sub-packages.
It's hard to be sure from the information provided in the question, but for instance, if your main application class is under the package com.example, and the security configuration class is under the package Security, it won't find it by default. Also, be sure to not use the default package (or "no package").
So the options are:
Add #ComponentScan annotation to the main class to indicate where to look: #ComponentScan("Security")
Move the security config class under the same package as the main class or a sub-package: com.example.security (recommended).
See Spring documentation for reference.
I also suggest that you use the standard naming and styling conventions for Java. (lower-case package names, lower-case method attributes, pascal-case class names). See Naming conventions for reference. It will not only look better and consistent with other code bases but will also make it compatible with many tools that follow convention over configuration, which is favored a lot in the Spring ecosystem.

mvn test does not run unit tests in springboot project

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

Basic spring boot app not working, showing: Failed to refresh live data from process xxxx

I am beginner for spring boot. I initialized a new project and tried to run it but it does not work successfully. WHen I run this as spring boot application, it starts execution. In bottom compiler/status bar, it shows processing and retrying. it goes upto 10 times and throw the following error:
Failed to refresh live data from process xxxx
More detail here
TanmayTestApplication.java
package com.example.tanmay_test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class TanmayTestApplication {
public static void main(String[] args) {
SpringApplication.run(TanmayTestApplication.class, args);
}
}
DemoControler.java
package com.example.cntr;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
#RestController
public class DemoControler {
#RequestMapping(path = "/index")
public String index() {
return "By Tanmay!";
}
}
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 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.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>tanmay_test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>tanmay_test</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</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>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
I had the same problem in STS, and tried different things to resolve it. The following dependency for spring actuator makes that problem disappear, but however the main point of spring actuator provides more features than this. To learn more, click https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-features.html
The dependency should be added to your pom.xml file
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
I have faced the same problem but managed to solve it.
The controller class has to be in the "child package" relative to the TestApplication class.
In your case, your TanmayTestApplication class is in the package com.example.tanmay_test. Therefore, your DemoControler class must be inside the package com.example.tanmay_test.xxx.
**Note that xxx can be anything but extends from package com.example.tanmay_test. For example, package com.example.tanmay_test.web.
Hope this helps!
Add this line in your file application.properties (src/main/resources):
spring.devtools.livereload.enabled=true
Live data is collected with the help of Spring Actuator.
You need to include the following dependency in your pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
See https://github.com/spring-projects/sts4/wiki/Live-Application-Information#application-requirements-for-spring-boot-projects for reference.
I was also facing same issue after adding Spring Actuator dependency, it resolved.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
after adding this in POM.xml, do maven build and run again.
It is simply saying that you didn't enable LiveReload.
This is non other then the Data Source error
To resolves this I disabled the auto-configuration of the DataSource. And, this will not affect auto-configuring any other beans.
#SpringBootApplication(exclude = {DataSourceAutoConfiguration.class })
I'm using VS Code and the thing that worked for me was adding a dev tool dependency.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
Also, add spring.devtools.livereload.enabled=true in application.properties file so that server knows that it has to reload every time a change is made.
Thanks for this one.

Maven install succeeds, but jar doesn't work correctly from bash (CLI)

currently I'm working on a project in my company that imports XML data to our database. While doing this I rely on some basic configuration projects which have been already created and used in other Projects, i.e. an EntityManagerBuilder or other utility classes that are used in order to create a connection to our oracle database. And it seems to me that those dependencies are creating some problems for me.
My project runs perfectly fine if I start it within eclipse. And when I create the project with mvn clean install -DskipTests it builds all fine.
But when I want to run it from the command line the application starts and after a few lines of code just stops, without throwing any errors or exceptions.
The reason why I think that it has something to do with some dependencies is that by logging I managed to find the point where the application stops. Since it stopped at a point I could investigate, I just did that. I downloaded the sources an only added some logging and suddenly my application had no problems at all with that class, instead it just stopped with the next static call to an other class.
I have no idea at all where to search for the error. Since this is an application that has to run by it self as a monthly task, executing it from eclipse is not an option.
Hopefully someone can give me a hint how to solve this.
Here is my POM:
<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>com.company.infrastructure.foo</groupId>
<artifactId>foo</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<artifactId>foo-import</artifactId>
<packaging>jar</packaging>
<name>FooImport</name>
<properties>
<company.consoleapp.main.class>com.company.infrastructure.foo.import.FooImporter</company.consoleapp.main.class>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.company.maven</groupId>
<artifactId>company-standalone-dm</artifactId>
<version>${company.parent.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- Logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</dependency>
<!-- Utilities -->
<dependency>
<groupId>args4j</groupId>
<artifactId>args4j</artifactId>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.2.11</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
<version>2.2.11</version>
</dependency>
<!-- Using foo-dataaccess -->
<dependency>
<groupId>com.company.infrastructure.marken</groupId>
<artifactId>foo-dataaccess</artifactId>
<version>1.0.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<finalName>foo-import</finalName>
<plugins>
<!-- Consoleapp-Mixin -->
<plugin>
<groupId>com.github.odavid.maven.plugins</groupId>
<artifactId>mixin-maven-plugin</artifactId>
<configuration>
<mixins>
<mixin>
<groupId>com.company.maven</groupId>
<artifactId>company-consoleapp-mixin</artifactId>
<version>${company.parent.version}</version>
</mixin>
</mixins>
</configuration>
</plugin>
</plugins>
</build>
I think this might be something to do with your dependency management in maven, since you haven't specified the versions and maven figures out those versions automatically.
However when you run the application, you need those jars to be in your classpath otherwise you might end up getting a ClassNotFoundException because the jars are not available. So unless you figure out what your dependencies are like you mentioned and add them to your classpath you would end up seeing the error.
It might be worth enabling a further level of logging in your application to give you some indication of where the error is. You could also try and see if at the point of failure is referenced to an external library, which is the one that is not available in your classpath.
Could you also please share how your running your application through CLI.

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.

Categories