mockito when interact with each other in different method - java

I have a short piece of code and two unit test. Strangely when I launch the test separately they works well but when I launch them together it look like the second method use the "when" of the first method.
Tested method :
public ProductIssuer update(ProductIssuer productIssuer) {
findById(productIssuer.getId())
.orElseThrow(() -> new B4FinanceException(ErrorCode.USER_NOT_FOUND, "Please provide an existing user"));
return productIssuerRepository.save(productIssuer);
}
The tests :
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
public class ProductIssuerServiceTest {
#InjectMocks
private static ProductIssuerService productIssuerService;
#Mock
private static ProductIssuerRepository productIssuerRepository;
public static final UUID DEFAULT_UUID = UUID.fromString("b8fc499a-2084-11e8-b467-0ed5f89f0000");
private static final String DEFAULT_NAME = "productIssuer Name";
#BeforeEach
public void setup() {
MockitoAnnotations.initMocks(this);
}
#Test
public void updateNotFoundThrowException() {
ProductIssuer productIssuer = new ProductIssuer();
productIssuer.setName(DEFAULT_NAME);
when(productIssuerRepository.findById(any())).thenReturn(Optional.empty());
assertThatExceptionOfType(B4FinanceException.class).isThrownBy(() -> productIssuerService.update(productIssuer));
}
#Test
public void update() {
ProductIssuer productIssuer = new ProductIssuer();
productIssuer.setName(DEFAULT_NAME);
productIssuer.setId(DEFAULT_UUID);
when(productIssuerRepository.findById(any())).thenReturn(Optional.of(productIssuer));
when(productIssuerRepository.save(any())).thenReturn(productIssuer);
productIssuerService.update(productIssuer);
}
}
The result is ok for the first test (updateNotFoundThrowException) but for the second test I got a "Please provide an existing user" error.

Related

JUnit setUp doesn't create object

Why do I get a NullPointerExeption for testManuscript when trying to run my test?
This is my Manuscript.java:
package org.lhoffjann;
public class Manuscript {
private String msID;
private String path;
public void setMSid(String msID){
this.msID = msID;
}
public String getMSid() {
return this.msID;
}
}
This is my ManuscriptTest.java:
package org.lhoffjann;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class ManuscriptTest {
private static Manuscript testManuscript;
#Before
public void setUp(){
testManuscript = new Manuscript();
}
#Test
public void testGetMSid() {
testManuscript.setMSid("1234");
assertTrue("1234" == testManuscript.getMSid());
}
}
You have imported #Test from JUnit 5, while you're using #Before from JUnit 4, that combination doesn't work. You need to choose which JUnit version you want to use, 4 or 5, and then consistently import classes from that JUnit version. I would recommend using JUnit 5, and removing all JUnit 4 dependencies from your classpath, or at least configure your IDE to not suggest those imports.
For this specific case, replace #Before (org.junit.Before) with #BeforeEach (org.junit.jupiter.api.BeforeEach).
In the example as shown, you don't even need this setUp method, as each test-execution gets its own instance of the test class. You can use:
private Manuscript testManuscript = new Manuscript();
That is, remove static, initialize the field directly, and remove the setUp method.
Even if you continue to use the setUp method, I recommend removing the static, so testManuscript is an instance field, like it is actually used.
You have mixed Junit4 with Junit5. You should use only one version.
Junit4 or
package org.lhoffjann;
import org.junit.Before;
import org.junit.Test;
import org.junit.Assert;
public class ManuscriptTest {
private static Manuscript testManuscript;
#Before
public void setUp(){
testManuscript = new Manuscript();
}
#Test
public void testGetMSid() {
testManuscript.setMSid("1234");
Assert.assertEquals("1234",testManuscript.getMSid());
}
or
Junit5
package org.lhoffjann;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
class ManuscriptTest {
private static Manuscript testManuscript;
#BeforeEach
public void setUp() {
testManuscript = new Manuscript();
}
#Test
void testGetMSid() {
testManuscript.setMSid("1234");
Assertions.assertEquals("1234", testManuscript.getMSid());
}
}

Is there a way to get the list of scenarios that are to be run in the #BeforeClass annotation in cucumber JVM

I have got a requirement to get the list of all the scenarios that are to be executed based on the tag I provided in cucumber Test runner. However I have to get this list before tests start execution.
I know there is a tag called "#BeforeClass" but I am not sure if I can use to get the list of all the scenarios that are going to be run. For example something like this
#BeforeClass
public void intialize(Scenario[] scenario) throws Exception { }
Below is the code for me test runner class
package com.automation.cucumber;
import com.automation.Utils;
import io.cucumber.java.Scenario;
import io.cucumber.testng.*;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import java.io.File;
#CucumberOptions(features = "features/amazon"
,glue="com.automation.cucumber"
,tags = "#tt"
,dryRun = true
, plugin = {"json:target/cucumber-reports/cucumber.json"})
public class CucumberTestRunner extends AbstractTestNGCucumberTests {
static String resultFolder;
#DataProvider(parallel = true)
public Object[][] scenarios() {
return super.scenarios();
}
#BeforeClass
public void intialize() throws Exception {
resultFolder = Utils.createTestReportFolder();
if(resultFolder==null)
{
throw new Exception("Unable to create a result folder");
}
System.out.println();
}
}
You may have to implement EventListener class to get that information and do dryRun = true in your Runner class in #CucumberOptions
Quoting from a question that can help you achieve what you need
public class DryRunPlugin implements EventListener {
#Override
public void setEventPublisher(EventPublisher publisher) {
publisher.registerHandlerFor(TestCaseStarted.class, this::handleCaseStarted);
}
private void handleCaseStarted(TestCaseStarted event) {
System.out.println(event.getTestCase().getUri());
System.out.println(event.getTestCase().getName());
System.out.println(event.getTestCase().getScenarioDesignation());
event.getTestCase().getTags().stream().forEach(t ->
System.out.println(t.getName()));
}
}

