Skip to content

Commit 78c9707

Browse files
committed
[Misc] Move 2 tests to JUnit5
1 parent e5e3872 commit 78c9707

File tree

2 files changed

+62
-58
lines changed

2 files changed

+62
-58
lines changed

xwiki-platform-core/xwiki-platform-url/xwiki-platform-url-schemes/xwiki-platform-url-scheme-standard/src/test/java/org/xwiki/url/internal/standard/PathWikiReferenceExtractorTest.java

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,19 @@
2121

2222
import java.net.URL;
2323

24-
import org.junit.Before;
25-
import org.junit.Rule;
26-
import org.junit.Test;
24+
import org.junit.jupiter.api.BeforeEach;
25+
import org.junit.jupiter.api.Test;
2726
import org.xwiki.context.Execution;
2827
import org.xwiki.context.ExecutionContext;
2928
import org.xwiki.model.reference.WikiReference;
30-
import org.xwiki.test.mockito.MockitoComponentMockingRule;
29+
import org.xwiki.test.junit5.mockito.ComponentTest;
30+
import org.xwiki.test.junit5.mockito.InjectMockComponents;
31+
import org.xwiki.test.junit5.mockito.MockComponent;
3132
import org.xwiki.url.ExtendedURL;
3233
import org.xwiki.wiki.descriptor.WikiDescriptor;
3334
import org.xwiki.wiki.descriptor.WikiDescriptorManager;
3435

35-
import static org.junit.Assert.assertEquals;
36+
import static org.junit.jupiter.api.Assertions.assertEquals;
3637
import static org.mockito.Mockito.*;
3738

