Skip to content
Advertisement

Tag: junit

Testing method called with specific string

I am trying to find answer to my question but unable to find something working. I have a class which does this and my test is How would I test that calling llw.logInfo actually calls lambdaLogger.log with the appropriate string? Answer Invoke the logInfo() method on the instance under test : And use the Mockito#verify() method to assert that the

How to abstract away java.time.Clock for testing purposes in Spring

I have a question with regards to the question Time dependent unit tests Let’s say I build Spring application which contains service interface and its implementation If I want to change clock in test, I would have to “pollute” production code and interface with e.g. setClock method as follows: In test, I can invoke, e.g.: How can I abstract away

How to reset Hibernate sequence generators?

I’m using Hibernate 3.5.6-Final with an Oracle database for production and a H2 database for integration tests. The Hibernate mapping for ID creation looks like this with every entity extending EasyPersistentObject: Before each JUnit integration test I am removing all data from the database with Everything works fine until I increment the allocationSize for sequence generation. Raising this to e.g.

toString method for a linkedList

I am getting strange output for my toString in my linkedList class. I cannot use any methods, only String concat. So limited in how to approach this. Here is the code: I wrote a JUnit test that: and that noOrderList().toString() comes from: When I run the test I get: Was is the cause of this , in the [, ]

Junit Mockito error on initialization

I have looked everywhere online. I have tried almost everything, from deleting STS to deleting all the code from my station and re-import everything back. I played with dependencies, build path, properties, etc. but still nothing works. Running the tests on Maven by CLI is working fine, and other team members don’t have that issue, so it is probably some

Test if an exception is caught with Junit

I’ll begin with a code example; I have to test a function, which handles data-packets. In this function, the data-packet is opened and when it doesn’t contain all expected data, an InvalidParameterExeption is thrown which is logged. So, if everything goes well, my exception is printed in my terminal. But how can I test this? I can’t use this: (because

JUnit – How many files for testing a single class?

Let’s say I have a class MyClass.java and we have to write a tests for it. Some of them will execute once, some of them will execute multiple times. I prepared two test classes – MyClassTestSingle.java for single tests and MyClassTestMultiple.java for tests with @Parametrized annotation. I wanted to run them from a single file so there is another class

Mocking a singleton with mockito

I need to test some legacy code, which uses a singleton in a a method call. The purpose of the test is to ensure that the clas sunder test makes a call to singletons method. I have seen similar questions on SO, but all the answers require other dependencies (different test frameworks) – I’m unfortunately limited to using Mockito and

Advertisement