Grails - How to Unit Test addTo* - unit-testing

Is it possible to unit test addTo* functions in Grails ?
thanks for your help.

The documentation says in section 9.1:
In Grails you need to be particularity
aware of the difference between unit
and integration tests because in unit
tests Grails does not inject any of
the dynamic methods present during
integration tests and at runtime.
You either have to use mockDomain(DomainClassName) in a unit test
or write an integration test:
Grails decorates domain object with some Dynamic methods when the DomainClassGrailsPlugin gets setup(doWithDynamicMethods).

I hit this problem in upgrading from Grails 2.1.2 to Grails 2.3.x. Where as before you only needed to mock the domain class you are adding to, now you need to also mock the domain class being added. Simple with Annotations.
#TestFor(YourService)
#Mock([MyClass, MyOtherClass])
class YourServiceTests {
.... //now myClass.addToMyOtherClasses(myOtherClassInstance) should work fine in your test or in the code being tested
}

Related

Include $_SERVER['PHP_AUTH_USER'] for all Unit Test with PHPUnit and Laravel 5.1

I'm testing Rest APIs.
For all test, I need to define :
$_SERVER['PHP_AUTH_USER'] = 'email#company.com';
$_SERVER['PHP_AUTH_PW'] = 'WgYRBz-Z9H9x-27exLt';
I'm sure it is posible to declare just one those variables, and to use it in my unit test, but don't know how!
Any idea?

Using both Arquillian and PowerMock in the same JUnit test

I would like to use the features of both Arquillian and PowerMock in the same JUnit 4 test.
The problem is that both products are JUnit Runners which should be used with #RunWith, and this is not possible to use multiple #RunWith on the same test class, or to put multiple Runners class in the same #RunWith annotation.
Do you know any way to do that ?
You can use PowerMock without using the runner if you use the PowerMockRule (which is a TestRule). From the PowerMockRule:
Since version 1.4 it's possible to bootstrap PowerMock using a JUnit
Rule instead of using the PowerMockRunner and the RunWith annotation.
This allows you to use other JUnit runners while still benefiting from
PowerMock's functionality. You do this by specifying:
#RunWith(Arquillian.class);
public class MyTest {
#Rule
PowerMockRule rule = new PowerMockRule();
// Tests goes here
...
}
See also the answers to Junit Parameterized tests together with Powermock - how? and the following thread in the PowerMock google group: Using PowerMock without the RunWith?.
No, you either need to:
use one and create a test base class that does the things you wanted the other runner to do.
separate your test into multiple tests, each using different runners.
JUnit4 only supports one #RunWith annotation, and JUnit4's #RunWith annotation doesn’t accept multiple runners.
Reference: project13

Correct way to metaprogram in grails so its available in unit tests

I can add a method to the java Integer type in Groovy with the lines:
ExpandoMetaClass.EnableGlobally()
Integer.metaClass.gimmeAP = {->return 'p'}
I don't know why I'd need that, but it gets the point across. Now I can make calls to Integers and get back a 'p'. Now lets say I want this in a grails app so I can make calls in the domain objects. The specific problem I'm having is that when I put those metaprogramming lines in the bootstrap all the metaprogramming isn't available in the unit tests, so my unit tests are failing with errors like "No method gimmeAP for java.lang.Integer" or something like that.
How do I either include the metaprogramming better, or execute that part of the bootstrap so I can use my tricked out syntax in unit tests?
I have seen this question: Grails - Making Methods Globally Available and Metaclass Programming and it seems my line ExpandoMetaClass.EnableGlobally() may fix his problem, but am I using it right?
Bootstrap isn't executed for unit tests. I would personally prefer to create a mockFoo method that does the above meta programming, and Then I will call the mockFoo from the test setup.
Also look at the GrailsUnitTestCase.registerMetaClass. Register metaclass before you add mock methods, so that they don't leak in other tests.
registerMetaClass(SecurityUtils)
SecurityUtils.metaClass.'static'.getSubject = { ->
return [logout: { return true } ] as Subject
}
I know you want to make your dynamic methods available to all unit tests, but there's nothing such as bootstrap for unit tests. So you have to do it in each test.
You can create a MockHelper with a static method, and call it from test setUp.

GWT unit test for Activity and View

Does anybody have a link for a tutorial on how to write JRE junit tests (extending TestCase and not GWTTestCase) that tests Activity and Views in GWT 2.1?
Best regards
Pich
Views can only be unit tested using GWTTestCase because they call (either explicitly or implicitly) GWT.create().
To test Activities use mock Views to avoid use of GWT.create().
I have managed to test views, which of course contains reference to the objects that calls GWT.create(), using PowerMock.
For Activities it is easy todo using Mockito for instance to mock the View.

Mocking a datacontext for an object that has a dependency

I'm writing some unit tests in my project and I have a datacontext dependency on the controller containing the methods I'd like to test.
I'm using Ninject to inject the dependency and Moq to create my mock datacontext. My DI makes use of an interface IDataContext which my dbml impliments and is used through out the injection process.
In my unit test I'm creating my mock datacontext as follows:
var mock = new Mock<IDataContext>();
var myController = new MyController(mock.Object);
This throws a Object reference not set to an instance of an object. exception on the second line whilst executing the datacontexts constructor.
I'm clearly missing a fundamental piece in setting this up however most of the Moq examples I've seen involve some kind of test against the mocked object using Setup().
Am I going about this the right way? Should I be creating a mock of my IDataContext interface or something else?
haha,
the answer came while i was reading through emad's blog on unit testing in ASP.Net MVC.
I'm guessing that you didn't add the connection string to the app.config of your test project right? :)
And that's the database dependency way because you're still not mocking the database end.
So if you want to do that, you need to google up for some codes, there are a few ways to do that.
i find these few references below to be quite useful, but since i don't really have a need to mock the database end, i'm just using my test DB server for now.
link