Is there an equivalent for DropwizardTestSupport in Micronaut? - java

I'd like to run my application with JUnit for the integration test.
For Dropwizard, I've been using DropwizardTestSupport library to achieve that.
Wondering if there's an equivalent for that in Micronaut.
Thanks

I cannot say that is the equivalent of DropwizardTestSupport because I have not used that but we do have a Micronaut Test library which is described at https://micronaut-projects.github.io/micronaut-test/latest/guide/index.html. In short, you can annotation your test with #MicronautTest and that causes useful things to happen including starting up the app and subjecting your test class to dependency injection.
I hope that helps.

Related

#Autowired variables access in Spock setupSpec()

I need to execute block of code once upon startup on the Spock tests. I cannot use #Autowired in setupSpec() which is default method for such initialisation, however #Beans would not be loaded till that time.
Found on web (dating back to 2015) source :
The behavior is a consequence of the design of Spring's TestContext framework. I don't
see a way to change it without hitting other problems. The situation isn't any different
when using the TestContext framework with JUnit.
It's been 6 years already, is there any clean way to do this? I want to omit dirty workarounds
You are in luck, thanks to #erdi for implementing this in Add support for injection into #Shared fields in spock-spring module, you can try the feature in the Spock Snapshot 2.0 builds, and it will be in the Spock-2.0M5 release. You need to opt-in into #Shared injection via placing #EnableSharedInjection on your specification, also really important, that you read the javadoc and understand the mentioned implications of doing that.

How to use in memory database with injected test with play java

The play documentation about functional tests in java shows two modes
using fakeApplication to specify a custom configuration (in memory database in the example)
using dependency injection to configure the application
I would like to use dependency injection but I have to set custom configuration on application startup like the use of in memory database.
I cannot achieve to do that. I guess this has to be done in the guice builder but I don't know how.
I am assuming you are using Guice for DI.
The discussion on issue 4809 on the framework's github repo should help on answering your question.
I was able to make it work using one of the solutions on the issue:
new GuiceApplicationBuilder()
.configure((Map) Helpers.inMemoryDatabase())
.in(Mode.TEST)
.build();

How do you custom configure RESTeasy for use with Mockito?

I'm trying to figure out how to configure RESTeasy for use with Mockito. I had to create custom Serializers and Deserializers. I found this article that talked about creating custom serializers and deserializers and turning them into a module.
I also found this article and this article about configuring Jackson to use the new custom module that I wrote.
The problem that I'm having is that I don't know how to test it. One of the articles says
The answer is to have Jackson(Jaxb)JsonProvider in the resteasy.providers section of resteasy-jaxrs.war/WEB-INF/web.xml
I'm using Mockito to test everything. As far as I know, there isn't a resteasy-jaxrs.war file that is made. Is there anyway that I can fake this?
I should probably also add that I'm using spring and I have an applicationContext.xml file that I use just for creating fake beans etc. for testing.
If you want unit tests REST application go with REST-assured. It is very simple and powerful library - you don't need to mock everything. For other options see Ways to test RESTful services?.
Remember that even if you manage to mock everything, such test might be hardly useful - you'll test your mocks, not a true application.

Mocking my custom dependencies with Spring

Is is possible to declare mocks using some mocking framework for my own classes declaratively with Spring? I know there are some standard mocks available in Spring, but I'd like to be able to mock out my own classes declaratively too.
Just to check I'm not going about this the wrong way: the idea is to have a pair of JUnit test and Spring config for each integration test I want to do, mocking everything except the specific integration aspect I'm testing (say I had a dependency on two different data services, test one at a time) and minimising the amount of repeated Java code specifying the mocks.
I did it using special context.xml that included the real XML and overwrote definition of the special beans. Id'd be happy to know that there is better and smarter solution but this one worked fine for me.
Seriously - you really dont want to be doing that.
I have seen a number of projects that attempt to do this and i promise that you will end up with
A huge number of spring files, each one slightly different, but you don't know what and why.
Spaghetti code, because the "declarative" definition doesn't allow to figure out that your objects are doing too much, or doing it with the wrong collaborators.
In the system case, there are a number of points at which you can stub out external services...
I would recommend that you read GOOS - It devotes a book to answering this kind of question.
http://www.growing-object-oriented-software.com/
If there is only a few beans that you want to change, and you want to change them for all tests, the you could have a look at the #Primary annotation.
You have to annotate the special class for the tests with #Primary - then it will "override" the real class. -- But use this only if you want to do it for all tests.

testNG tests extending BaseTest

We noticed that when testNG test cases extend TestCase (JUnit) those tests start executing as Junit tests. Also, I should probably mention, the tests are run through Maven.
Is this a bug or a feature? Is it possible to override this behavior and still run those types of tests as TestNG tests? Do you know a link where TestNG talks about this?
thanks.
I didn't think either TestNG or JUnit required any base classes now that both use annotations to specify test methods. Why do you think you need to extend a class? And why on earth would a TestNG class extend the JUnit base class TestCase? Is it any surprise that they run as JUnit tests?
It sounds like neither bug nor feature but user error on your part. I'm not sure what you're getting at here. Why would you do this?
UPDATE: Your question is confusing me. Did you have JUnit tests running successfully that you're not trying to convert to TestNG, or visa versa? I'm having a very hard time understanding what you're trying to achieve here. Leave Maven out of it. It's immaterial whether they're run by you, Ant, or Maven.
Looking at the maven surefire plugin info I can't see any way to select a test for TestNG processing only if it also extends a jUnit 3 class.
IFAIK your best bet is to just work on each class seperately, removing the jUnit references and then retesting. That way you never have the mixture in one class and you should avoid problems. To make the work manageable I would be inclined to do this only when I was changing a test case for some other reason.

Categories

Resources