Mocking SessionContext

I have a EJB:

@Stateless
@Remote
public class ejbBean implements EjbBean {
	@PersistenceContext
	private EntityManager em;
	@Resource
	private SessionContext ctx;
 
	public String someMethod() {
		String name = ctx.getCallerPrincipal().getName();
		return name;
	}
}
public class EjbBeanTest {
	@Rule
	public final MockitoRule mockitoRule = MockitoJUnit.rule().strictness(Strictness.STRICT_STUBS);
	@Mock
	private EntityManager em;
	@Mock
	private SessionContext ctx;
 
	@InjectMocks
	private EjbBean cut;
 
	@Before
	public void setUp() {
		Principal p = mock(Principal.class);
		when(ctx.getCallerPrincipal()).thenReturn(p);
		when(p.getName()).thenReturn("TestUser");
	}
 
	@Test
	public void test() {
		String result = cut.someMethod();
		assertEquals("TestUser", result);
	}

causes the following exception

org.mockito.exceptions.base.MockitoException: 
Mockito cannot mock this class: interface javax.ejb.SessionContext.

Mockito can only mock non-private & non-final classes.

I made lot of searches over the web. (google, stackOverflow, git, everywhere)
I found some suggestions like https://stackoverflow.com/questions/32695925/how-do-i-mock-a-sessioncontext-injected-via-resource-in-java-ee.
I tried everything.
But what ever I tried I got failing test with the Error message above.
WTF.
My friend give me the tip … Don`t use an mocking framework, make your own dummy.
OK lets try this. Fill the one method I need then instantiate this.

public class DummySessionContext implements SessionContext {
...
	@Override
	public Principal getCallerPrincipal() {
		// TODO Auto-generated method stub
		Principal principal = () -> "TESTUSER";
		return principal;
	}
...	
}

And this brings a new Error up.

java.lang.Error: Unresolved compilation problems: 
	The type javax.xml.rpc.handler.MessageContext cannot be resolved. It is indirectly referenced from required .class files
	MessageContext cannot be resolved to a type

It turns out that there was a missing dependency in pom.
Because we have scope = provide

<dependency>
	<groupId>org.jboss.spec.javax.ejb</groupId>
	<artifactId>jboss-ejb-api_3.2_spec</artifactId>
	<scope>provided</scope>
</dependency>

my mentor said

provided -> means provided by ejb container
provided dependencies will NOT be there for tests
I think you have to create dependency with test

I added the following to my pom

<!-- needed SessionContext mock-->
<dependency>
	<groupId>org.jboss.spec.javax.xml.rpc</groupId>
	<artifactId>jboss-jaxrpc-api_1.1_spec</artifactId>
	<version>1.0.1.Final</version>
	<scope>test</scope>
</dependency>

Now the test works fine.
With my own dummy.
EJB:

@Stateless
@Remote
public class ejbBean implements EjbBean {
	@PersistenceContext
	private EntityManager em;
	private SessionContext ctx;
 
	@Resource
	void setSessionContext(SessionContext ctx) {
		this.ctx = ctx;
	}
 
	public String someMethod() {
		String name = ctx.getCallerPrincipal().getName();
		return name;
	}
}

Dummy:

public class DummySessionContext implements SessionContext {
...
	@Override
	public Principal getCallerPrincipal() {
		// TODO Auto-generated method stub
		Principal principal = () -> "TESTUSER";
		return principal;
	}
...	
}

TEST:

public class EjbBeanTest {
	@Rule
	public final MockitoRule mockitoRule = MockitoJUnit.rule().strictness(Strictness.STRICT_STUBS);
	@Mock
	private EntityManager em;
 
 
	@InjectMocks
	private EjbBean cut;
 
	@Before
	public void setUp() {
		cut.setSessionContext(new DummySessionContext());
	}
 
	@Test
	public void test() {
		String result = cut.someMethod();
		assertEquals("TestUser", result);
	}
}

Furthermore I tried to mock again.
And now this works too.

EJB:

@Stateless
@Remote
public class ejbBean implements EjbBean {
	@PersistenceContext
	private EntityManager em;
	@Resource
	private SessionContext ctx;
 
	public String someMethod() {
		String name = ctx.getCallerPrincipal().getName();
		return name;
	}
}

TEST:

public class EjbBeanTest {
	@Rule
	public final MockitoRule mockitoRule = MockitoJUnit.rule().strictness(Strictness.STRICT_STUBS);
	@Mock
	private EntityManager em;
	@Mock
	private SessionContext ctx;
 
	@InjectMocks
	private EjbBean cut;
 
 
	@Test
	public void test() { 
		Principal p = mock(Principal.class);
		when(ctx.getCallerPrincipal()).thenReturn(p);
		when(p.getName()).thenReturn("TestUser");
 
		String result = cut.someMethod();
		assertEquals("TestUser", result);
	}
}

Conclusion
I my case using the mockito hides the missing dependency. And I had a hard time to figure it out.
Many thanks to my friend and Sensai Jiri.

Search Engine shortcuts

Working with shortcuts will increase your speed and make your work more effective.
I discovered a new cool shortcut.
When I search something on the web I’m switching over several search engines. Like Metager, Google, StackOverflow, GitHub, Wikipedia.
You can set a keyword to the engine.

  1. click „Burger menu“ -> „Einstellungen“ -> „Suche“
  2. double click on the Entry and enter keyword.

searchengine

Now you can simply type into the address bar the chosen shortcut followed by what you like to search.
For Example git java lamda
CTRL+L – switch to address bar or CTRL-T – open a new Tab
s lamda for each – perform https://stackoverflow.com/search?q=lamda+foreach

SVN – Branch aktuell halten

Branch aktuell halten.

Wenn wir in einem Entwicklungsbranch arbeiten sollte regelmäßig (oft) der Trunk-Head in den Entwicklungsbranch gemerged werden. Damit sich die Zweige nicht zu weit von einander entfernen.

In unserem Fall nehmen wir immer die letzte Tag version. Also die letzte released Version des Trunks. So lange wir also in einem Entwicklungsbranch parallel zum Trunk arbeiten sollten nach einem Release alle Changes in den Entwicklungsbranch einfließen.

All changes that are going to be committed to Trunk, must be applied to all changes already committed to Trunk, before those changes are merged into Trunk.

Quelle:https://designbygravity.wordpress.com/2009/10/19/what-mother-never-told-you-about-svn-branching-and-merging/

Vorausetzung am Bsp. Entwicklungsbranch GUI

Der Entwicklungsbrunch ist ausgecheckt und upgedatet und liegt als „saubere“ working copy im Eclipse Workspace.

Das heißt keine Datei hat ein Change welcher nicht eingecheckt ist. (Kein Sternchen)

Schritt-für-Schritt-Anleitung

  1. bei allen pom.xml´s die Version von 8.x.0-GUI-SNAPSHOT auf die neue Version 8.x.0 ändern (bspsw. 8.4.0-GUI-SNAPSHOT auf 8.5.0)
    Damit wir nicht soviele Mergekonflikte in den pom.xml´s bekommen die sich nur auf die Versionsnummer beziehen.
  2. nicht commiten
  3. Rechtsklick auf dem Projektordner in Eclipse -> Team -> Merge
    image2015-7-7-14-40-38
  4. In dem folgenden Screen das Diff auswählen was gemerged werden soll. Also die letzte Version wo der Trunk gemerged wurde (hier Tag 8.0.0) bis zu der Version die released wurde (Tag 8.1.0)
    Ziel ist unsere Working Copy!
    image2015-7-7-14-48-46
  5. Nun alle Konflikte manuell aufheben.
  6. nicht commiten.
  7. in allen poms die Version wieder ändern auf 8.x.0-GUI-SNAPSHOT
    image2015-7-7-14-56-12
  8. Dann einchecken und commit mit merged version 8.x.0 beschreiben.
    Nun haben wir im Branch den selben Entwicklungsstand wie die letzte release Version. Alle Änderungen aus der Trunkentwicklung sind vorhanden.
  9. Projekt (GUI) in Jenkins bauen.