From faf211fe8747394d89a19a36cd6f2e5c7d474118 Mon Sep 17 00:00:00 2001 From: Bryan Parry Date: Tue, 21 Mar 2017 23:21:54 -0400 Subject: [PATCH] Create unit test for LazilyInstantiate --- .../cloud/kubernetes/LazilyInstantiate.java | 2 +- .../kubernetes/LazilyInstantiateTest.java | 72 +++++++++++++++++++ 2 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 spring-cloud-kubernetes-core/src/test/java/org/springframework/cloud/kubernetes/LazilyInstantiateTest.java diff --git a/spring-cloud-kubernetes-core/src/main/java/org/springframework/cloud/kubernetes/LazilyInstantiate.java b/spring-cloud-kubernetes-core/src/main/java/org/springframework/cloud/kubernetes/LazilyInstantiate.java index 6255ca59..6bd9e49d 100644 --- a/spring-cloud-kubernetes-core/src/main/java/org/springframework/cloud/kubernetes/LazilyInstantiate.java +++ b/spring-cloud-kubernetes-core/src/main/java/org/springframework/cloud/kubernetes/LazilyInstantiate.java @@ -29,7 +29,7 @@ public class LazilyInstantiate implements Supplier { return current.get(); } - private LazilyInstantiate(Supplier supplier) { + private LazilyInstantiate(Supplier supplier) { this.supplier = supplier; this.current = () -> swapper(); } diff --git a/spring-cloud-kubernetes-core/src/test/java/org/springframework/cloud/kubernetes/LazilyInstantiateTest.java b/spring-cloud-kubernetes-core/src/test/java/org/springframework/cloud/kubernetes/LazilyInstantiateTest.java new file mode 100644 index 00000000..0263c55d --- /dev/null +++ b/spring-cloud-kubernetes-core/src/test/java/org/springframework/cloud/kubernetes/LazilyInstantiateTest.java @@ -0,0 +1,72 @@ +/* + * Copyright (C) 2016 to the original authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.kubernetes; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.verifyZeroInteractions; +import static org.mockito.Mockito.when; + +import java.util.function.Supplier; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; + +@RunWith(MockitoJUnitRunner.class) +public class LazilyInstantiateTest { + + private static final String SINGLETON = "singleton"; + + @Mock + private Supplier mockSupplier; + + @Before + public void setUp() throws Exception { + // common setup + when(mockSupplier.get()).thenReturn(SINGLETON) + .thenThrow(new RuntimeException("Supplier was called more than once!")); + } + + @Test + public void supplierNotCalledInLazyInstantiateFactoryMethod() { + LazilyInstantiate.using(mockSupplier); + + // verify + verifyZeroInteractions(mockSupplier); + } + + @Test + public void factoryReturnsSingletonFromSupplier() { + LazilyInstantiate lazyStringFactory = LazilyInstantiate.using(mockSupplier); + String singletonString = lazyStringFactory.get(); + + // verify + assertEquals(SINGLETON, singletonString); + } + + @Test + public void factoryOnlyCallsSupplierOnce() { + LazilyInstantiate lazyStringFactory = LazilyInstantiate.using(mockSupplier); + lazyStringFactory.get(); + + // mock will throw exception if it is called more than once + lazyStringFactory.get(); + } + +}