Win a copy of Mastering Corda: Blockchain for Java Developers this week in the Cloud/Virtualization forum!

Kirk James

+ Follow
since Mar 13, 2019
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Kirk James

Hi guys, I've few questions about unit and integration tests with Spring framework.

Lets suppose I've a simple architecture, made by controllers, services and repositories and I want to use Spring to make some tests with the specific mocks and annotations.
This is what I suppose to use for each layer:

Testing repositories -> @DataJpaTest (H2 in-mem database)
Testing services -> @Mock and @InjectMocks (Mockito library)
Testing controllers -> @WebMvcTest and @MockBean (@MockBean for mocking services)

As you can se I didn't find out a reason to use @SpringBootTest and bootstrap the entire Spring Context.

So, my questions are:

- What's the purpose of the annotation @SpringBootTest and when I should use it bootstrapping the entire application context?
- What if I need to test a specific component that's not a service/controller/repository? Should I use @SpringBootTest just for the specific class(es) I need to test and avoiding to bootstrap the entire context? (IE to test a mapping component)
- How can I perform an end to end test without mocking any component (so a real integration test)? Is it usually required as unit testing single layers?

Thanks for your help.
1 week ago
Thank you for your opinion, so, about the scenario I showed previously, do you think that a checked exception could be a better solution?

Supposing that UserNotFoundException is an UncheckedException, I will not force the external layer (Controller) to catch the exception and build a specific body for the Client, in that case so it will be handled as a generic Exception (500) instead of a not found (404)

Snippet edit:

3 months ago

Claude Moore wrote:Have a look at Java Documentation. In a nuthshell, you should use unchecked exception when dealing with unrecoverable errors (a stack overflow, a connection to a DBMS dropped).
My personal taste: use checked exception as much as possibile in your business layer.

Actually I want to recover from every exception by catching it and building a proper body for the Client side of the application.

I mean, when I think about a recoverable error I suppose that somewhere in my code the exception is catched and nothing will be rethrowed back from it.

3 months ago
Hi guys,

I have to ask you a question about the decision to throw checked or unchecked exceptions in a multilayer application.

Let's say that I've the following structure:

- Controller class that will call the Service layer and handle exceptions, returning a body response to the Client.
- Service class, will implement the business logic.

The Service class impements a method to retrieve the User data from a database, a simple findById().
In case of entity not found I would throw a custom exception, let's say UserNotFoundException, that the Controller class will handle, building a proper response to the Client.
The UserService class is called only but the UserController, no other Controller or Service makes use of it.

The question is, should the UserNotFoundException implemented as an unchecked (runtime) or checked exception (force the Controllare layer to handle it)?

Generally, which kind of rule should I follow to decide how to implement an exception handling process?

Thanks for your help.
3 months ago

Paul Clapham wrote:Good to hear that!

As for "what happens under the hood", that isn't what you need to know. You need to know how variables work in Java. Here's a link to a pretty decent tutorial which tells you about that: Variable Scope in Java.

And then you say that some other code tries to use some other value, and it's null. Right?

Well no, it's not what I'm saying or what I tryied to say.

Thanks for your help but I think you misunderstood the point. The behaviour I was facing was not inside the aspect but inside the method that is matching the aspect:

playerRepository.findById(id) was returning null as result, even if the entity exists, so I was facing a null pointer exception: playerRepository.findById(id).orElse(null);.

I'm not sure that it's related to the variables scope, but if you say that for some reason the variable result is needed by the getById method, so probably it could be related to that variable (not specifically about the scope you're telling me), then if it is I want to know the reason why.
7 months ago
Well, it seems I found the problem, I need to return the result in my aspect:

Can someone please explain me what happens under the hood?
7 months ago
Hi guys,

I'm facing a weird problem using Spring-AOP, Spring-Data and H2 in memory database.

This is my aspect:

this is my joinpoint:

Well, the problem is that, the joinpoint is working, matching the findById method, the problem is that playerRepository.findById is returning null (even with an id of an entity that really exists) and I'm facing a null pointer exception.

This is what I see from the logs:

- [ACCESS] data FIND operation: CrudRepository.findById(..) | args: [1]

- [END] method: CrudRepository.findById(..) | result: Optional[MyEntity my properties bla bla bla]

- java.lang.NullPointerException: null

So it's really strange, because I see that the Aspect is working and is retrieving correctly the entity, but when it proceed to the real method then playerRepository.findById return null.

Can you help me to solve this problem?

7 months ago
Hi guys,

what I want to ask is why the following practice is a good one whenever you have to create a WS architecture (but maybe this could not be the only case but a generic good practice):

Why we should expose an interface marked as our API (Api interface) instead of expose the class that implement that interface (Controller class)? Do we follow some specific practice or principles doing this?

1 year ago
Hi all,

the following code shows a REST endpoint made with SpringMVC module:

The PutMapping annotation doesn't provide a consume or produce element that could be:

Is that option (consume/produce) mandatory for some reasons? Why I should use it or not? If I try to make a call with a client (Postman) I can send a JSON format request even if I don't provide the consume/produce element for this endpoint.
Can you please give me more explainations?

1 year ago

Tim Holloway wrote:You confused me. I call "DML" SQL. Since basically, DDL is the part of "SQL" that isn't actually SQL and SQL is the part that was defined by Codd and Date.

Anyway, as I recall, auto-commit is on by default with MySQL and PostgreSQL and probably Oracle, SQL Server and DB2.

Realistically, saving stuff up to do a commit is something more common to programming than for just fiddling around with the database manually, so I'd expect auto-commit to be the default in general.

But I don't think that it's actually mandated anywhere. For that matter, I don't think that there's a standard that says a DBMS even has to have a command line interface application. Although it's going to be a lot less fun tweaking things without one.

About the DML, I use to define DML as Data Manipulation Language, so everything related to your database manipulation (INSERT, DELETE, UPDATE), then DDL as Data Definition Language, everything related to the definition of your data architecture (ALTER, DROP). When you say that:

DDL is the part of "SQL" that isn't actually SQL

sorry but,  I got confused.

Thank you for your reply.

Tim Holloway wrote:Actually, while it might vary depending on which DBMS you're using, I've always seen DDL auto-commit. And actually, having to explicitly commit DDL sounds a bit problematic to me.

What about DML? no auto-commit by default? Never worked on the DBMS settings, probably that behaviour can be managed.

as far as I know DDL commands such as an alter/drop table are by default setted to autocommit on a RDBMS; on the other hand DML need an explicit commit operation.

Can someone confirm that assumption or explain me if some RDBMS have a different behaviuor?


Paul Clapham wrote:Technical? I don't understand what you're asking.

I was just talking about the Spring IOC container that manages the bean instances and that cannot work with static elements.
1 year ago