3839
/**
@@ -41,78 +42,81 @@
4142
* @version $Id$
4243
* @since 6.3M1
4344
*/
44-
public class PathWikiReferenceExtractorTest
45+
@ComponentTest
46+
class PathWikiReferenceExtractorTest
4547
{
46-
@Rule
47-
public MockitoComponentMockingRule<PathWikiReferenceExtractor> mocker =
48-
new MockitoComponentMockingRule<>(PathWikiReferenceExtractor.class);
48+
@InjectMockComponents
49+
private PathWikiReferenceExtractor extractor;
4950

51+
@MockComponent
5052
private WikiDescriptorManager wikiDescriptorManager;
5153

52-
@Before
53-
public void setUp() throws Exception
54+
@MockComponent
55+
private Execution execution;
56+
57+
@MockComponent
58+
private StandardURLConfiguration urlConfiguration;
59+
60+
@BeforeEach
61+
void setUp() throws Exception
5462
{
55-
this.wikiDescriptorManager = mocker.getInstance(WikiDescriptorManager.class);
56-
when(wikiDescriptorManager.getMainWikiId()).thenReturn("xwiki");
63+
when(this.wikiDescriptorManager.getMainWikiId()).thenReturn("xwiki");
5764
}
5865

5966
@Test
60-
public void extractWhenWikiDescriptor() throws Exception
67+
void extractWhenWikiDescriptor() throws Exception
6168
{
6269
setUpConfiguration(WikiNotFoundBehavior.REDIRECT_TO_MAIN_WIKI);
6370

64-
WikiDescriptorManager wikiDescriptorManager = mocker.getInstance(WikiDescriptorManager.class);
65-
when(wikiDescriptorManager.getByAlias("someWiki")).thenReturn(new WikiDescriptor("wikiid", "someWiki"));
71+
when(this.wikiDescriptorManager.getByAlias("someWiki")).thenReturn(new WikiDescriptor("wikiid", "someWiki"));
6672

6773
testAndAssert("http://localhost/xwiki/wiki/someWiki/view/Main/WebHome", "wikiid");
6874
}
6975

7076
@Test
71-
public void extractWhenNoWikiDescriptor() throws Exception
77+
void extractWhenNoWikiDescriptor() throws Exception
7278
{
7379
setUpConfiguration(WikiNotFoundBehavior.REDIRECT_TO_MAIN_WIKI);
7480
testAndAssert("http://localhost/xwiki/wiki/someWiki/view/Main/WebHome", "xwiki");
7581
}
7682

7783
@Test
78-
public void extractWhenWikiDescriptorButEmptyServerName() throws Exception
84+
void extractWhenWikiDescriptorButEmptyServerName() throws Exception
7985
{
8086
setUpConfiguration(WikiNotFoundBehavior.REDIRECT_TO_MAIN_WIKI);
8187

82-
WikiDescriptorManager wikiDescriptorManager = mocker.getInstance(WikiDescriptorManager.class);
83-
when(wikiDescriptorManager.getByAlias("someWiki")).thenReturn(new WikiDescriptor("", "someWiki"));
88+
when(this.wikiDescriptorManager.getByAlias("someWiki")).thenReturn(new WikiDescriptor("", "someWiki"));
8489

8590
testAndAssert("http://localhost/xwiki/wiki/someWiki/view/Main/WebHome", "xwiki");
8691
}
8792

8893
@Test
89-
public void extractWhenNoDescriptorMatchingAliasButDescriptorMatchingId() throws Exception
94+
void extractWhenNoDescriptorMatchingAliasButDescriptorMatchingId() throws Exception
9095
{
9196
setUpConfiguration(WikiNotFoundBehavior.REDIRECT_TO_MAIN_WIKI);
9297

93-
WikiDescriptorManager wikiDescriptorManager = mocker.getInstance(WikiDescriptorManager.class);
94-
when(wikiDescriptorManager.getByAlias("someWiki")).thenReturn(null);
95-
when(wikiDescriptorManager.getById("someWiki")).thenReturn(new WikiDescriptor("dummy", "dummy"));
98+
when(this.wikiDescriptorManager.getByAlias("someWiki")).thenReturn(null);
99+
when(this.wikiDescriptorManager.getById("someWiki")).thenReturn(new WikiDescriptor("dummy", "dummy"));
96100

97101
testAndAssert("http://localhost/xwiki/wiki/someWiki/view/Main/WebHome", "somewiki");
98102
}
99103

100104
@Test
101-
public void extractWhenNoWikiDescriptorButWithDomainBasedURL() throws Exception
105+
void extractWhenNoWikiDescriptorButWithDomainBasedURL() throws Exception
102106
{
103107
setUpConfiguration(WikiNotFoundBehavior.REDIRECT_TO_MAIN_WIKI);
104108
testAndAssert("http://wiki.server.com/xwiki/bin/view/Main/WebHome", "xwiki");
105109
}
106110

107111
@Test
108-
public void extractWhenNoWikiDescriptorAndDisplayErrorWhenWikiNotFound() throws Exception
112+
void extractWhenNoWikiDescriptorAndDisplayErrorWhenWikiNotFound() throws Exception
109113
{
110114
setUpConfiguration(WikiNotFoundBehavior.DISPLAY_ERROR);
111115
testAndAssert("http://localhost/xwiki/wiki/someWiki/view/Main/WebHome", "somewiki");
112116
}
113117

114118
@Test
115-
public void extractWhenNoExecutionContext() throws Exception
119+
void extractWhenNoExecutionContext() throws Exception
116120
{
117121
testAndAssert("http://localhost/xwiki/wiki/someWiki/view/Main/WebHome", "somewiki");
118122

@@ -125,19 +129,16 @@ private void testAndAssert(String urlToTest, String expectedWikiId) throws Excep
125129
ExtendedURL url = new ExtendedURL(new URL(urlToTest), "xwiki");
126130
// Remove the resource type (i.e. the first segment) since this is what is expected by the extractor
127131
url.getSegments().remove(0);
128-
WikiReference wikiReference = this.mocker.getComponentUnderTest().extract(url);
132+
WikiReference wikiReference = this.extractor.extract(url);
129133
assertEquals(new WikiReference(expectedWikiId), wikiReference);
130134
}
131135

132-
private void setUpConfiguration(WikiNotFoundBehavior wikiNotFoundBehavior) throws Exception
136+
private void setUpConfiguration(WikiNotFoundBehavior wikiNotFoundBehavior)
133137
{
134138
// Simulate a configured Execution Context
135-
Execution execution = mocker.getInstance(Execution.class);
136139
ExecutionContext executionContext = new ExecutionContext();
137140
executionContext.setProperty("xwikicontext", true);
138-
when(execution.getContext()).thenReturn(executionContext);
139-
140-
StandardURLConfiguration urlConfiguration = mocker.getInstance(StandardURLConfiguration.class);
141-
when(urlConfiguration.getWikiNotFoundBehavior()).thenReturn(wikiNotFoundBehavior);
141+
when(this.execution.getContext()).thenReturn(executionContext);
142+
when(this.urlConfiguration.getWikiNotFoundBehavior()).thenReturn(wikiNotFoundBehavior);
142143
}
143144
}

xwiki-platform-core/xwiki-platform-url/xwiki-platform-url-schemes/xwiki-platform-url-scheme-standard/src/test/java/org/xwiki/url/internal/standard/StandardExtendedURLResourceReferenceSerializerTest.java

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,23 @@
1919
*/
2020
package org.xwiki.url.internal.standard;
2121

