Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: fix unit tests after app-config changes #359

Merged
merged 1 commit into from
Dec 23, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions vaadin-cdi/src/test/java/com/vaadin/cdi/CdiVaadinServletTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,18 @@
import javax.servlet.ServletException;

import java.util.Collections;
import java.util.Enumeration;

import com.vaadin.flow.server.startup.ApplicationConfiguration;
import com.vaadin.flow.server.startup.ApplicationConfigurationFactory;
import com.vaadin.flow.server.startup.DefaultApplicationConfigurationFactory;
import org.apache.deltaspike.core.api.provider.BeanProvider;
import org.apache.deltaspike.testcontrol.api.junit.CdiTestRunner;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;

import com.vaadin.cdi.context.ServiceUnderTestContext;
Expand Down Expand Up @@ -61,6 +66,14 @@ public void setUp() throws ServletException {
Mockito.when(servletContext.getAttribute(Lookup.class.getName()))
.thenReturn(lookup);

final DefaultApplicationConfigurationFactory applicationConfigurationFactory

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no need to mock DefaultApplicationConfigurationFactory or its interface.
It's enough to properly mock ServletContext:
the ApplicationConfiguration is retrieved from the context by the attribute ApplicationConfiguration.class.getName().

If there is no attribute value then the factory is called. But if the attribute is given right away then it will be just returned without asking the factory.

But this is not really important. It just allows to reduce a number of lines.

= Mockito.mock(DefaultApplicationConfigurationFactory.class);
final ApplicationConfiguration applicationConfiguration = Mockito.mock(ApplicationConfiguration.class);
Mockito.when(applicationConfiguration.getPropertyNames()).thenReturn(Collections.emptyEnumeration());

Mockito.when(lookup.lookup(ApplicationConfigurationFactory.class)).thenReturn(applicationConfigurationFactory);
Mockito.when(applicationConfigurationFactory.create(Mockito.any())).thenReturn(applicationConfiguration);

Mockito.when(servletConfig.getInitParameterNames())
.thenReturn(Collections.emptyEnumeration());
Mockito.when(servletConfig.getServletContext())
Expand Down