Mockito: Intercepting method call without reference to object

I want to test the following example code:
public class Example {
...
public void doStuff() {
...
Lift lift = new Lift();
lift.call(5);
...
}
...
}
How can I 'intercept' lift.call(5)?
Generally I would use when(lift.call(anyInt()).thenReturn(...), but I have no reference to the Lift object.
You can't do it with mockito alone. The cleanest solution is to refactor your code so you can have access to it. However if that's not an option then "power mockito" is what you want. Grab "powermock-api-mockito"+"powermock-module-junit4" and then something like this will do the trick:
import static org.mockito.Mockito.verify;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
#RunWith(PowerMockRunner.class)
#PrepareForTest(Example.class)
public class ExampleTest {
private Example testSubject;
#Mock
private Lift lift;
#Test
public void testDoStuff() throws Exception {
testSubject.doStuff();
verify(lift).call(5);
}
#Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
PowerMockito.whenNew(Lift.class).withNoArguments().thenReturn(lift);
testSubject = new Example();
}
}
Can you modify the Example class? If yes, the simplest way would be to extract the Lift dependency and provide it via constructor. Like this:
public class Example {
private final Lift lift;
public Example(Lift lift) {
this.lift = lift;
}
public void doStuff() {
this.lift.call(5);
}
}
Then you can stub lift as you want since now you have access to the instance.

Mocked EhCache NullPointerException in JUnit 5 test

I am writing Unit tests for a service I want to test. Several methods try to retrieve values from an EhCache.
I tried mocking them with Mockito and simply have the get(String key) method of Cache return null, since I want to ignore the caching for these tests.
My test class:
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.Arrays;
import java.util.List;
import javax.annotation.Resource;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import com.jysk.dbl.esldataservice.model.Preis;
import com.jysk.dbl.esldataservice.service.PreisService;
import com.jysk.dbl.esldataservice.service.external.PimDataService;
import com.jysk.dbl.esldataservice.service.external.SapCarService;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
public class PreisServiceTest {
#Mock
private SapCarService sapCarService;
#Mock
private ArticleDataService articleDataService;
#Mock
private CacheManager cacheManager;
#Mock
private Cache cache;
#InjectMocks
#Resource
private PreisService preisService;
#BeforeEach
public void setup() {
MockitoAnnotations.initMocks(this);
when(this.cacheManager.getCache(anyString())).thenReturn(this.cache);
when(this.cache.get(anyString())).then(null);
}
protected static final String TEST_STORE_IDENTIFIER = "1234";
private static final String ARTICLE_IDENTIFIER_1 = "12345001";
private static final String ARTICLE_IDENTIFIER_2 = "54321001";
private final Preis p1 = new Preis(ARTICLE_IDENTIFIER_1, 10.00, 15.00, "01", "01", "01");
private final Preis p2 = new Preis(ARTICLE_IDENTIFIER_2, 20.00, 25.00, "02", "02", "02");
#Test
void testGetPreisReturnsOneCorrectPreis() {
when(this.sapCarService.getPreise(Arrays.asList(ARTICLE_IDENTIFIER_1), TEST_STORE_IDENTIFIER, true)).thenReturn(Arrays.asList(this.p1));
final List<Preis> actual = this.preisService.getPreis(ARTICLE_IDENTIFIER_1, TEST_STORE_IDENTIFIER);
verify(this.sapCarService, times(1)).getPreise(anyList(), anyString(), anyBoolean());
assertNotNull(actual);
assertEquals(1, actual.size());
assertEquals(this.p1, actual);
}
}
My implementation:
private Preis searchPreisInCache(String key) {
final Element preisOptional = this.cacheManager.getCache("preis").get(key); // NPE here
if (preisOptional != null) {
final Preis preis = (Preis) preisOptional.getObjectValue();
logger.info(String.format("Preis with key '%s' found in cache 'preis'.", key));
return preis;
}
return null;
}
The stackTrace showed, that the NPE gets thrown inside the net.sf.ehcache.Cache class:
public final Element get(Object key) throws IllegalStateException, CacheException {
getObserver.begin(); // NPE thrown here
checkStatus();
if (disabled) {
getObserver.end(GetOutcome.MISS_NOT_FOUND);
return null;
}
Element element = compoundStore.get(key);
if (element == null) {
getObserver.end(GetOutcome.MISS_NOT_FOUND);
return null;
} else if (isExpired(element)) {
tryRemoveImmediately(key, true);
getObserver.end(GetOutcome.MISS_EXPIRED);
return null;
} else if (!skipUpdateAccessStatistics(element)) {
element.updateAccessStatistics();
}
getObserver.end(GetOutcome.HIT);
return element;
}
Is there any easy solution for this problem, if I simply want the Cache to return null, whenever it's called?
Mockito can't mock final methods and classes without some configuration. As Morfic pointed out, it is posible with Mockito v2.x, like explained here and here.
Basically, you have to add a text file named org.mockito.plugins.MockMaker under the directory src/test/resources/mockito-extensions with the content mock-maker-inline and tada, Mockito can mock final methods and classes.
However, this uses a different engine with different limitations, so be aware of that.

