How to get an instance of scala.Unit in Java code - scala-java-interop

I'm trying to implement (A, B) => Unit from Java code. How do I return a value for Unit?

Unit is the scala equivalent of void in Java, so there's no need to return anything.

Related

How to run event sequentially in Mutiny

I am using the Mutiny library within the Quarkus framework in Java 11.
I am wonderring which is the best way of running several events sequentially by storing them into a Multi object. I am going to describe my issue in the following java-like pseudocode:
for(P1 p1 : params1){
for(P2 p2 : params2){
multiObject.add(functionThatRetunsUni(p1, p2))
}
}
multiObject.runAll().sequentially();
I need to develop the actions sequentially since the function described in the pseudocode persist entities in a DB, so it maybe the case that two of the calls to the method need to persist the same entity.
I don't know about the best way, but I tend to use a builder object for running several Uni sequentially.
// I'm just assuming the return type of functionThatReturnsUni is Uni<String> for this brief example
Builder<String> builder = Uni.join().builder();
for (P1 p1 : params1){
for (P2 p2 : params2){
builder.add(functionThatReturnsUni(p1, p2));
}
}
return builder.joinAll().andFailFast();

Multiplatform runBlocking coroutine tests fail if last assertion doesn't have a Unit return type

I'm testing my coroutine functions in my Kotlin Multiplatform project. I've mocked out the implementation behind them, so no actual await occurs during tests.
Consider the following test, curated from the test README:
#Test fun testAsyncFunction() = runBlocking {
val result: List<myClass> = myService.someSuspendFunction()
assertEquals(result.first()?.name, "name")
assertNotNull(result.first()?.someRequiredValue)
}
The second assertion has a return type of T, which causes its result to be returned to the runBlocking function, throwing the following error:
Invalid test class 'com.example.shared.core.service.ExampleTests':
1. Method testAsyncFunction() should be void
I've found 2 solutions to this, either I can swap the two assertions around (assertEquals has a Unit return type, thus no issues), or write val ignored = assertNotNull(result.first()?.someRequiredValue). However neither of these two solutions are ideal, as I'll either have extraneous code that my IDE is warning me to remove, or my assertions are out of order.
What is the best solution to this issue?
The problem is that the method is inferring the return type from runBlocking, which returns value from the inner suspending function.
You can force it to generate a void return type by specifying the return type : Unit explicitly rather than rely on the inferred value.

Where to put reusable pure functions in java?

I can't decide where to store reusable pure functions in Java. Example :
class ServiceA(){
private C pureFunction1(A a, B b) {
//code to produce C c;
return c;
}
}
class ServiceB(){
private C pureFunction1(A a, B b) {
//code to produce C c;
return c;
}
}
According to DRY i should extract this pure function somewhere.
I've considered to put it into following places :
Static helper class (smell + against SOLID's dependency inversion principle)
Spring bean (isn't it an overkill for just a pure function)
Super class (does not feel like a right thing for two independent services)
Interface with default method (Interfaces have different purpose)
Where would you recommend to put code for pureFunction1?
My preference would be for static helper class if there is no business logic involved in the method. For example, computing dates which don't have any business logic would be a right candidate for static helper class.
Spring bean can be an option if there is some proper business logic involved in the method
Having superclass may not be the right idea. Reasons here
I prefer static helper class. With java 8 you can declare static method in interface (and implement it in your services).
No difference between spring bean and static helper class for your case
Super class - not good idea without multiple inheritance feature.

Postconditions and TDD

One colleague in my team says that some methods should have both preconditions & postconditions. But the point is about code coverage, those conditions were not being called (not tested) until an invalid implementation implemented (just used in unit test). Lets take below example.
public interface ICalculator
{
int Calculate(int x, int y);
}
public int GetSummary(int x, int y)
{
// preconditions
var result = calculator.Calculate(x, y);
// postconditions
if (result < 0)
{
**throw new Exception("...");**
}
return result;
}
Two options for us:
1/ Remove test implementations + postconditions
2/ Keep both test implementations + postconditions
Can you give some advice please?
Keep pre- and post-conditions.
You'll need at least four tests here: combinations of (pre, post) x (pass, fail). Your failing post-condition test will pass if the expected exception is thrown.
This is easy to do in JUnit with its #Test(expected = Exception.class) annotation.
Be careful with colleagues that make blanket statements like "X must always be true." Dogma in all its forms should be avoided. Understand the reasons for doing things and do them when they make sense.
These conditions should be seen from design view. They ensure the calculator should be working fine, returning result in a range of expected values.
You should see the MS code contracts project to take a document there.

Why empty collection assertion does not work in MSTest?

I have an assertion like the following
Assert.AreEqual(1.Primes(), new List());
Where Primes returns IList and the code for primes is
public static class PrimesKata
{
public static IList Primes(this int n)
{
return new List();
}
}
as you can guess I am trying out the prime number kata, when using MSTest for unit testing this test fails but the same code works just fine in NUnit. Is there something extra I need to do in MSTest for this test to pass?
NUnit's Assert supports equality of collections.
MSUnit doesn't. You can use CollectionAssert in MSTest instead.
In .NET (unlike Java, for instance) two lists are not equal just because they have the same contents.

Resources