I am testing a Spring Boot application using code from a tutorial. The tutorial describes the setup and configuration of a Spring Boot application, and also describes a test that uses JAssert calls in the following manner:
package hello;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
#RunWith(SpringRunner.class)
#SpringBootTest
public class SmokeTest {
#Autowired
private HomeController controller;
#Test
public void contexLoads() throws Exception {
assertThat(controller).isNotNull();
}
}
The test, unfortunately, will not compile in my IDE. The compilation is failing on the assertThat() method.
I am using Eclipse with Maven for my IDE. I have checked the Maven dependencies and see that the JAssert core library is included. Unfortunately, despite this the compiler can not seem to "find" the assertThat() call.
It fails to compile the test for that reason.
How do I get the test to utilize JAssert and find the calls to JAssert functions?
I'm not an eclipse expert, but it seems like you have some configuration issue here.
If you're sure that the dependency appears in pom.xml in scope test then try to eliminate eclipse related issues by running the tests directly via maven:
mvn test
will do the job
If it runs sucessfully, then re-create eclipse configurations from pom.xml / re-import the project.
If not, its a pom.xml related issue and has nothing to do with eclipse, you'll have to fix the pom or maven ecosystem. I suggest the following:
go to the local repository and remove the dependency from the file system (manually, the whole directory, with jar, pom.xml and everything) and then rerun mvn test
Sometimes dependencies are downloaded corrupted and despite being defined correctly in pom.xml they do not really contain classes in a form that java/maven can read
Actually:
It turns out that neither the Boot starter nor the IDE seems to find the necessary import declaration for the JAssert functions. Could it be because they are static?
In looking at some example code, I found in the code the import declaration for the assertThat() method. Usually, Eclipse would have suggested this declaration but it does not.
What is weird is that I put in the declaration by hand, and not only does this allow me to compile, but Eclipse, once the import is included, the code assistant makes proper assertThat() suggestions without problems!
There could be a bug somewhere, but that is beyond the scope of my particular problem. My test now compiles without problems.
I hope someone looking at this can figure out why Elipse's code assist does not work for this particular library properly. I wouldn't be surprised to discover that there are other objects/libraries in Spring Boot that don't get properly handled by the code assistant.
Related
I have an eclipse workspace (version 2019-12, non-maven) with multiple web projects, all containing java code, and some referencing each other. At the end of the reference 'graph' is a project dedicated to contain a test suite, which in turn is supposed to reference all other projects' tests.
All projects export their test-source folders. (All test classes are visible to the downstream projects).
I started to define the following suite:
package platformTest;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.platform.suite.api.SelectClasses;
import org.junit.runner.RunWith;
import de.***.PlatformEnvironmentTestcases;
import de.***.ClientTestcases;
#RunWith(JUnitPlatform.class)
#SelectClasses({
PlatformEnvironmentTestcases.class,
ClientTestcases.class
})
public class JUnit5TestSuite {
// test suite
}
However the referenced test classes (though successfully running on their own, and being directly referenced) are not being executed at all.
I tried:
setting the debug config to junit4, with the result that no tests are executed. It seems the compatibility layer between junit 4 and 5 is not working here.
setting the debug config to junit5, with the result that a message window pops up: "No tests found with test runner 'JUnit 5'.
setting the debug config to junit5 and selecting 'Run all tests in the selected project, package or folder:' which - surprisingly - is working, but I cannot select the workspace root, which means I can never select all projects, and this setup will only find the tests in the suit's project and the selected project, but not the rest.
I found multiple bugs referenced for older versions of eclipse displaying similar symptoms, but since I am running a very recent version, I suppose those should not apply anymore. Also I tried most of the workarounds suggested without success.
I have two projects. One written fully in Kotlin that exports a Client artifact. I am trying to use the client in a second project that has a mix of java/kotlin code.
In the java classes I have no problem importing the kotlin files for use, but in any kotlin files, IntelliJ cannot resolve the imports. All the other kotlin code works fine and I can compile on the command line via maven with no problems. It's just an issue with IntelliJ not recognizing the package I'm looking for.
I see the package in both my maven toolbar as well as in the external libraries listed in the project. I've inspected the jars and sure enough the file I expect com/foo/bar/BazClient.class is present, but
import com.foo.bar.BazClient tells me the package bar (the code from the other project) does not exist. The very same import statement works just fine in java code.
Further adding to my problems the exact same java class that imports this client, if I convert to kotlin using IJ's builtin method, fails to compile in IntelliJ. The "build project" action completes successfully with no warnings/errors.
Kotlin client is defined as:
package com.foo.bar
import retrofit2.http.GET
interface BazClient {
#GET("/v1/fuzz")
fun getFuzz(): Call<FuzzResponse>
}
Working java code:
package com.whodat.wat;
import javax.inject.Singleton;
import com.foo.bar.BazClient;
#Singleton
public class CallTheService {
private final BazClient bazClient;
public CallTheService(BazClient bazClient) {
this.bazClient = bazClient;
}
public FuzzResponse callIt() throws IOException {
return bazClient.getFuzz().execute().body();
}
}
Failing kotlin code:
package com.whodat.wat
import javax.inject.Singleton
import com.foo.bar.BazClient // "bar" is red in editor
#Singleton
// Can't resolve "BazClient" here
class CallTheService(private val bazClient: BazClient) {
fun callIt(): FuzzResponse {
return bazClient.getFuzz().execute().body()!!
}
}
It turns out this was a problem in publishing the client artifact. We were using the maven shade and jar plugins to create the jars and was leading to many kotlin_modules in the jar which were confusing IJ
META-INF/client.kotlin_module
META-INF/descriptors.jvm.kotlin_module
META-INF/descriptors.kotlin_module
META-INF/descriptors.runtime.kotlin_module
META-INF/deserialization.kotlin_module
META-INF/kotlin-reflect-api.kotlin_module
META-INF/metadata.jvm.kotlin_module
META-INF/metadata.kotlin_module
META-INF/util.runtime.kotlin_module
META-INF/kotlin-stdlib.kotlin_module
META-INF/kotlin-stdlib_coroutinesExperimental.kotlin_module
META-INF/kotlin-stdlib-common.kotlin_module
META-INF/kotlin-stdlib-common-coroutines.kotlin_module
META-INF/kotlin-stdlib-jdk8.kotlin_module
META-INF/kotlin-stdlib-jdk7.kotlin_module
Removing those from the client build seems to have cleared things up, and now we just have the one client.kotlin_module.
This problem has a ticket in Jetbrains issue tracker. The ticket has not been resolved yet, but support said:
This is a known problem since the 1.2.50 release of the Kotlin plugin:
KT-25709. As a workaround, please stop bundling the Kotlin
runtime in the dependency jar (i.e. everything under the package
kotlin should be removed from the jar)
My aplication uses Spring 4.3.1 and JUnit 4.12.
I have been trying test my methods with JUnit test cases.
I placed the #RunWith(SpringJUnit4ClassRunner.class) annotation in my test class, but eclipse complains "Class cannot be resolved to a type".
I read that this was enough for Eclipse to recognize SpringJUnit4ClassRunner, but isn't.
Is there Anything else remaining to do?
Try to:
Add spring-starter-test dependency through your configuration file & include relevant import statements in test files.
Delete local dependency repository & force new build to your application.
Adding the dependency spring-test the problem has been solved.
I didn't know there was need for this additional dependence, but someone told me here. Thank you.
I initially said that this measure was not enough because I had written the dependency version name wrongly and Maven was not finding the resource. But now it works!
I was writing unit tests for my javaagent but faced with behavior of JVM I guess which I wasn't aware before and I curious if there is any explanation or article about it. I tried google it and search on SO but with no success.
I found that classes which are included into boot classpath doesn't have annotations with them. To demonstrate it I created a simple JUnit test
import org.junit.Test;
public class SimpleTest {
#Test
public void myTest() {
}
}
It perfectly runs of course :) but if I configure eclipse project like this:
Then it fails with java.lang.Exception: No runnable methods. I see in the debugger that code which checks annotations cannot find them.
I found answer on SO to my question which is perfectly explains what is going on (I voted up there).
https://stackoverflow.com/a/23502439/2013497
JUnit library is added by Eclipse and it goes to regular classpath whenever bootloaded classes doesn't have a way to reference them.
a Blank XML is being generated when im using the following code :
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
import org.junit.runner.RunWith;
#RunWith(Cucumber.class)
#CucumberOptions(plugin = "junit:target/cucumber-report.xml")
public class RunCukesTest {
}
I'm using cucumber version 1.2.4. Is there some error in the code, or there is some version issue. as it was working with 1.2.5 but as soon as integrated with ivy it stopped generating. Is there any other jar to add in code for this.
Why do you think there is a problem with Cucumber when it worked before you added another tool to your tool chain? Look for problems around the new tool you added.
But before you do that, revert your changes until it starts working again. Then add whatever you need in small steps and always keep the solution working. I.e. run often and see that the report you are expecting still appears and contains the expected values. Commit your code between each working step and revert when something unexpected happens that breaks your functionality.
So the problem was generated due to usuage of logback-classic jar. When i replaced logback-classic jar to logback-core jar , the issue was resolved.