I created a spring-boot application using jhipster and open in Spring Studio Tool.
To run Junit cases when I click on any Test File and Run as JUnit, it shows DEBUG : [onnection adder] org.postgresql.ssl.MakeSSL : converting regular socket connection to ssl in console window in JUnit shows Runs 0/0 ,Errors 0, Failures 0 .
How I can run each test case in Jhipster. I write test case like below
#Test
public void saveApplication() throws Exception {
restMockMvc.perform(post("/api/application")
.contentType(MediaType.APPLICATION_JSON)
.content(asJsonString(App)))
.andExpect(status().isOk());
}
and junit only shows Run 1/21 , failed 1 when liquibase lock error comes.
Related
We have a plugin project with some domain classes and services etc.
We have an application project which uses the plugin project.
This is a common pattern.
Integration test (which hit the DB) cant run in the plugin project, as it has no application context, so we run the integration tests in the main application project.
We have a very simple integration test:
/*#TestFor(Site)*/
class SiteIntegrationSpec extends IntegrationSpec {
static transactional=false;
def setup() {
}
def cleanup() {
}
void "test something"() {
Site site
when:
site = Site.get(1L)
then:
site.name == "root"
}
}
The site is just a domain object, similar to this:
class Site {
String name
// some more fields here
}
NOTE: tried it with TestFor(Site) un-commented also - same error.
If I look in the DB, there are site entries there.
OK, just found another clue. SiteIntegrationSpec test used to work. It worked before we added a second test, ParamIntegrationSpec. If we run either of these tests on their own thusly:
test-app --stacktrace --verbose ParamIntegrationSpec
works
test-app --stacktrace --verbose SiteIntegrationSpec
works
but if we run both of them:
test-app --stacktrace --verbose *IntegrationSpec
The SiteIntegrationSpec test always fails with the above exception.
any ideas?
Full stack trace:
java.lang.IllegalStateException: Could not find ApplicationContext, configure Grails correctly first
at grails.util.Holders.getApplicationContext(Holders.java:97)
at grails.test.spock.IntegrationSpec.$spock_initializeSharedFields(IntegrationSpec.groovy:41)
Note2:
test-app --stacktrace --verbose -integration
gives the same error on Site test.
Thanks to user1690588 I found the problem. Grails IntegrationSpec IllegalStateException gave me the clue: the probem was not in the test which failed, but in the test which passed!
Basically, the ParamIntegrationSpec test had:
#TestFor(ParamService)
This kills any subsequent test. I have no idea what TestFor does, only seen it in all examples.
To fix, just removed that line from the working test.
Am running Junit test cases for my project module. After the execution of the test case am getting the below error report.
ClaimControllerTest.shouldReturnClaimWithValidationResult:417 Status expected:<200> but was:<500>
Am using the below maven comment to execute the test case for a specific module.
mvn clean compile verify -Ptest
But, my project have many module dependency. How can i execute my test case for my complete project ( will all modules )
The above error is pointing to the below piece of test case code
mockMvc.perform(get(String.format("/claims/%d/validation-result", claim.getId())))
.andExpect(status().isOk())
.andExpect(jsonPath("$.catClaimNumber").value(claim.getCatClaimNumber().intValue()))
.andExpect(jsonPath("$.validationResult.programErrorCode").value("UNTHRZD_EXTRNL_USR"))
.andExpect(jsonPath("$.validationResult.hasErrors").value(true));
Update 1 :
Am seeing the below null pointer exception after doing Sys out
2019-01-03 15:46:27,401 [main] ERROR api.controller.BaseController: 83 - Unhandled exception while processing request for URL : http://localhost/claims/1901/validation-result with exception : null
java.lang.NullPointerException
For the below code :
mockMvc.perform(get(String.format("/claims/%d/validation-result", claim.getId()))).andDo(print());
It is telling you that instead of a success code 200 from calling your rest endpoint, it received an error code of 500. It means that your rest end point threw an exception.
you may want to print the full response with exception to further debug
mockMvc.perform(get(String.format("/claims/%d/validation-result", claim.getId()))).andDo(print());
You have two questions:
Getting 500 instead of 200 - This is already answered by #mkjh
How to execute test case for complete project ( will all modules ) - For this you should try mockito (mocking other modules) because you cannot and should not take into account of all modules until and unless it's an integration test where in you make sure all the modules are up and running or else you will again end up getting 500 error which basically tells that there is some internal server error happened and in most of the cases it happens because of some services are not up
I'm trying to run the sample code in this book: Building RESTful Web Services with Spring 5 - Second Edition. You can download the sample code for free. In chapter 7, they have some sample code that includes some integration tests and unit tests all in same directory "chapter7\ticketmanagement/src/test/java/com/packtpub/restapp/ticketmanagement/". The unit tests work fine. The integration tests fail with "testUsersJsoup(com.packtpub.restapp.ticketmanagement.JsoupUserTest) Time elapsed: 1.011 sec <<< ERROR!
java.net.ConnectException: Connection refused: connect"
I experimented with changing the port from 8080 to 9090 in "ticket-management\src\main\resources\application.properties" (I also changed the hardcoded values in the integration test) and this did not help.
So "mvn test" produces the above error.
So does running them in spring tool suite.
I edited the original pom.xml and added the "maven-compiler-plugin" to specify java version 1.8. I'm running windows 10.
So the failing code is in "chapter 7\ticket-management\src\test\java\com\packtpub\restapp\ticketmanagement\JsoupUserTest.java".
Here is the failing code:
public void testUsersJsoup() throws IOException {
String doc = Jsoup.connect("http://localhost:9090/user").ignoreContentType(true).get().body().text();
_log.info("{test} doc : " + doc);
JsonParser parser = new JsonParser();
JsonElement userElement = parser.parse(doc);
JsonArray userArray = userElement.getAsJsonArray();
_log.info("{test} size : " + userArray.size());
assertEquals(4, userArray.size());
}
HOWEVER, when I type
mvn spring-boot:run
it starts successfully and I point my chrome browser to http://localhost:9090/user (or http://localhost:8080/user if I am using the original code) I see
[{"userid":100,"username":"David"},{"userid":101,"username":"Peter"},{"userid":102,"username":"John"}]
No connection error! The results look correct.
We know this is not a firewall problem because it is just with localhost and it works with chrome.
There is no proxy involved.
I think the ports are correct: I presently have 9090 in the application.properities file, and in the source code and in the browser.
"mvn clean" and then "mvn test" again did not help.
There are annotations to start the web app as exemplified here:
Spring Live Lessons Tutorial and they work. See %JAVA_BUILDINGMICROSERVICES%\livelessons-security\livelessons-security-basic\src\test\java\basic\BasicSecurityApplicationTests.java.
These annotations have been deprecated, and replaced by newer ones. The problem is that the author of this tutorial is not using theses new annotations correctly: he needs to also use #WebAppConfiguration and this requires some configuration arguments from the #SpringBootTest (which he is missing). Therefor it necessary to manually start the web server with "mvn spring-boot:run" in a separate process and then run the tests.
When I tried to use #WebAppConfiguation, I got other errors. I will document these errors in another post (some day).
When I run my testcase using righclick on project Run as Junit test Its running fine.
But when I try to run with maven(maven clean install)
I am getting timeout exception.
[![INFO 2016-02-01 16:48:28,900 \[Thread-5\] org.mule.module.xml.filters.SchemaValidationFilter: Schema factory implementation: com.sun.org.apache.xerces.internal.jaxp.validation.XMLSchemaFactory#3074311d][1]][1]
I am getting continuous above log.
I have an aws instance running with webservices that I want to test with a java application running JUnit. If I have a script that runs the jar during the validate services step of the code deploy process how does the code deployment handle the return value of the jar if a test fails?
Is it required that I catch exceptions thrown by the jar for the failing tests and call system.exit(-1) to have the script terminate the code deployment or does the exception thrown automatically give the return value of non-zero?
If you are running JUnit using JunitCore, it should return a non-zero exit code if any of the tests fail.
main
public static void main(String... args)
Run the tests contained in the classes named in the args. If all tests run successfully, exit with a status of 0. Otherwise exit with a status of 1. Write feedback while tests are running and write stack traces for all failed tests after the tests all complete.
Parameters:
args - names of classes in which to find tests to run
From: http://junit.org/apidocs/org/junit/runner/JUnitCore.html#main%28java.lang.String...%29