Since i upgraded to WildFly 23 I have not been able to get shrinkwrap/arquillian to resolve classes correctly.
Here is the createDeployment function
public static Archive<?> createBasicShrinkWrappedDeployment()
{
File[] mavenImports = Maven.resolver()
.loadPomFromFile("pom.xml")
.importRuntimeDependencies()
.resolve()
.withTransitivity()
.asFile();
return ShrinkWrap.create(WebArchive.class, "<project>-tests.war")
.addAsLibraries(mavenImports)
.addPackages(true, "org.<company>.crs")
.addAsResource("jbossas-managed/test-persistence.xml", "META-INF/persistence.xml")
.addAsResource("jbossas-managed/test-orm.xml", "META-INF/orm.xml")
.addAsResource("templates/email/template1.vm")
.addAsResource("templates/email/template2.vm")
.addAsResource("templates/email/template3.vm")
.addAsResource("templates/email/template4.vm")
.addAsResource("templates/pdf/template5.vm")
.addAsWebInfResource("beans.xml", "beans.xml");
}
My issue is that for testing we have some test data that exists at: org.<company>.crs.utils, it is a bunch of static data that we use for our functional tests to compare the expected database data to the static data in the application. Here is an example:
package org.<company>.crs.utils;
public class UserInfo{
public static class Id
{
public static UUID Steve = UUID.fromString("...");
public static UUID TestPerson = UUID.fromString("...");
public static UUID Anonymous = UUID.fromString("...");
}
... <more test classes like Id>
}
Now, when we run the tests we may run something like:
Assert.assertEquals(permission.getIdentityId(), UserInfo.Id.Steve);
However, UserInfo.Id.Steve is null, i am assuming this is a shrinkwrap or arquillian issue since that data is statically defined and cannot be null.
This had worked until we updated the application server from WF8 to WF23 (and made a bunch of other changes along the way). Wondering if anyone knows what caused this, or how to resolve it?
Further developments in the troubleshooting process have concluded that this is an issue with (i think) my IDE and not the testing framework. See the above comments for a link to the new question about the IDE issue.
Related
I am writing an extension for a test using #ParameterizedTest. My test looks like:
#ParameterizedTest
#ValueSource(strings={"string1", "string2"})
public void test1(String name){
System.out.println(name);
}
In the extensions, how do I get the TestDesciptor to find out which invocation in currently active?
public class MyExtension implements BeforeEachCallBack{
#Override
public void beforeEach(ExtensionContext extensionContext)
// What to do here? extensionContext.?
}
}
As you can see by its package name org.junit.platform.engine.TestDescriptor is not part of JUnit Jupiter API which has org.junit.jupiter.api as its base package. Moreover, org.junit.platform.engine is not even imported by the API.
So the sad answer is you cannot get at the test descriptor, at least not without some dirty and unstable reflection. I am, however, quite sure that the underlying reason you have for wanting access can be mitigated in a different way. So what is it you’d like to achieve?
I have been trying to set up a Junit 5 extension to force every test to get a separate ClassLoader. I am able to do it quite easily in Junit4, creating my own BlockJUnit4ClassRunner. But, I fail to have it work now.
The purpose is to be able to test things such as static blocks or memorized fields in different states.
I have been trying to use the TestInstanceFactory without any success so far with something like that:
public class SeparateClassLoaderExtension implements TestInstanceFactory {
#SneakyThrows
#Override
public Object createTestInstance(TestInstanceFactoryContext factoryContext, ExtensionContext extensionContext) throws TestInstantiationException {
ClassLoader testClassLoader = new TestClassLoader();
final Class<?> testClass = Class.forName(factoryContext.getTestClass().getName(), true, testClassLoader);
Constructor<?> defaultConstructor = testClass.getDeclaredConstructor();
defaultConstructor.setAccessible(true);
return defaultConstructor.newInstance();
}
}
I get an exception from Junit saying that the class is not of the right type.
Someone any idea?
JUnit Jupiter does not support this, yet. Here's the related issue: https://github.com/junit-team/junit5/issues/201
TL;DR How does one write unit tests for NetBeans Platform code which uses static methods to look up dependencies?
In a NetBeans platform application I come across code like this:
MyService service = Lookup.getDefault().lookup(MyService.class);
service.doStuff(....);
To me the static access seems like an antipattern and hard to test. When I Google around I only find comments about low coupling and high cohesion, teleinterfaces etc.
Many people seem to think this is a Good Idea but I am wondering how I can write a reasonable unit test for code like this, without resorting to mocking static methods or using the Lookup feature in my unit test.
The first idea that comes to my mind is to refactor the lookup as a regular dependency:
public class MyClass {
private Lookup lookup = Lookup.getDefault();
public void myMethod() {
MyService service = lookup.lookup(MyService .class);
service.doStuff(....);
}
public void setLookup(Lookup lookup) {
this.lookup = lookup;
}
And then use the setter to provide a mock Lookup for testing.
This would work, but still causes the tested code to call Lookup.getDefault() before setting the mock. There is no regular dependency injection mechanism provided by Netbeans Platform so if I introduce it like this it feels like swimming against the stream.
I get the feeling I am missing something. Is there a standard approach to write unit tests for Netbeans Platform code?
So far I found several ways of solving this.
1 - Publish a test version of the class in Lookup with a higher position
#org.openide.util.lookup.ServiceProvider(service = MyService.class, position = 1)
public class TestService implements MyService {
public void doStuff(....) {
2 - Use NBJunit's MockService
public class MyTest extends NbTestCase {
public void setUp() throws Exception {
org.netbeans.junit.MockServices.setServices(TestService.class);
}
3- Register your own lookup implementation:
static {
System.setProperty("org.openide.util.Lookup", TestLookup.class.getName());
}
public class TestLookup extends org.openide.util.lookup.AbstractLookup {
public TestLookup() {
this(new org.openide.util.lookup.InstanceContent());
}
private TestLookup(org.openide.util.lookup.InstanceContent ic) {
super(ic);
ic.add(new TestService());
}
Some of these ideas were found here: https://openide.netbeans.org/tutorial/test-patterns.html.
The class TestService must be visible in your test class (In general, we use Lookup to loose dependency, that's why the interfaces and the implementations are in separate modules).
Think about adding the module of TestService in the independency of your test module.
I am new to Android . . .Till now i was thinking that all the resource's ids are in android app are managed and mapped using R.java file. But i got an application in market to work on,in which i did not found R.java/R.class file in it, after decompiling it using dex2jar utility
my issue is that till now my work was dependant on R.java/ R.class file , I was accessing all ids in an app using this file and reflection concept. But since this app does not contain R.class file my work is stuckked for now.
Interesting thing for me is that, when i create dex file from this jar and replace it in app and sign app using one_click_signer ,the app works fine on mobile.
So ,
i want to know how they could have managed ids without R.java?
Is it possible to have an application without R.java, or i may have
made mistake while decompiling app?
how can i find ids in the application?
They used R but it was optimized away.
Let's say the app had this code:
public class R {
public static class id {
public static final int something = 123456;
}
}
public class Main {
public void doSomething() {
doSomethingWith(R.id.something);
}
}
When compiling Java, the compiler "inlines" static final fields - replacing the field access with the value, since the compiler already knows the value. That means the compiler translates the code to this:
public class R {
public static class id {
public static final int something = 123456;
}
}
public class Main {
public void doSomething() {
doSomethingWith(123456);
}
}
If the application is optimized/obfuscated with Proguard - which is common with Android applications - then Proguard would then detect that the class "R" is not used and delete it, resulting in this:
public class Main {
public void doSomething() {
doSomethingWith(123456);
}
}
I've got some code I'm deploying to Google App Engine - Java (GAE/J) that makes use of the URLFetchService. I'd like to use JUnit to test this code. Per the testing documentation, it appears I should have a test that uses their LocalURLFetchServiceTestConfig class ROUGHLY as follows:
public class MyRemoteServiceTests {
private static final LocalURLFetchServiceTestConfig urlConfig = new LocalURLFetchServiceTestConfig();
private static final LocalServiceTestHelper helper =
new LocalServiceTestHelper(urlConfig);
#Before
public void setUp() throws Exception {
service = new SampleService();
helper.setUp();
}
#After
public void tearDown() throws Exception {
service = null;
helper.tearDown();
}
#Test
public void testThatCallsCodeThatUsesUrlFetch() {
Object data = service.getRemoteDataUsingUrlFetch("foo", "bar");
Assert.assertNotNull(data);
}
}
I'm finding that this test continues to fail despite using the "helper" as suggested in the GAE/J documentation on testing: "The API package 'urlfetch' or call 'Fetch()' was not found.".
I was assuming that using the "helper" would somehow setup the GAE environment such that when I call URLFetchServiceFactory.getURLFetchService() from within my getRemoteDataUsingUrlFetch method, the interface returned would be an instance of LocalURLFetchService that would just "work" but that seems NOT to be the case.
How can I test this code?
Am I missing something? (I'm pretty new to GAE...)
Do I have to refactor my getRemoteDataUsingUrlFetch so that it doesn't use URLFetchServiceFactory.getURLFetchService() because that makes it untestable locally??? (That sounds like it would really suck...)
Any help/suggestions much appreciated!
Actually, it turns out my problem was failure to include two additional jars that ARE mentioned on the Local Unit Testing page of the documentation. RTM FTW!
appengine-local-runtime.jar
appengine-api-stubs.jar
afaik, the LocalURLFetchService doesn't configure the GAE like you expect. It is more of a way to fetch URL from the local dev and then process the contents however. (Similarly even the LocalDatastoreService and LocalMemcacheService operate on isolated spaces within the test environment)
One way to test your code is to refactor the getRemoteDataUsingUrlFetch() to take maybe the contents of the Url response. somewhat like,
URLFetchResponse resp = LocalURLFetchService.fetch(status, request)
getRemoteDataUsingUrlFetch(foo, bar, resp)