22-
import org.junit.Rule;
23-
import org.junit.Test;
22+
import javax.inject.Named;
23+
24+
import org.junit.jupiter.api.Test;
2425
import org.xwiki.component.manager.ComponentLookupException;
2526
import org.xwiki.component.manager.ComponentManager;
2627
import org.xwiki.component.util.DefaultParameterizedType;
2728
import org.xwiki.resource.AbstractResourceReference;
2829
import org.xwiki.resource.ResourceReferenceSerializer;
2930
import org.xwiki.resource.ResourceType;
3031
import org.xwiki.resource.UnsupportedResourceReferenceException;
31-
import org.xwiki.test.mockito.MockitoComponentMockingRule;
32+
import org.xwiki.test.junit5.mockito.ComponentTest;
33+
import org.xwiki.test.junit5.mockito.InjectMockComponents;
34+
import org.xwiki.test.junit5.mockito.MockComponent;
3235
import org.xwiki.url.ExtendedURL;
3336

34-
import static org.junit.Assert.assertEquals;
35-
import static org.junit.Assert.fail;
37+
import static org.junit.jupiter.api.Assertions.assertEquals;
38+
import static org.junit.jupiter.api.Assertions.assertThrows;
3639
import static org.mockito.Mockito.*;
3740

3841
/**
@@ -41,11 +44,15 @@
4144
* @version $Id$
4245
* @since 6.1M2
4346
*/
44-
public class StandardExtendedURLResourceReferenceSerializerTest
47+
@ComponentTest
48+
class StandardExtendedURLResourceReferenceSerializerTest
4549
{
46-
@Rule
47-
public MockitoComponentMockingRule<StandardExtendedURLResourceReferenceSerializer> mocker =
48-
new MockitoComponentMockingRule<>(StandardExtendedURLResourceReferenceSerializer.class);
50+
@InjectMockComponents
51+
private StandardExtendedURLResourceReferenceSerializer serializer;
52+
53+
@MockComponent
54+
@Named("context")
55+
private ComponentManager componentManager;
4956

5057
public class TestResourceReference extends AbstractResourceReference
5158
{
@@ -56,41 +63,37 @@ public TestResourceReference()
5663
}
5764

5865
@Test
59-
public void serialize() throws Exception
66+
void serialize() throws Exception
6067
{
6168
TestResourceReference resource = new TestResourceReference();
6269

6370
ResourceReferenceSerializer serializer = mock(ResourceReferenceSerializer.class);
6471

65-
ComponentManager componentManager = this.mocker.getInstance(ComponentManager.class, "context");
66-
when(componentManager.getInstance(new DefaultParameterizedType(null, ResourceReferenceSerializer.class,
72+
when(this.componentManager.getInstance(new DefaultParameterizedType(null, ResourceReferenceSerializer.class,
6773
TestResourceReference.class, ExtendedURL.class), "standard")).thenReturn(serializer);
6874

69-
this.mocker.getComponentUnderTest().serialize(resource);
75+
this.serializer.serialize(resource);
7076

7177
// Verify the serializer is called and with the proper parameters
7278
verify(serializer).serialize(same(resource));
7379
}
7480

7581
@Test
76-
public void serializeWhenNoMatchingSerializer() throws Exception
82+
void serializeWhenNoMatchingSerializer() throws Exception
7783
{
7884
TestResourceReference resource = new TestResourceReference();
7985

80-
ComponentManager componentManager = this.mocker.getInstance(ComponentManager.class, "context");
81-
when(componentManager.getInstance(new DefaultParameterizedType(null, ResourceReferenceSerializer.class,
86+
when(this.componentManager.getInstance(new DefaultParameterizedType(null, ResourceReferenceSerializer.class,
8287
TestResourceReference.class, ExtendedURL.class), "standard")).thenThrow(
8388
new ComponentLookupException("error"));
84-
when(componentManager.getInstance(new DefaultParameterizedType(null, ResourceReferenceSerializer.class,
89+
when(this.componentManager.getInstance(new DefaultParameterizedType(null, ResourceReferenceSerializer.class,
8590
TestResourceReference.class, ExtendedURL.class))).thenThrow(
8691
new ComponentLookupException("error"));
8792

88-
try {
89-
this.mocker.getComponentUnderTest().serialize(resource);
90-
fail("Should have thrown an exception here");
91-
} catch (UnsupportedResourceReferenceException expected) {
92-
assertEquals("Failed to find serializer for Resource Reference [type = [test], parameters = []] and "
93-
+ "URL format [standard]", expected.getMessage());
94-
}
93+
Throwable exception = assertThrows(UnsupportedResourceReferenceException.class, () -> {
94+
this.serializer.serialize(resource);
95+
});
96+
assertEquals("Failed to find serializer for Resource Reference [type = [test], parameters = []] and "
97+
+ "URL format [standard]", exception.getMessage());
9598
}
9699
}

0 commit comments

Comments
 (0)