刘光辉
5 小时以前 06f314545ac3514d7ce28939920866ae8188d31c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package jnpf.elnController;
 
import jnpf.elnEntity.runtime.ElnRecordVos;
import jnpf.elnService.ElnRecordQueryService;
import jnpf.util.context.SpringContext;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.support.StaticApplicationContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
 
import java.lang.reflect.Method;
import java.util.concurrent.atomic.AtomicReference;
 
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
 
public class ElnRecordQueryControllerTest {
 
    @BeforeClass
    public static void setUpSpringContext() {
        SpringContext.setApplicationContext(new StaticApplicationContext());
    }
 
    @Test
    public void exposesSideEffectFreeLookupByTestWorkItem() throws Exception {
        AtomicReference<String> captured = new AtomicReference<>();
        ElnRecordVos.Created expected = new ElnRecordVos.Created();
        ElnRecordQueryService service = testWorkItemId -> {
            captured.set(testWorkItemId);
            return expected;
        };
        ElnRecordQueryController controller = new ElnRecordQueryController(service);
 
        assertSame(expected, controller.getByTestWorkItem("item-1").getData());
        assertEquals("item-1", captured.get());
 
        RequestMapping mapping = ElnRecordQueryController.class.getAnnotation(RequestMapping.class);
        Method method = ElnRecordQueryController.class.getMethod("getByTestWorkItem", String.class);
        assertEquals("/records", mapping.value()[0]);
        assertEquals("/byTestWorkItem/{testWorkItemId}", method.getAnnotation(GetMapping.class).value()[0]);
        assertEquals("testWorkItemId", method.getParameters()[0].getAnnotation(PathVariable.class).value());
    }
}