I'm getting a very strange error with Mockito:
java.lang.IllegalStateException
at java.util.LinkedList$ListItr.remove(LinkedList.java:923)
at org.mockito.internal.debugging.WarningsFinder.find(WarningsFinder.java:36)
at org.mockito.internal.debugging.WarningsPrinterImpl.print(WarningsPrinterImpl.java:28)
at org.mockito.internal.debugging.WarningsCollector.getWarnings(WarningsCollector.java:34)
at org.mockito.internal.junit.JUnitRule$1.evaluate(JUnitRule.java:29) <26 internal calls>
There is no reference to my test code, but here are the classes I'm using and the test:
Endpoint.java: Uses Retrofit 2.0
public interface Endpoint {
#GET("items/{itemId}")
Call<List<Item>> list(#Path("itemId") String itemId);
Call<List<Item>> list(#Path("itemId") String itemId, #Path("alternativeId") String alternativeId);
}
Query.java
public class Query {
private String itemId;
// constructor, etc
public String getItemId() {
return itemId;
}
}
ItemDataSource.java: Unfinished implementation (following TDD)
public class ItemDataSource {
private Endpoint endpoint;
// constructor, etc
public Optional<Item> get(Query query) {
List<Item> list = null;
try {
list = endpoint.list(query.getItemId()).execute().body();
} catch (IOException e) {
e.printStackTrace();
}
Template result = modelAdapter.apply(list.get(0));
return Optional.ofNullable(result);
}
}
ItemDataSourceTest.java:
public class TemplateNetworkDataSourceTest {
#Rule
public MockitoRule mockitoRule = MockitoJUnit.rule();
#Mock
private Query query;
#Mock
private Item item;
#Mock(answer = RETURNS_DEEP_STUBS)
private ItemEndpoint endpoint;
#Test
public void shouldProvideItemFromEndpoint() throws Exception {
when(query.getItemId()).thenReturn("itemId");
when(endpoint.list("itemId").execute()).thenReturn(Response.success(singletonList(networkTemplate)));
ItemDataSource dataSource = new ItemDataSource(endpoint);
Optional<Item> actual = dataSource.get(query);
assertThat(actual.get()).isEqualTo(item);
}
#Test
public void shouldProvideNoItemsWhenNotFound() throws Exception {
//when(query.getProductId()).thenReturn("incorrect_productId"); // <- works
when(endpoint.list(anyString()).execute()).thenReturn(Response.success(emptyList()));
ItemDataSource dataSource = new ItemDataSource(endpoint);
Optional<Item> actual = dataSource.get(query);
assertThat(actual.isPresent()).isFalse();
}
}
If I run it, the first test runs correctly but the second gives me the provided error. If I comment out the first line in the statement of the second test it works (so that could be the answer) but I'm wondering if I'm missing something or if this is a bug on Mockito.
From my slight digging through Mockito's code I see that a List is modified when collecting the warnings after the test is executed.
Related
Below is main code consist of one util class and service class using it
#PropertySource("classpath:atlas-application.properties")
public class ApacheAtlasUtils {
#Value("${atlas.rest.address}")
private String atlasURL;
#Value("${atlas.rest.user}")
private String atlasUsername;
#Value("${atlas.rest.password}")
private String atlasPassword;
private AtlasClientV2 client;
public AtlasClientV2 createClient() {
if (client == null) {
return new AtlasClientV2(new String[] {atlasURL}, new String[] {atlasUsername, atlasPassword});
} else {
return client;
}
}
}
Service Class is below :-
#Override
public Page<SearchResultDto> findFilesWithPages(QueryParent queryParent, Pageable pageable)
throws AtlasServiceException {
// Some code
client = new ApacheAtlasUtils().createClient();
//some code
}
I am writing unit test for service method and I am getting exception for createClient method asking for values for url, username and password which should not happen as this should be mocked but the mocking is giving me below error
java.lang.IllegalArgumentException: Base URL cannot be null or empty.
at com.google.common.base.Preconditions.checkArgument(Preconditions.java:141)
at org.apache.atlas.AtlasServerEnsemble.<init>(AtlasServerEnsemble.java:35)
at org.apache.atlas.AtlasBaseClient.determineActiveServiceURL(AtlasBaseClient.java:318)
at org.apache.atlas.AtlasBaseClient.initializeState(AtlasBaseClient.java:460)
at org.apache.atlas.AtlasBaseClient.initializeState(AtlasBaseClient.java:448)
at org.apache.atlas.AtlasBaseClient.<init>(AtlasBaseClient.java:132)
at org.apache.atlas.AtlasClientV2.<init>(AtlasClientV2.java:82)
at com.jlr.stratus.commons.utils.ApacheAtlasUtils.createClient(ApacheAtlasUtils.java:40)
at com.jlr.stratus.rest.service.impl.FileSearchService.findFilesWithPages(FileSearchService.java:49)
The Test code is as follows:-
private FileSearchService fileSearchService;
#Spy
private ApacheAtlasUtils apacheAtlasUtils;
#Mock
private AtlasClientV2 client;
#Before
public void setup() {
MockitoAnnotations.initMocks(this);
fileSearchService = new FileSearchService();
}
#Test
public void findFilesWithPages_searchAll() throws AtlasServiceException {
Mockito.doReturn(client).when(apacheAtlasUtils).createClient();
service.search(queryParent,pageable);
}
Your idea with spying is adequate (you can even go for mocking if you do not actually need any true implementation of that class).
The problem lies in the implementation:
// Some code
client = new ApacheAtlasUtils().createClient();
//some code
}
Instead of having the ApacheAtlasUtils as an instance variable (or a supplier method) you create the instance on the fly.
Mockito is not smart enough to catch that operation and replace the real object with you spy.
With the supplier method you can set up your test as follows:
#Spy
private FileSearchService fileSearchService = new FileSearchService();
#Spy
private ApacheAtlasUtils apacheAtlasUtils = new ApacheAtlasUtils();
#Mock
private AtlasClientV2 client;
#Before
public void setup() {
MockitoAnnotations.initMocks(this);
doReturn(apacheAtlasUtils).when(fileSearchService).getApacheUtils();
}
in your SUT:
#Override
public Page<SearchResultDto> findFilesWithPages(QueryParent queryParent, Pageable pageable)
throws AtlasServiceException {
// Some code
client = getApacheUtils().createClient();
//some code
}
ApacheAtlasUtils getApacheUtils(){
return new ApacheAtlasUtils();
}
I am trying to write unit tests for Repository layer classes with Junit and Mockito.
I have mocked the base class that supplies NamedParameterJdbcOperations and tried to inject into the repo class.
In the repo class, we are loading sql queries from files on classpath. This is done in a method that is annotated with #PostConstruct.
When trying to test a method of the repo, it is not able to find or load the query and thus throwing NullPointerException.
Need help / suggestion on how to deal with such scenario.
PS: I am not allowed to change the repo class implementation.
Attaching the code of repo and test class for reference.
RepositoryImpl.java
#Repository
public class RepositoryImpl extends AppJdbcImpl implements
Repository {
private static final StudentMapper STUDENT_ROW_MAPPER = new StudentMapper();
private static final CourseMapper COURSE_ROW_MAPPER = new CourseMapper();
#Value("classpath:sql/sql1.sql")
private Resource sql1;
private String query1;
#Value("classpath:sql/sql2.sql")
private Resource sql2;
private String query2;
public RepositoryImpl() { }
public RepositoryImpl(NamedParameterJdbcOperations jdbc) {
super(jdbc);
}
#PostConstruct
public void setUp() {
query1 = loadSql(sql1);
query2 = loadSql(sql2);
}
public Iterable<Course> findCoursesByStudentId(int studentId) throws
DataAccessException {
try {
return jdbc().queryForObject(query1,
ImmutableMap.of("studentId", studentId),
COURSE_ROW_MAPPER);
} catch (EmptyResultDataAccessException emptyResult) {
return null;
} catch (DataAccessException e) {
// Need to create exception classes and throw specific exceptions
throw e;
}
}
public Iterable<Student> findStudentsByCourseId(int courseId) throws DataAccessException {
try {
return jdbc().query(query2,
ImmutableMap.of("courseId", courseId),
STUDENT_ROW_MAPPER);
} catch (DataAccessException e) {
// Need to create exception classes and throw specific exceptions
throw e;
}
}
private String loadSql(Resource resource) {
try {
return CharStreams.toString(new InputStreamReader(resource.getInputStream()));
} catch (IOException e) {
return null;
}
}
}
RespositoryImplTest.java
#RunWith(MockitoJUnitRunner.class)
public class RepositoryImplTest {
#Mock
private NamedParameterJdbcOperations jdbc;
#Mock
private ResultSet resultSet;
#Mock
private StudentMapper studentMapper;
#Mock
private CourseMapper CourseMapper;
#InjectMocks
private RepositoryImpl repository;
private Student student1;
private Student student2;
private Course course1;
private Course course2;
#Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
course1 = new Course(1, "Karate");
course2 = new Course(2, "Riding");
course8 = new Course(8, "Swimming");
List<Course> courseList = Arrays.asList(course1, course2, course8);
student1 = new Student(1, "Chuck", "Norris", 27, new Arrays.asList(course1, course2));
student2 = new Student(2, "Bruce", "Lee", 54, new Arrays.asList(course1, course8));
List<Student> studentList = Arrays.asList(student1, student2);
when(jdbc.queryForObject(Matchers.anyString(), anyMap(),
isA(StudentMapper.class)))
.thenAnswer(new Answer() {
#Override
public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
Object[] args = invocationOnMock.getArguments();
int queryParam = Integer.parseInt(args[0].toString());
Iterable<Credentials> result = studentList.stream()
.filter(d -> d.getId() == queryParam)
.collect(Collectors.toList());
return result;
}
});
}
#Test
public void findCoursesByStudentId() {
Iterable<Course> result = repository.findCoursesByStudentId(1);
assertNotNull(result);
}
}
In repo class, exception is thrown as query1 is null.
Need help to properly solving the issue.
Thanks, Baru
#RunWith(MockitoJUnitRunner.class)
you start test with mockito starter, not spring starter. It's mean that spring not provided you beans. Mockito starter nothing know about PostConstruct annotation.
You may call PostConstruct method youself in sturUp junit method or in test method.
public class MyXML {
private MessageParser messageParser;
private String valueA;
private String valueB;
private String valueC;
public MyXML (MessageParser messageParser) {
this.messageParser=messageParser;
}
public void build() {
try {
setValueA();
setValueB();
setValueC();
} catch (Exception e) {
e.printStackTrace();
}
}
private void setValueA() {
valueA = messageParser.getArrtibuteUsingXPath("SomeXPath1...");
}
private void setValueB() {
valueB = messageParser.getArrtibuteUsingXPath("SomeXPath2...");
}
private void setValueC() {
valueC = messageParser.getArrtibuteUsingXPath("SomeXPath...");
}
public String getValueA() {
return valueA;
}
public String getValueB() {
return valueB;
}
public String getValueC() {
return valueC;
}
}
So I need to use Mockito to test the builder method. Im fairly new to Mockito could someone give me some example code as to how I might write a test for the builder method?
If you want to suggest any ways I might change the design of the class or make it easier to test let me know.
To test build() you can try :
#RunWith(MockitoJUnitRunner.class)
public class YourTest {
#Mock
private private MessageParser messageParserMock;
// this one you need to test
private MyXML myXML;
#Test
public void test() {
myXML = new MyXML(messageParserMock);
// I believe something like this should work
Mockito.doAnswer(/* check mockito Answer to figure out how */)
.when(messageParserMock).getArrtibuteUsingXPath(anyString());
// you should do this for all your 3 getArrtibuteUsingXPath because setValueA(), setValueB(), setValueC() are called that one and then call build and verify results
myXML.build(); // for instance
assertEquals("something you return as Answer", myXML.getValueA());
}
}
The resource https://static.javadoc.io/org.mockito/mockito-core/2.8.9/org/mockito/Mockito.html#stubbing_with_exceptions might be useful - it describes how to stub void methods call.
I want use a Powermock to test a private method (one) but this private method relies on another private method (two).
So I have to mock the other private method. But while I am debugging it, it turns out that the private method one is not calling the mocked private method (two) and if I run instead of debugging it throws out exception:
1matchers expected, 2 recorded.
private int getCurrentLocaleID(WebIServerSession currentSession, String preferenceName) {
String prefLocaleID = getUserPreference(currentSession, preferenceName);
int lcid;
if (HTTPHelper.isDefaultLocale(prefLocaleID)) {
prefLocaleID = _appContext.getBrowserHeaderLocaleId();
}
try {
lcid = Integer.parseInt(prefLocaleID);
} catch (NumberFormatException nfe) {
lcid = DEFAULT_LCID; // default behavior from old session manager
}
return lcid;
}
#Test
public void getCurrentLocaleID() throws Exception {
PowerMockito.mockStatic(HTTPHelper.class);
WebAppSessionManagerImpl webAppSessionMangerImpl2 = PowerMockito.spy(new WebAppSessionManagerImpl(appConext));
given(HTTPHelper.isDefaultLocale("1")).willReturn(true);
given(HTTPHelper.isDefaultLocale("0")).willReturn(false);
given(appConext.getBrowserHeaderLocaleId()).willReturn("1");
PowerMockito.doReturn("1").when(webAppSessionMangerImpl2, "getUserPreference", anyObject(), anyString());
int result = Whitebox.invokeMethod(webAppSessionMangerImpl2, "getCurrentLocaleID", webIserverSession, "test");
assertEquals(result, 1);
}
Dont test private methods. If you have to, that means that your class is doing too much than it supposed to and it does not comply with the Single Responsibility Principle.
This is a chance for some refactoring and isolation of logic in specialized class like something follwing:
public class SpecializedClass{
private Context context;
public SpecializedClass(Context context){
this.context = context;
}
public int getCurrentLocaleID(WebIServerSession currentSession, String preferenceName) {
String prefLocaleID = getUserPreference(currentSession, preferenceName);
int lcid;
if (HTTPHelper.isDefaultLocale(prefLocaleID)) {
prefLocaleID = _appContext.getBrowserHeaderLocaleId();
}
try {
lcid = Integer.parseInt(prefLocaleID);
} catch (NumberFormatException nfe) {
lcid = DEFAULT_LCID; // default behavior from old session manager
}
return lcid;
}
String getUserPreference(Session session, String preferenceName){..}
}
Now haiving the method public and the getUserPreference marked as package level, the test would look something like:
#Test
public void getCurrentLocaleID() throws Exception {
PowerMockito.mockStatic(HTTPHelper.class);
SpecializedClass specializedClassSpy = Mockito.spy(new SpecializedClass(appConext));
given(HTTPHelper.isDefaultLocale("1")).willReturn(true);
given(HTTPHelper.isDefaultLocale("0")).willReturn(false);
given(appConext.getBrowserHeaderLocaleId()).willReturn("1");
Mockito.doReturn("1").when(specializedClassSpy)
.getUserPreference(webIserverSession, "test");
int result = specializedClassSpy.getCurrentLocaleID(webIserverSession, "test");
assertEquals(result, 1);
}
I came accross a problem with JUnit 4.12 I cannot explain when testing a method in my code which should throw a specific exception.
I'm using the #Test annotation with (expected = MyException.class).
MyException is simply derived from Exception.
Due to an programming error (missing 'throw' clause) this exception is not thrown. Because of the test case setup including mocked classes using jmockit 1.29, later in the method a NullPointerException is thrown.
Example classes shwoing the basic class layout:
package l00tr.test;
public class MyException extends Exception {
private static final long serialVersionUID = 1L;
public MyException(String msg) {
super(msg);
}
}
package l00tr.test;
import java.util.Map;
import java.util.Set;
public class MyClass {
private Set<String> testSet;
private Map<String, String> testMap;
private boolean condition;
private MyClass() {
this.testSet = null;
this.testMap = null;
this.condition = false;
}
private MyClass(Set<String> testSet, Map<String, String> testMap,
boolean condition) {
this.testSet = testSet;
this.testMap = testMap;
this.condition = condition;
}
public void method() throws MyException {
if (testSet.size() < 2) {
throw new MyException("test set");
}
if (!condition) {
// FORGOTTEN: throw
new MyException("test");
}
System.out.print("set size: " + testSet.size());
System.out.print("some more stuff succeeding");
System.out.print("map size: " + testMap.size()); // NullPointerException
}
public static final class Builder {
private Set<String> testSet;
private Map<String, String> testMap;
private boolean condition;
private Builder() {
this.testSet = null;
this.testMap = null;
this.condition = false;
}
public static Builder newBuilder() {
return new Builder();
}
public Builder testSet(Set<String> testSet) {
this.testSet = testSet;
return this;
}
public Builder testMap(Map<String, String> testMap) {
this.testMap = testMap;
return this;
}
public Builder condition(boolean condition) {
this.condition = condition;
return this;
}
public MyClass build() {
return new MyClass(testSet, testMap, condition);
}
}
}
So I would expect the test to fail. But it did succeed. Removing the "(expected = ...)" part of the Test annotation results in the test failing with the NullPointerException as to expect.
package l00tr.test;
import java.util.Set;
import org.junit.Test;
import mockit.Expectations;
import mockit.Mocked;
public class MyClassTests {
#Mocked
private Set<String> testSet;
#Test(expected = MyException.class)
public void testMethod() throws MyException {
new Expectations() {
{
testSet.size();
result = 3;
}
};
MyClass mc = MyClass.Builder.newBuilder().condition(false)
.testSet(testSet).build();
mc.method();
}
}
Jacoco helped to spot this, otherwise I would have wrongly assumed everything to be fine. I also checked the open issues for junit 4.12 but did not find a matching issue.
Why does the test succeed? Any ideas?
Update:
I improved the example to show the relevant details of my code. Unfortunately, the code pasted above does behave as expected, i.e. showing the error:
java.lang.Exception: Unexpected exception, expected<l00tr.test.MyException> but was<java.lang.NullPointerException>
My specific development environment setup may play a role, so here are some details:
gradle 3.2.1 with active daemon
junit 4.12
jmockit 1.29
eclipse Neon.1a Release (4.6.1)
java version "1.8.0_112" 64 bit
Windows 10
After the helpful comments, I've found out that the problem could be narrowed down to the "'false positive' problem with annotation based exception checking with Junit4":
After checking again the full stacktrace and code including an abstract superclass, I've found a try/catch converting any exception to MyException after logging. So from tool and code perspective, everything works as to expect.