How to make jUnit class have parameter

I have problem with passing a bean class like mvc concept to implement to jUnit class. I don't want change the structure jUnit class, because i have need it.
Class DataBean
public class DataBean {
private String browserName;
private String userName;
public String getBrowserName() {
return browserName;
}
public void setBrowserName(String browserName) {
this.browserName = browserName;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
Class Main
public class Main {
public static void main(String[] args) {
String[] arrBrowserName = {"chrome", "firefox"};
String[] arrUserName = {"user1", "user2"};
for(int i=0; i<2; i++) {
DataBean dataBean = new DataBean();
String browserName = arrBrowserName[i];
String userName = arrUserName[i];
dataBean.setBrowserName(browserName);
dataBean.setUserName(userName);
//How to call "TestCase1", include passing the "databean"
JUnitCore junit = new JUnitCore();
junit.run(TestCase1.class);
}
}
}
Class TestCase1
public class TestCase1 {
DataBean dataBean = new DataBean();
//Here, how to do ? i want to get "databean" from "Main" class, without change this is class as jUnit
#Before
public void setUp(){
//set up based on data from "Main class"
}
#Test
public void scenario(){
//
}
#After
public void tearDown(){
//
}
}
Based on the above code, let's say i have 2 data as data testing, i want setup the before based on the data from Main class. Where is i placement the parameter in TestCase1 so that I can get databean ? and is this possible?
FYI, the DataBean object is a smell called anemic object.
It is a DTO (object to transport data), so except when required by some framework, try to keep these immutable, or at least make it explicit that there is no encapsulation:
public class DataBean {
public String browserName;
public String userName;
}
Anyway, it seems that you are trying to parameterize a test.
Given that the test object is built by JUnit, you will have to change the test class using the API provided by JUnit to inject the needed object/data.
You could use a ThreadLocal storage to bypass the framework API, but I do not recommand it at all.
If you use JUnit, do it the JUnit way.
Assuming that you still use JUnit4, you can go (at least) two ways:
First, using JUnit4 built-in runner Parameterized, making a test class parameterized:
import java.util.Arrays;
import java.util.Collection;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
#RunWith(Parameterized.class)
public class BrowserTest {
#Parameters
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][]{
{new DataBean("chrome", "user1")},
{new DataBean("firefox", "user2")}
});
}
private final DataBean dataBean;
public BrowserTest(DataBean dataBean) {
this.dataBean = dataBean;
}
#Test
public void test() {
// Test using a dataBean instance
}
}
Second, use a third library called JUnitParams with the runner JUnitParamsRunner, making a test method parameterized:
import org.junit.Test;
import org.junit.runner.RunWith;
import junitparams.JUnitParamsRunner;
import junitparams.Parameters;
#RunWith(JUnitParamsRunner.class)
public class BrowserTest {
#Test
#Parameters(method = "test_cases")
public void test(DataBean dataBean) {
// Test using a dataBean instance
}
public static Object[] test_cases() {
return new Object[]{
new DataBean("chrome", "user1"),
new DataBean("firefox", "user2")
};
}
}
Finally, there is the new version, JUnit5 which changes a lot regarding its extention model as it allows composition of features (instead of having only one runner).
With JUnit5, the same test can be written like this:
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
class BrowserTest {
#ParameterizedTest
#CsvSource({
"chrome, user1",
"firefox, user2"
})
void test(String browserName, String userName) {
// Test using a dataBean instance
}
}
Hope this helps !

Categories

Resources