how to override method in other module in Java 9 - module

In my JUnit tests, I need to override some public method behavior from class A (module M1). For that, I am doing something similar to that (in JUnit module M2):
package com.acme;
public class B extends A{
#Override
public String methodToOverride(){
return "TestDataHere";
}
}
How Java 9 JPMS recommended to override some package in 3rd party module or your module? How to properly configure to JPMS to use modules M1 and M2 with the same package names?

Related

Null controller when test with Mockito

I'm testing a controller that needs a Autowired Service.
I read in many place (example Mockito: How to test my Service with mocking?) I need to do it like this
#RunWith(JUnitPlatform.class)
public class AdminControllerTest {
#Mock
private AdminService service;
#InjectMocks
private AdminController adminController;
#Test
public void registerUser() {
Boolean resultReal = adminController.registerUser();
assertTrue(resultReal);
}
}
But it's fail and I see is because the adminController is null
Instead, if I create the controller like this
AdminController adminController = new AdminController();
It works, but then I can inject the mock.
Maybe I'm forgotten something
Per the documentation for InjectMocks:
MockitoAnnotations.openMocks(this) method has to be called to initialize annotated objects. In above example, openMocks() is called in #Before (JUnit4) method of test's base class. For JUnit3 openMocks() can go to setup() method of a base class. Instead you can also put openMocks() in your JUnit runner (#RunWith) or use the built-in MockitoJUnitRunner.
Thus, either:
call openMocks(this) somewhere before the test is run or
Use #RunWith(MockitoJUnitRunner.class) on the class

org.jetbrains.annotations.NotNull instead of javax.annotation.Nonnull when implement method in Intellij IDEA

After recent JetBrains Intellij IDEA updates I found out that when I'm trying to implement method annotated with javax.annotation.Nonnull - IDE implements it with org.jetbrains.annotations.NotNull instead.
Example:
If you have an interface:
import javax.annotation.Nonnull;
interface User {
#Nonnull
String getName();
}
it will be implemented as:
import org.jetbrains.annotations.NotNull;
class Customer implements User {
#NotNull
#Override
public String getName() {
return null;
}
}
The question is how to configure IDE to implement methods with strict validation annotation?
Looks like a defect (https://youtrack.jetbrains.com/issue/IDEA-253324) although there is a workaround exist:
Inspections > Java > Probable bugs > Nullability problems > #NotNull/#Nullable problems > Configure Annotations. Set javax.annotation.Nullable/javax.annotation.Nonnull as defaults and restart the IDE.
To add the library with annotations to a Gradle project, add the implementation org.jetbrains:annotations:23.0.0 dependency to the build.gradle file.

How to use #Autowire when using #ParameterizedTest in Junit5

I currently use SpringBoot1.5 and Junit5.
How do I use #autowire to dependency injection when I use the annotation #ParameterizedTest for parametric testing because I need to interact with the database.
I try to use
TestContextManager testContextManager = new TestContextManager(getClass());
testContextManager.prepareTestInstance(this);
but it will cause the #transaction to be unavailable.
this my code
#ExtendWith(MockitoExtension.class)
#RunWith(SpringRunner.class)
public abstract class AbstractUnitTest {
}
public class PatientFacadeTestParameterized extends AbstractUnitTest {
...
#Autowired
PatientFacade patientFacade;(is null)
...
#Transactional
#ParameterizedTest(name = "{index}: {0}")
#YamlFileSource(resources = {"logistics/patient_facade.yaml"})
public void testCreateAccountPhonePatienta(PatientFacadeData patientFacadeData) {
...
patientFacade.createAccountPhonePatient(patientForm1);
...
}
...
I just want to use #ParameterizedTest to manage my input.
Spring Boot 1.5.x depends on Spring Framework 4.3.x, but the latter does not provide built in support for JUnit Jupiter (a.k.a., JUnit 5).
So, if you want to use Spring Framework 4.3.x with JUnit Jupiter, the only option is to use my spring-test-junit5 project.
Once you have configured the dependency on spring-test-junit5, you will have access to the SpringExtension for JUnit Jupiter. This replaces the SpringRunner for JUnit 4.
You should then be able to rewrite your test class in a manner similar to the following. I cannot provide you an exact working example, since I do not have access to the types in your project.
#ExtendWith(SpringExtension.class)
#ExtendWith(MockitoExtension.class)
public class PatientFacadeTestParameterized {
#Autowired
PatientFacade patientFacade;
#Transactional
#ParameterizedTest(name = "{index}: {0}")
#YamlFileSource(resources = {"logistics/patient_facade.yaml"})
public void testCreateAccountPhonePatienta(PatientFacadeData patientFacadeData) {
// ...
patientFacade.createAccountPhonePatient(patientForm1);
// ...
}
What is responsible for injecting the PatientFacadeData into your test method? Does #YamlFileSource take care of that?
By the way, you should practically never need to use the TestContextManager directly in your tests. The SpringRunner and SpringExtension handle that for you.

How to implement Java interfaces in Frege?

I have been trying out Frege and one of the first things I would like to do is implement a Java interface.
How is that done?
Here's my example in Java:
package mypkg;
import frege.repl.FregeRepl;
import frege.runtime.Concurrent;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
public class FregeMain implements BundleActivator {
public FregeMain() {
}
#Override
public void start( BundleContext context ) throws Exception {
System.out.println( "Frege Bundle activated" );
new Thread( () -> FregeRepl.main( new String[ 0 ] ) ).start();
}
#Override
public void stop( BundleContext context ) throws Exception {
System.out.println( "Frege stopping. Goodbye!" );
Concurrent.shutDownIfExists();
}
}
To implement this in Frege, I would need to know:
how to declare something that will be visible as a class called mypkg.FregeMain implementing BundleActivator in JVM bytecode (notice that this is important as the OSGi framework will scan the jar for classes implementing that interface, and call them automatically).
How to implement a Runnable (as a Haskell lambda, probably) and pass it on to the Thread constructor. Also same issue: implement a Java interface, but this time with an anonymous class or lambda.
I tried to understand the Calling Java from Frege post, but probably due to my lack of experience in Frege/Haskell, I just don't understand most of that.
Thanks for any input.
The simplest way to implement Java interface in Frege is possibly to use an inline module definition. Some thorough examples are in https://github.com/Frege/FregeFX/blob/master/fregefx/src/main/frege/fregefx/JavaFxUtils.fr

Interceptor on super method in CDI 1.0/JEE6

In the following case,
public class Base {
#Transactional
public void doSave() {
// ...
}
}
public class Inherited extends Base {
public void someMethod() {
super.doSave();
}
#Override
public void doSave() {
super.doSave();
}
}
If I add the #Transactional annotation to Inherited.someMethod, the interceptor gets called without issue.
However, without the annotation on the inherited class, the interceptor does not get involved in the call to the super class from Inherited.someMethod().
Furthermore, calling Inherited.doSave() does not seem to get the interceptor invoked either. I would have expected the annotation on the superclass to be also valid on the subclass. Is this not the expected behaviour?
I am using Apache DeltaSpike for the #Transactional annotation and this is being deployed as a war in an ear (technically as a jar in a war in an ear). However, this may not be relevant as trying with a custom interceptor shows the same behaviour.
This is JBoss EAP 6.3.0 Alpha in case its relevant.
This is expected. Interceptors are only applied if the object is managed. When you you write it this way with inheritence, it's not applied as it's not part of a call stack that CDI is aware of. You would need to inject Base into your class and call Base.doSave