We started to use jhipster for our Microservices, when migrating the old code an issue arises, that our unit tests do not start anymore cause of missing or duplicate beans.
Searching for an answer led me to theses similar questions:
Cannot run Unit Tests against REST layer
#ComponentScan in application class breaks #WebMvcTest and #SpringBootTest
As soon as I add the exclusions to the ComponentScan-Annotation of the generated application class the WebMvcTest run successfully.
Are there any reasons why these exclusions are missing in jhipster-generated application class?
Related
I'm trying to write unit tests for my class in ABAP but when I write the code manually in Eclipse, the IDE tells me there is no test class and no executable tests. If I use the wizard in SAP GUI, the generated test works and I can replace it with my test - which then also works - but when I close the SAP GUI and reopen it again, it doesn't detect any test class or executable tests again.
It seems like a bug to me or some issue with the SAP version I am using:
SAP NETWEAVER 7.4 15 (06/2016) sap.com SAP NETWEAVER 7.4
I'm a beginner ABAP programmer but I already successfully covered another one of my classes with test and I have no problems there. The only difference is that the other class runs on a different system with higher version of SAP - 7.5.
Have you encountered issue like this?
After few days of light research I found out there is a SAP Note 2598526 which pretty much describes the issue I have.
For the future reference it states that:
Reason and Prerequisites:
The ABAP unit test framework calls the class CL_ABAP_COMPILER to determine the test classes from the current program. However, the call of the class CL_ABAP_COMPILER terminates with an internal error.
Solution:
If the description applies to your situation, please install a new kernel that contains the patch "ABAP-SYCH: Dumped TYPES table has dangling type ids".
Right now we will try to patch the kernel and I will let you know if that helps.
EDIT 06/05/2019:
So... the patch didn't help with this issue but it looks like I finally found a solution. There are three steps to it:
Open SE03 and make sure the namespace is listed here with development license and in production role (P)
Open report: SATC_AC_INIT_NAMESPACE_REG and register the namespace to the ATC (ABAP Test Cockpit) - otherwise the cockpit will ignore the namespace and won't scan it for test classes.
The registration of the namespace will create a request so transport it and reactive the class that has unit tests.
When you do this, you should be able to run the tests - in both SAP GUI and Eclipse no problem :).
So, I have adde Flyway to my application to run integration tests with an embedded H2. I have also added a data script to db/migration/afterMigrate.sql. Everything worked fine with Spring Boot autoconfiguration up until the point that I created a second test class with #SpringBootTest. This class fails execution because Flyway tries to execute afterMigrate.sql again. I can't seem to prevent it from running, I have added the flyway-spring-test library to the project and tried using it but with no luck.
The thing I find odd is that the db migration isn't executed again for the second class, just the afterMigrate.sql script.
This was caused by a #SpyBean in the second class - the Spring context of the first one was not being reused. The workaround for this is creating an abstract class that both test classes extend from, and declaring the beans in it. This is expected behaviour from Spring but has been debated whether it should be or not - see more at https://github.com/spring-projects/spring-boot/issues/7174
Occasionally, when making some updates to a Grails unit test, I will run into an error along the following lines:
BUG! exception in phase 'instruction selection' in source unit ... unexpected NullpointerException
What causes this? Or what is the best strategy to debug this problem?
My best approach thus far has been to iteratively comment things out until the code compiles. A grails clean does not fix the issue.
Using this process I've found that the problem is my #TestFor(Class) annotation. I had moved some code from a service to a class in src/groovy. This appears to have caused the annotation to break. I don't really understand why specifically this breaks, but that was sufficient for me to get things working.
So try removing the #TestFor() annotation if your class under test is not a service or controller or similar.
If that doesn't work, comment everything out and add back one piece at a time.
In my case my service class don't have default constructor. I resolved this issue by providing default constructer As, #TestFor wants to inject service bean, as my service bean don't have default constructor spring container unable to inject.
I'm using ShrinkWrap to start Jetty server in my integration tests.
Problem:
When I start my test jetty-server and than make mockup of my controller - mockup doesn't work!
I suggest that the reason is different classloaders: JMockit - AppClassLoader, Jetty - WebAppClassLoader.
Question:
How to make mocking works fine?
P.S.
I've googled that -javaagent:jmockit.jar option may help. But it doesn't. Is it necessary for maven project based on 1.7 jdk?
ADDITION:
I've written demo to illustrate my problem. You can find it by the reference.
About my demo:
Except of ten stokes of code, it is identical to those project.
I've only added JMockit and a single mock to illustrate the problem.
You should see JettyDeploymentIntegrationUnitTestCase.requestWebapp method: in those method we make mock which doesn't work.
You can check that Jetty & JMockit loads classes by siblings classloaders, so JMockit simply doesn't see Jetty's classes
URLClassLoader
|
|-Launcher$AppClassLoader
|-WebAppClassLoader
The JUnit test in the example project is attempting to mock the ForwardingServlet class. But, in this scenario with an embedded Jetty web server, there are actually two instances of this class, both loaded in the same JVM but through different classloaders.
The first instance of the class is loaded by the regular classloader, through which classes are loaded from the thread that starts the JUnit test runner (AppClassLoader). So, when ForwardingServlet appears in test code, it is the one defined in this classloader. This is the class given to JMockit to mock, which is exactly what happens.
But then, a copy of ForwardingServlet is loaded inside the deployed web app (from the ".class" file in the file system, so not affected by the mocking as applied by JMockit, which is in-memory only), using Jetty's WebAppClassLoader. This class is never seen by JMockit.
There are two possible solutions to this issue:
Somehow get the class object loaded by WebAppClassLoader and then mock it by calling the MockUp(Class) constructor.
Configure the Jetty server so that it does not use a custom classloader for the classes in the web app.
The second solution is the easiest, and can be done simply by adding the following call on the ContextHandler object created from the WebArchive object, before setting the handler into the Jetty Server object:
handler.setClassLoader(ClassLoader.getSystemClassLoader());
I tested this and it worked as expected, with the #Mock doGet(...) method getting executed instead of the real one in ForwardingServlet.
I have a unit tests for Zend Framework controllers extending Zend_Test_PHPUnit_ControllerTestCase.
The tests are dispatching an action, which forwards to another action, like this:
// AdminControllerTest.php
public testAdminAction()
$this->dispath('/admin/index/index');
// forwards to login page
$this->assertModule('user');
$this->assertController('profile');
$this->assertController('login');
$this->assertResponseCode(401);
}
// NewsControllerTest.php
public testIndexAction()
{
$this->dispatch('/news/index/index');
$this->assertModule('news');
$this->assertController('index');
$this->assertController('index');
$this->assertResponseCode(200);
}
Both of the tests are passing when they are run as a seperate tests.
When I run them in the same test suite, the second one fails.
Instead dispatching /news/index/index the previous request is dispatched (user module).
How to trace this bug? Looks like I have some global state somewhere in the application, but I'm unable do debug this. How can I dump the objects between the tests in the suite? setUpBefore/AfterClass are static, so there are no so many data about the object instances.
I know this is a kind of guess what question. It's hard to provide reliable data here, because they would took to much place, so feel free to ask for details.
The whole unit test setup is more or less like described in: Testing Zend Framework MVC Applications - phly, boy, phly or Testing Zend Framework Controllers « Federico Cargnelutti.
Solution:
I've determined the issue (after a little nap). The problem was not in unit test setup, but in the tested code.
I use different ACL objects based on module name. Which one to use was determined by static call to action helper, which cached the result in a private static variable to speed things up. This cache was executed only when run in a test suite. I just need more unit tests for this code :)
(I'm sorry for such a rubbish post, but I've stuck with this for a day and I hoped someone else experienced similar kind of this Heisenbug with unit tests in general)
You may try clearingrequest and response objects before dispatching each action, like this:
$this->resetRequest()
->resetResponse()
->dispatch('/news/index/index');